0

私は現在、拡張する主な活動をしていますListActivity

ListAdapterデータベースからのエントリでアクティビティを膨らませています。TextViewsコンテキストメニューとして動作する膨張したエントリがありますが、クリックすると、選択した ListView 内のいずれかから値を取得できるようにしたいと考えていますOnListItemClick listener

問題は、長押しでコンテキスト メニューがアクティブになったときにOnItemClickListener登録されずListView、通常の短いクリックと同じように値を取得できないことです。onListItemClickViewクリックすると が表示されますが、 は表示さonContextItemSelectedれず、 のみが表示されますMenuItem

public class EntryActivity extends ListActivity
{ 
   String currentItemName;

   @Override
   protected void onListItemClick(ListView l, View v, int position, long id) 
   {
       //The Value i need is this: currentItemName, 
       //and i need it to register when a list item is clicked
       TextView curName =(TextView)v.findViewById(R.id.txtName) ;
       currentItemName = curName.getText().toString(); 
   }

   //I need to use the String obtained from the click in the context menu
   //to call a method, but a long click makes the onContextItemSelected 
   //be called, so onItemClickListener is never called, and i cant get the string
   @Override
   public boolean onContextItemSelected(MenuItem item ) 
   {
      switch(item.getItemId())
      {
         case ADD_ONE: 
            methodCalled(currentItemName);
            break;
      }
   }

   //I am inflating the list with a DataAdapter and a ListAdapter
   private void refresh()
   {
      //create an array the size of the cursor(one item per row)
      InventoryItem[] items = new InventoryItem[c.getCount()];

      //create and set the DataAdaptor with the array of inventory items, to the 
      //inventoryList layout
      da = new DataAdapter(this,  R.layout.inventorylist, items);
      setListAdapter(da);      
   }
}

クリックしたビューを contextMenu リスナーで使用できるようにする方法はありますか? それとも、これについて間違った方法で行っていますか?

4

2 に答える 2

0

ユーザーがリストビュー項目を長押しした後、コンテキストメニューを呼び出して表示することができますListView.setOnItemLongClickListener(...)AlertDialog.Builder.setItems(CharSequence[] items, DialogInterface.OnClickListener listener)

于 2013-05-29T01:53:22.207 に答える