2012-03-20 85 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