2012-03-20 9 views
1

私はAndroidでlistmanagerアプリケーションを作ろうとしています。私はListViewを作成し、ボタンとEditTextで項目を追加できるArrayListを作成しました。それから、listitemsを削除したり、新しい項目を追加したり、編集したりするXMLファイルを持つコンテキストメニューを作成しました。問題は、コンテキストメニューのonContextItemSelected()メソッド内でアイテムを削除する方法がわかりません。もっと正確に言えば、このメソッド内のリスト項目の参照方法は他のすべての操作と同じですが、削除に成功したときにそれらを実行できることを願っています。 (私はそれを正しく使用するので、私の悪い英語のため申し訳ありませんが、それはしばらくしている:))Androidのコンテキストメニューで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)は、タイプメニューのために定義されていません"。アダプタの宣言をpublic void onCreate(...)メソッドの外に置いて、onContextItemSelected()がそれにアクセスできるようにする必要がありますか? – user1281948

+0

@ user1281948もちろん、アダプタを表示する必要があります。クラス変数にすることをお勧めします。 – Egor

+0

ありがとう@エゴル! :) – user1281948

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

...範囲外のエラーを返します。 –

関連する問題