我是新手,我一直在尝试开发联系人应用程序以及其他待办事项应用程序,但这是与我的联系人应用程序相关的问题。
插入和查看和删除功能正常工作,但唯一的问题是,无论我选择在ListView上删除哪个项目,它总是删除最顶层的项目。
这里有一些代码:
MainActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView) findViewById(R.id.listView1);
im=(ImageButton) findViewById(R.id.add);
ii=(ImageButton) findViewById(R.id.image);
list = new ArrayList<HashMap<String,String>>();//memory allocation
list = db.getvalues();
ListAdapter ad= new SimpleAdapter(MainActivity.this, list, R.layout.custom, new String[]{"id","name"}, new int[]{R.id.id,R.id.name});
lv.setAdapter(ad);
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
TextView numtask = (TextView) arg1.findViewById(R.id.name);
phnum = numtask.getText().toString();
registerForContextMenu(lv);
return false;
}
});
ii.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i =new Intent(MainActivity.this,Main1Activity.class);
startActivity(i);
}
});
}
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Select The Action");
menu.add(0, v.getId(), 1, "DELETE");
}
public boolean onContextItemSelected(MenuItem item){
String number=phnum;
if(item.getTitle()=="DELETE"){
TextView id1=(TextView) findViewById(R.id.id);
db.deleterow(id1.getText().toString());
Toast.makeText(MainActivity.this,"Task Deleted!", Toast.LENGTH_SHORT).show();
list = new ArrayList<HashMap<String,String>>();//memory allocation
list = db.getvalues();
ListAdapter ad= new SimpleAdapter(MainActivity.this, list, R.layout.custom, new String[]{"id","name"}, new int[]{R.id.id,R.id.name});
lv.setAdapter(ad);
}else{
return false;
}
return true;
}
我不打算添加从SQLiteOpenHelper类扩展的DBConnector类,因为它包含的函数因为正在填充数据库而正在删除项目,所以我不认为有这个问题。但是,如果你想让我补充一下,如果你想要我,我会这样做......
这是custom.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
这是带有DBconnector类的deleterow方法:
public void deleterow(String numid) {
// TODO Auto-generated method stub
SQLiteDatabase sb = this.getWritableDatabase();
String sq = "DELETE FROM user WHERE id="+numid+";";
sb.execSQL(sq);
}
另外,我需要建议如何在ListView中显示id号和名称。
请帮帮我们!我贪恋你们所有人!
答案 0 :(得分:2)
您正在TextView id1=(TextView) findViewById(R.id.id);
方法中调用onContextItemSelected(MenuItem item)
,因此您将返回第一项的文字,因为您尚未提供所选项View
。
问题的解决方案是AdapterContextMenuInfo
。当用于onCreateContextMenu(ContextMenu, View, ContextMenuInfo)
的上下文菜单时,它用于获取提供给AdapterView
回调的额外菜单信息。
您可以获取所选项View
,然后从TextView
获取案例中的文字。
以下是修改后的onContextItemSelected(MenuItem item)
方法。
public boolean onContextItemSelected(MenuItem item){
String number=phnum;
if(item.getTitle()=="DELETE"){
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
TextView id1=((TextView) info.targetView).findViewById(R.id.id);
db.deleterow(id1.getText().toString());
Toast.makeText(MainActivity.this,"Task Deleted!", Toast.LENGTH_SHORT).show();
list = new ArrayList<HashMap<String,String>>();//memory allocation
list = db.getvalues();
ListAdapter ad= new SimpleAdapter(MainActivity.this, list, R.layout.custom, new String[]{"id","name"}, new int[]{R.id.id,R.id.name});
lv.setAdapter(ad);
}else{
return false;
}
return true;
}