除去Android中的ListView项目项目、Android、ListView

2023-09-11 12:19:28 作者:女生昵称

有人可以请给删除所有的ListView项目和新项目替换了我一个例子code?

Can somebody please give me an example code of removing all ListView items and replacing with new items?

我试着更换适配器项目没有成功。我的code是

I tried replacing the adapter items without success. My code is

populateList(){

 results //populated arraylist with strings

 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, results);

 listview.setAdapter(adapter);
 adapter.notifyDataSetChanged();
 listview.setOnItemClickListener(this);

}

// now populating list again

repopulateList(){

 results1 //populated arraylist with strings

 ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, results1);

 listview.setAdapter(adapter1);
 adapter1.notifyDataSetChanged();
 listview.setOnItemClickListener(this);
}

下面 repopulateList()方法添加到ListView的项目,但它不会删除/替换所有的ListView项目。

Here repopulateList() method will add to ListView items, but it doesn't remove/replace all ListView items.

推荐答案

您想从你的适配器对象中删除()选项,然后只需运行notifyDatasetChanged()上的适配器,任何列表视图将(应该)回收和更新它自己。

You will want to remove() the item from your adapter object and then just run the notifyDatasetChanged() on the Adapter, any ListViews will (should) recycle and update on it's own.

下面是用AlertDialogs一个简短的活动例如:

Here's a brief activity example with AlertDialogs:

adapter = new MyListAdapter(this);
    lv = (ListView) findViewById(android.R.id.list);
    lv.setAdapter(adapter);
    lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> a, View v, int position, long id) {
        AlertDialog.Builder adb=new AlertDialog.Builder(MyActivity.this);
        adb.setTitle("Delete?");
        adb.setMessage("Are you sure you want to delete " + position);
        final int positionToRemove = position;
        adb.setNegativeButton("Cancel", null);
        adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                MyDataObject.remove(positionToRemove);
                adapter.notifyDataSetChanged();
            }});
        adb.show();
        }
    });
 
精彩推荐
图片推荐