我正在使用安卓 v4。我创建了一个显示联系人的列表视图
public class MainActivity extends Activity {
public ListView mainListView ;
public ArrayAdapter<String> listAdapter ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find the ListView resource.
mainListView = (ListView) findViewById( R.id.mainListView );
String[] contacts = new String[] { "Adam", "James", "Alex", "Tom",
"Horton", "Jim","Einstein","Thomson","Dick","Mascow"};
ArrayList<String> contactsList = new ArrayList<String>();
contactsList.addAll( Arrays.asList(contacts) );
listAdapter = new ArrayAdapter<String>(this, R.layout.rowview, contactsList);
listAdapter.add( "Cameroon" );
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter( listAdapter );
registerForContextMenu(mainListView);
}
在长按列表项时显示 ContextMenu。而Context_menu.xml如下,
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/Edit"
android:title="@string/Edit">
</item>
<item
android:id="@+id/Delete"
android:title="@string/Delete">
</item>
</menu>
当单击上下文菜单中的“删除”时,我正在获取 ListItem 值,从 List 中删除 ListItem。我的上下文菜单代码如下,
public boolean onContextItemSelected(MenuItem item){
AdapterContextMenuInfo info=(AdapterContextMenuInfo)item.getMenuInfo();
switch(item.getItemId())
{
case R.id.Edit:
// Edit List Item and Save
return true;
case R.id.Delete:
listAdapter.remove(listAdapter.getItem(info.position));
Toast.makeText(this, "Deleted", Toast.LENGTH_SHORT).show();
}
return true;
}
现在,我的疑问是如何在单击“编辑”按钮并保存已编辑的联系人时编辑特定的列表项。
我是 Android 新手,对处理事件一无所知。请任何人帮助!
谢谢。