Listview删除项目和刷新 - android

时间:2011-12-29 03:58:22

标签: java android

这个问题与此类似 - Android - Listview delete item and Refresh

我无法用:

刷新我的适配器
adapter.notifyDataSetChanged();

我试过了:

adapter.remove(adapter.getItem(pos));

但没有成功,只有一次(奇怪......)。

还有另一个答案:

Call that Activity once again Using Intent

sombody可以给我这个(或适配器/光标)的确切代码吗?

我正在尝试这几个小时没有成功。

我的完整代码:

protected void onCreate (Bundle SavedInstanceState) {


    super.onCreate(SavedInstanceState);
    setContentView(R.layout.personalmessageview);

    headtitle= getIntent().getExtras().getString("head");

    setTitle(headtitle);


     personalresults = getIntent().getExtras().getStringArrayList("personalres");
     personalresultswithtime = getIntent().getExtras().getStringArrayList("personalrestime"); 



    // setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,personalresults));

     ListView list = (ListView)findViewById(R.id.listview_personal);    
    // ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, personalresults);
     list.setAdapter(adapter);
     registerForContextMenu(list);


     list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 
            public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {


                 String time = personalresultswithtime.get(pos).toString();


                    Show_Alert_box(v.getContext(),"Please select action.",time,pos);


                return true;
            }
        });

public void Show_Alert_box(Context context,String message,String time,int position)                   final String timestamp = time;

              final int pos = position;

             final AlertDialog alertDialog = new  AlertDialog.Builder(context).create();
                alertDialog.setTitle(getString(R.string.app_name));
                alertDialog.setButton("Delete", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {

                        try
                        {
                            db = databaseHelper.getWritableDatabase();


                            db.delete("messages","timestamp" + "=?", new String[] { timestamp }); 

                            Log.d("DB"," delete! ");

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


                                    adapter.remove(adapter.getItem(pos)); //not working t all! why ?

                            list.notify();
                            list.invalidate();


                            personalresults.remove(pos);
                            personalresultswithtime.remove(pos);

                            adapter.notifyDataSetChanged();

                            db.close();




                        }
                        catch(Exception e)
                        {

                        }
                } }); 
                alertDialog.setButton2("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        alertDialog.dismiss();
                } }); 

                alertDialog.setMessage(message);
                alertDialog.show();

}

10 个答案:

答案 0 :(得分:6)

尝试

adapter.remove(adapter.getItem(pos));
notifyDataSetChanged();

答案 1 :(得分:6)

你可能已经解决了,但万一其他人有同样的问题,这是我的解决方案:

ArrayAdapter<String> myAdapter = (ArrayAdapter<String>)getListView().getAdapter();
myAdapter.remove(myAdapter.getItem(info.position));
myAdapter.notifyDataSetChanged();

问题是您没有列表的适配器。

答案 2 :(得分:3)

数据更改后列表视图无效,ListActivity内部在更改数据时使用以下行

 getListView().invalidate();

答案 3 :(得分:2)

make函数将您的适配器与Listview绑定,并在删除完成时再次调用该函数,以便再次填充Listview并获得刷新列表。
你在使用数据库吗?

答案 4 :(得分:2)

listview.invalidateViews();

之后尝试使用adapter.remove(adapter.getItem(pos));

答案 5 :(得分:2)

你必须更新你的数据库中的更改然后你更新你的arraylist(有一个requery或类似的东西,因为它已被弃用) 那么你必须调用adapter.notifyDataSetChanged();

答案 6 :(得分:0)

    public class Listview_cls extends Activity implements OnItemClickListener{
            ListView lv;
            String items[]= {"Kolkata","Delhi","Mumbai","Pune"};
            ArrayAdapter adp;
            public void onCreate(Bundle savedInstanceState)
            {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            arr = new ArrayList();
            lv = (ListView) findViewById (R.id.ListView01);
            for(int i=0; i<items.length; i++)
            {
                arr.add(items[i]);
            }
        adp = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arr);
        lv.setAdapter(adp);
        lv.setOnItemClickListener(this);
   } 


   public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) 
   {    

    adp.remove(adp.getItem(arg2));
        adp.notifyDataSetChanged();

    }
}

答案 7 :(得分:0)

@Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();      
        int position = info.position;
        favouriteReportList d;
        d= array_list.get(position);
        switch (item.getItemId()) {
            case R.id.connect:              
                return true;
            case R.id.mark:
                return true;
            case R.id.delete:        
                myFavReport = new DBhandlerReport(this);
                myFavReport.deleteMyFavReport(d.getReportName(), d.getPathName(), myUrl);
                myFavReport.close();
// arrarAdapter is an object of your class.   
                arrayAdapter.remove(arrayAdapter.getItem(info.position));
                arrayAdapter.notifyDataSetChanged();
                return true;                
            default:
                return super.onContextItemSelected(item);
        }
    }

答案 8 :(得分:0)

您的问题是,您正在创建一个新的适配器对象,并在该对象上调用.notifyDataSetChanged(),而不是在附加到列表视图的适配器上调用。

答案 9 :(得分:-2)

试试这个

Intent intent= getIntent();
            finish();
            startActivity(intent);