android - 通过上下文菜单提供带有更新和删除选项的 ListView

标签 android sqlite listview android-contextmenu

我在这里很困惑,我想在删除任何项目后刷新listview。我只是使用 Intent 调用相同的 Activity ,但它无法工作。任何其他技巧或想法请提出。 我的示例代码如下:

update_page.java

 protected Object adapter;

     @Override
    public void onCreate(Bundle savedInstanceState)
        {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.update_list);
            final ProgressDialog myPd_ring=ProgressDialog.show(update_page.this, "Please wait", "Login please wait..", true);
            myPd_ring.setCancelable(true);
            myPd_ring.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            new Thread() {

                public void run() {

                try{

                sleep(1000);

                } catch (Exception e) {

                Log.e("tag", e.getMessage());

                }

                // dismiss the progress dialog

                myPd_ring.dismiss();

                }

                }.start();


            myview = (ListView)this.findViewById(R.id.list);
            ctx=this;





            database = act.getInstance().openOrCreateDatabase("contactsManager",
            SQLiteDatabase.CREATE_IF_NECESSARY, null);       
            Toast.makeText(getApplicationContext(), "Database is open", 1500).show();


       //  database=this.openOrCreateDatabase("contactsManager", SQLiteDatabase.CREATE_IF_NECESSARY,null);

            final String[] columnsone={"_id","task_name","date_time"};
            String[] columnstwo={"task_name","date_time"};
            int to[] = {R.id.lbl_task,R.id.lbl_datetime}; 
            c = database.query("contacts", columnsone, null, null, null, null, null);
            status=c.getCount();

            Toast.makeText(getApplicationContext(), "totle task :" +status, 1500).show();



             if(c!=null)
               {

                Toast.makeText(getApplicationContext(), "in courser", 1500).show();
                SimpleCursorAdapter adapter=new 
                        SimpleCursorAdapter(update_page.this,R.layout.update_listview, c, columnstwo, to);
                Toast.makeText(getApplicationContext(), "records are in list", 1500).show();

                myview.setAdapter(adapter);


                        myview.setOnItemLongClickListener(new OnItemLongClickListener() 
                        {

                            public boolean onItemLongClick(AdapterView<?> arg0,
                                    View record, int arg2, long arg3)
                            {


                                update_page.this.registerForContextMenu(record);
                                /*task= ((TextView)record.findViewById(R.id.lbl_task)).getText().toString();
                                date_time1= ((TextView)record.findViewById(R.id.lbl_datetime)).getText().toString();
                                Toast.makeText(getApplicationContext(), "task is"+task, 1500).show();
                                database = act.getInstance().openOrCreateDatabase("contactsManager",
                                        SQLiteDatabase.CREATE_IF_NECESSARY, null);    

                                String query = "SELECT _id FROM contacts WHERE task_name='"+task + "&date_time="+date_time1+"'";


                                c=database.rawQuery(query, null);
                                 c.moveToNext();

                                new_id=c.getInt(c.getColumnIndex("_id"));
                                */

                                return false;
                            }

                        });
                }
             }
            //@Override 
            public boolean onContextItemSelected(MenuItem item)
            {
                AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo)item.getMenuInfo();
                int choice = item.getItemId();
                switch(choice)
                {
                case 1:
                    Toast.makeText(getApplicationContext(), "Update Task", 2000).show();

                    /*Intent op_intent = new Intent(update_page.this,MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                    op_intent.putExtra("task_id", new_id);
                    op_intent.putExtra("task", task);
                    op_intent.putExtra("datetime", date_time1);

                    update_page.this.startActivity(op_intent);*/

                    break;

                case 2:


                    Toast.makeText(getApplicationContext(), "position :" +new_id , 2000).show();

                    String table_name = "contacts";
                    String where = "task_name='"+task+ "&date_time="+date_time1+"'";
                    String[] whereArgs = null;
                    database.delete(table_name, where, whereArgs);  
                     //over

                        c.close();
                        database.close();
                        Toast.makeText(getApplicationContext(), "Task Deleted Successfully", 1500).show();

                        update_page.this.startActivity(new Intent(update_page.this, update_page.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));

                    break;

                }

                return super.onContextItemSelected(item);
            }


            @Override
            public void onCreateContextMenu(ContextMenu menu, View v,
                    ContextMenuInfo menuInfo)
            {
                menu.add(0,1,0,"Update Task");
                menu.add(0,2,0,"Delete");

                super.onCreateContextMenu(menu, v, menuInfo);
            }


    }

最佳答案

您可以尝试 onResume() 方法并通知您列表。

@Override
public void onResume() {
    //set your adapter or referesh
    super.onResume();
}

关于android - 通过上下文菜单提供带有更新和删除选项的 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19175114/

相关文章:

java - 如何为 Android 应用程序创建标题屏幕?例如。当应用程序像 Facebook 一样加载时

python - "NOT NULL constraint failed"持久化 ORM 对象时

python - 使用 Python 比较两个 sqlite 数据库

android - 在 Android 中显示 ListView 的有效方法

android - 一个ListView支持多少种资源布局?

android - 在 ListView 上长按事件后创建菜单

android - 如何在 Android 11 上启动ActivityForResult?

android - TextInputLayout 自动对焦

android - Android PCM绘图

SQLite3 中的 PHP JSON 数组