除去Android中的ListView项目 [英] Remove ListView items in Android

查看:113
本文介绍了除去Android中的ListView项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以请给删除所有的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();
        }
    });

这篇关于除去Android中的ListView项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆