2012-03-20 58 views
1

我正在尝试在Android中创建listmanager应用程序。我做了一个ListView和一个ArrayList,我可以用一个按钮和一个EditText添加项目。然后我用xml文件创建了一个上下文菜单,我可以删除listitems,添加新的,编辑它们等。问题是我不知道如何删除我的contextmenu的onContextItemSelected()方法中的项目。更确切地说,我不知道如何引用此方法中的列表项,与所有其他操作一样,但我希望当删除成功时可以执行它们。 (对不起,我的英语不好,它已经有一段时间,因为我正确地使用它:))。在Android中使用contextmenu从ListView中删除项目?

这里是我的main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 

<ListView 
    android:id="@android:id/list" 
    android:layout_width="match_parent" 
    android:layout_height="fill_parent" 
    android:layout_above="@+id/linear" 
    android:layout_weight="1" 
    android:background="#000000" 
    android:drawSelectorOnTop="true" /> 

<LinearLayout 
    android:id="@+id/linear" 
    android:layout_alignParentBottom="true" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:orientation="horizontal"> 
    <EditText 
     android:id="@+id/entry" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:layout_weight="1"/> 
    <Button 
     android:text="Hozzáad" 
     android:id="@+id/bHozzaad" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 
</LinearLayout> 
</RelativeLayout> 

这里是我的contextmenu.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:id="@+id/editItem" 
     android:title="edit"/> 

<item android:id="@+id/markItem" 
     android:title="mark" /> 

<item android:id="@+id/deleteItem" 
     android:title="delete" /> 

<item android:id="@+id/permItem" 
     android:title="permanently delete" /> 

<item android:id="@+id/copyItem" 
     android:title="copy" /> 

<item android:id="@+id/moveItem" 
     android:title="move"/> 
</menu> 

这里是我主要的Java:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    final ArrayList<String> listaelemek = new ArrayList<String>(); 

    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, listaelemek); 
    setListAdapter(adapter); 
    adapter.add("first item"); 

    setContentView(R.layout.main); 

    ListView lv = getListView(); 
    lv.setTextFilterEnabled(true); 
    registerForContextMenu(lv); 

    lv.setOnItemClickListener(new OnItemClickListener() { 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {} 
    }); 

    final Button hozzaad = (Button) findViewById(R.id.bHozzaad); 
    hozzaad.setOnClickListener(new View.OnClickListener() 
     { 
     public void onClick(View v) 
      { 
      EditText entry = (EditText) findViewById(R.id.entry); 
      listaelemek.add(entry.getText().toString()); 
      adapter.notifyDataSetChanged(); 
      } 
     } 
); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
MenuInflater inflater = getMenuInflater(); 
inflater.inflate(R.menu.menu, menu); 
return true; 
} 

@Override 
public void onCreateContextMenu(android.view.ContextMenu menu, View v, ContextMenuInfo menuInfo)  
{ 
super.onCreateContextMenu(menu, v, menuInfo); 
MenuInflater inflater = getMenuInflater(); 
inflater.inflate(R.menu.contextmenu, menu); 
} 

@Override 
public boolean onContextItemSelected(MenuItem item) { 
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); 
switch (item.getItemId()) { 
case R.id.editItem: 
//do something 
return true; 
case R.id.markItem: 
//do something 
return true; 
case R.id.deleteItem: 
//here's my question mark :) 
return true; 
case R.id.permItem: 
//do something 
return true; 
case R.id.copyItem: 
//do something 
return true; 
case R.id.moveItem: 
//do something 
return true; 
default: 
return super.onContextItemSelected(item); 
} 
} 

希望你能帮助我:)

回答

10

此代码应该做的工作:

adapter.remove(adapter.getItem(info.position)); 
+0

谢谢!但现在有一个错误:“方法remove(MenuItem)未定义类型Menu”。也许我应该把适配器声明放在public void onCreate(...)方法之外,所以onContextItemSelected()可以访问它吗? – user1281948 2012-03-20 22:41:20

+0

@ user1281948,当然,适配器应该是可见的,最好使它成为一个类变量。 – Egor 2012-03-21 05:04:42

+0

谢谢@Egor! :) – user1281948 2012-03-22 17:47:50

1

我上面适配器代码在我的计划中写道,当我运行使用上下文菜单当时力节目&删除资料关闭窗口进来

这是我的代码

公共类ShowContextMenu扩展ListActivity {

ArrayAdapter<String> adapter; 



/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    final String []s=(getResources().getStringArray(R.array.names)); 

    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,s); 

    setListAdapter(adapter); 


    registerForContextMenu(getListView()); 
} 

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { 
    super.onCreateContextMenu(menu, v, menuInfo); 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.context_menu, menu); 
} 






public boolean onContextItemSelected(MenuItem item) { 
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); 

    String[] names = getResources().getStringArray(R.array.names); 

    switch(item.getItemId()) { 

    case R.id.edit: 
     Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.edit) + 
       " context menu option for " + names[(int)info.id],       
       Toast.LENGTH_SHORT).show(); 



     return true; 

    case R.id.save: 
     Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.save) + 
       " context menu option for " + names[(int)info.id], 
       Toast.LENGTH_SHORT).show(); 
     return true; 

    case R.id.delete: 

//插入代码在这里

adapter.remove(adapter.getItem(info.position)); 


     Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.delete) + 
       " context menu option for " + names[(int)info.id], 
       Toast.LENGTH_SHORT).show(); 

     return true; 

    case R.id.view: 

     Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.view) + 
       " context menu option for " + names[(int)info.id], 
       Toast.LENGTH_SHORT).show(); 
     return true; 
    default: 
     return super.onContextItemSelected(item); 
    } 
} 

}

+0

给出错误...索引超出范围。 – 2013-09-17 09:24:39

相关问题