列表视图中删除项,刷新 - 机器人 [英] Listview delete item and Refresh - android

查看:138
本文介绍了列表视图中删除项,刷新 - 机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是与此类似 - Android的 - 列表视图中删除项,刷新

this question is is similar to this - Android - Listview delete item and Refresh .

我无法刷新我的适配器:

I cant refresh my adapter with :

adapter.notifyDataSetChanged();

我想:

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

但没有成功,只是一个时间(奇怪...)。

but without success, just one time (weird...).

还有另一种答案有:

Call that Activity once again Using Intent

sombody可以给我确切的code本(或适配器/鼠标)?

sombody can give me the exact code for this (or for the adapter/cursor) ?

我想这个了几个小时都没有成功。

I am trying this for a couple of hours without success.

我的全力code:

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;
            }
        });

公共无效Show_Alert_box(上下文的背景下,消息字符串,字符串时,INT位置)                   最后弦乐时间戳=时间;

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();

}

推荐答案

您可能得到它解决,但为了以防万一别人有同样的问题,这是我的解决方法:

You probably got it solved but just in case anyone else has the same Problem, here is my Solution:

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

但问题是,你没有你的列表中选择适配器。

The Problem was that you did not have the Adapter of your List.

这篇关于列表视图中删除项,刷新 - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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