通过上下文菜单使用更新和删除选项的列表视图
Posted
技术标签:
【中文标题】通过上下文菜单使用更新和删除选项的列表视图【英文标题】:list view with update and delete option via contextmenu 【发布时间】:2013-10-11 02:11:29 【问题描述】:我这里一头雾水,我想在deleting any item
之后刷新listview
。我只是使用intent
和call the same activity
,但它不起作用。那么请提出任何其他技巧或想法。 my sample code
如下:
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);
【问题讨论】:
这将有助于vogella.com/articles/androidSQLite/article.html 只关注“4.教程:使用SQLite”,不要关注“5.内容提供者和共享数据”部分。 @M-WaJeEh: 谢谢你先生..我会试试看。:-) 【参考方案1】:您可以尝试 onResume() 方法并通知您列表。
@Override
public void onResume()
//set your adapter or referesh
super.onResume();
【讨论】:
是的,兄弟,我认为这看起来不错..我会尝试 :) thnx 建议@Elesh【参考方案2】:当您对列表视图中的项目完成删除操作时,您只需调用适配器的adapter.notifydatasetchanged()
。
【讨论】:
以上是关于通过上下文菜单使用更新和删除选项的列表视图的主要内容,如果未能解决你的问题,请参考以下文章