【问题标题】:Stuck trying to refresh ListView after deleting file删除文件后尝试刷新 ListView
【发布时间】:2014-05-14 15:06:47
【问题描述】:

在我的List2 活动(扩展ListActivity)中,我正在删除一个文件,然后我调用方法init(); 来刷新我的ListView,但它并不刷新,它只是重复(出现旧的和新的)个)项目。 如果我单击其中一个新生成的项目,它将强制关闭。我知道notifyDatasetChanged 在我的情况下不起作用。

任何帮助将不胜感激。

这是我的 List2 类:

    public class List2 extends ListActivity {

    ListView lv;
    private ListAdapter adapter;

    public ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

    public ArrayList<HashMap<String, String>> myHash = new ArrayList<HashMap<String, String>>();

    // private ArrayList<DataSetObserver> observers = new
    // ArrayList<DataSetObserver>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list2);

        init();

        lv = getListView();

        registerForContextMenu(getListView());

        // listening to single listitem click
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    final int position, long id) {

                // // Starting new intent
                Intent in = new Intent(getApplicationContext(),
                        secondActivity.class);

                // Sending songIndex to PlayerActivity
                in.putExtra("Index", fileIndex);

                // setResult(100, in);
                // Closing PlayListView
                startActivity(in);
                finish();
            }
        });

    }

    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);

    }

    public boolean onContextItemSelected(MenuItem item) {
        final AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
                .getMenuInfo();

        switch (item.getItemId()) {

        case R.id.delete:

            File file = new File(Path);
            if (file.exists()) {

                file.delete();

                init();

            }

            return true;
        default:
            return super.onContextItemSelected(item);
        }
    }

    public ArrayList<HashMap<String, String>> getPlayList() {
        File home = new File(MEDIA_PATH);

        if (home.listFiles(new FileExtensionFilter()).length > 0) {
            for (File file : home.listFiles(new FileExtensionFilter())) {
                HashMap<String, String> song = new HashMap<String, String>();

                song.put(
                        "songTitle",
                        file.getName().substring(0,
                                (file.getName().length() - 4)));
                song.put("songPath", file.getPath());

                songsList.add(song);
            }
        }

        // return songs list array
        return songsList;
    }

    void init() {

        this.getPlayList();

        // looping through playlist
        for (int i = 0; i < songsList.size(); i++) {
            // creating new HashMap
            HashMap<String, String> song = songsList.get(i);

            myHash.add(song);
        }
        adapter = new SimpleAdapter(this, myHash, R.layout.playlist_item,
                new String[] { "songTitle", "singerName" }, new int[] {
                        R.id.songTitle, R.id.singerName });

        setListAdapter(adapter);

    }

}

【问题讨论】:

    标签: android listview android-listview hashmap listadapter


    【解决方案1】:

    这是因为您添加 myHash 而不清除它,所以它包含所有旧条目以及新条目。在添加更多项目之前,只需致电clear

    void init() {
    
    this.getPlayList();
    myHash.clear();
    
    // looping through playlist
    for (int i = 0; i < songsList.size(); i++) {
    
        // creating new HashMap
        HashMap<String, String> song = songsList.get(i);
    
        myHash.add(song);
    }
    
    adapter = new SimpleAdapter(this, myHash,
    R.layout.playlist_item, new String[] { "songTitle","singerName" }, new int[] {
    R.id.songTitle,R.id.singerName });
    
    setListAdapter(adapter);
    
    }
    

    【讨论】:

    • 感谢您的回答
    • 没问题,很高兴能帮上忙:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-18
    • 1970-01-01
    相关资源
    最近更新 更多