gpt4 book ai didi

android - 如何在 Listview 中获取上下文菜单以在 android 中删除和显示记录?

转载 作者:行者123 更新时间:2023-11-30 03:51:08 25 4
gpt4 key购买 nike

我已经在我的“ListActivity.java”中使用了上下文菜单,但是上下文菜单没有打开和弹出。我在上下文菜单中添加了两个选项,第一个用于显示记录,另一个用于删除记录。当我长按 ListView 记录时,上下文菜单打开,我选择“显示”,然后特定记录显示在其他 Activity 上。在这个 Activity 中,我创建表格。在此表单字段中调整记录。那我该怎么做呢???

**My ListActivity.java is here:**

public class ListActivity extends Activity {
ListView lv;
ArrayList<String> items = new ArrayList<String>();
ArrayList<String> items_id = new ArrayList<String>();
dbhelper dh;
SQLiteDatabase db;
int index_id;
Button btn;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
Button btn;
btn = (Button)findViewById(R.id.newProjectlist);
btn.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub


Intent intent= new Intent(ListActivity.this,NewProject.class);
startActivity(intent);
}
});
lv = (ListView)findViewById(R.id.projectListView);
setUpList();
dh = new dbhelper(this);

Cursor c = getAllData();
showAllData(c);
registerForContextMenu(lv);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);

if(v.getId() == R.id.projectList){
menu.setHeaderIcon(R.drawable.ic_launcher);
menu.setHeaderTitle("Record List");
menu.add(0,1,menu.NONE,"Delete Record");
menu.add(0,2,menu.NONE,"Show Record");
}
}

@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
int itemId = item.getItemId();
if(itemId == 1)
{
AdapterView.AdapterContextMenuInfo menuinfo = (AdapterContextMenuInfo) item.getMenuInfo();
int poistion = menuinfo.position;
String id_string = items_id.get(poistion);
long id = Long.valueOf(id_string);

//menuinfo = (AdapterContextMenuInfo) item.getMenuInfo();
//index_id = menuinfo.position;
deleteUserData(index_id);
items_id.remove(poistion);
}
if(itemId == 2){
AdapterView.AdapterContextMenuInfo menuinfo;
menuinfo = (AdapterContextMenuInfo) item.getMenuInfo();
index_id = menuinfo.position;
editUserData(index_id);
}
return super.onContextItemSelected(item);
}

private void editUserData(int indexid) {
// TODO Auto-generated method stub
String user_id = items_id.get(indexid);
Intent intEdit = new Intent(ListActivity.this, MainActivity.class);
intEdit.putExtra("EditID", user_id);
startActivityForResult(intEdit, 1);
}

private void deleteUserData(int indexid) {
// TODO Auto-generated method stub
// String user_id = items_id.get(indexid);
//Toast.makeText(getApplicationContext(), "id " + user_id, 1).show();
db = dh.getWritableDatabase();
db.delete("timer","_ID" + "=" + indexid, null);
finish();
// Toast.makeText(getApplicationContext(), "Record deleted successfully", 1).show();
}

private void showAllData(Cursor c) {
// TODO Auto-generated method stub
while(c.moveToNext())
{
int id = c.getInt(0);
String name = c.getString(1);
String timerpoint1 = c.getString(2);
String timerpoint2 = c.getString(3);
String timerpoint3 = c.getString(4);
String servicetime1 = c.getString(5);
String servicetime2 = c.getString(6);
items.add("" + name);
items_id.add(""+id);
}
setUpList();
}

private Cursor getAllData() {
// TODO Auto-generated method stub
db = dh.getReadableDatabase();
//String sortorder = dh.Name + "DESC";
// String query ="SELECT DISTINCT name FROM timer";
// Cursor c = db.rawQuery(query, null);
// if (c != null) {
// c.moveToFirst();
// }
// String[] cols = new String[] {"name"};
Cursor c = db.query(true, "timer",null,null,null,null,null,null,null);
//Cursor cur = db.query(Distinct,"timer", null, null, null, null, null, null, null);
startManagingCursor(c);
// Cursor c=db.rawQuery("select distinct Name from timer;",null);
return c;
}
private void setUpList() {
// TODO Auto-generated method stub
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,items));
}

}

最佳答案

您应该能够删除一行:

if(itemId == 1)
{
AdapterView.AdapterContextMenuInfo menuinfo = (AdapterContextMenuInfo) item.getMenuInfo();
deleteUserData(menuInfo.id);
items_id.remove(menuInfo.position);
}

如果您使用 CursorAdapter 将数据库信息绑定(bind)到 ListView,则无需调用 remove(),它会通过内置的 DataObserver 自动完成。

关于android - 如何在 Listview 中获取上下文菜单以在 android 中删除和显示记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14133032/

25 4 0