【问题标题】:Trying to get a selected view from onContextItemSelected试图从 onContextItemSelected 获取选定的视图
【发布时间】:2013-05-24 02:39:05
【问题描述】:

我目前有一个扩展 ListActivity 的主要活动

我正在使用带有数据库条目的ListAdapter 来夸大活动。 我将膨胀的条目作为上下文菜单运行,但我希望能够在单击选定的 ListView 时从其中一个 TextViews 中获取值,这是我通过 OnListItemClick listener 获得的。

问题是,当长按激活上下文菜单时,OnItemClickListener 没有注册,我无法像通过常规短按一样从ListView 获取值。 onListItemClick 在单击时可以看到View,但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 监听器可用?还是我以错误的方式解决这个问题?

【问题讨论】:

    标签: android listadapter dataadapter android-contextmenu


    【解决方案1】:

    您可以在用户长按列表视图项后调用ListView.setOnItemLongClickListener(...)AlertDialog.Builder.setItems(CharSequence[] items, DialogInterface.OnClickListener listener) 显示上下文菜单。

    【讨论】:

    • 感谢您的回复,但我有点困惑。我看到设置 listview onLonClick 侦听器有什么帮助,但我不知道在省略号(...)中放置什么,正如您所描述的那样。您的意思是我必须创建一个启动上下文菜单的侦听器吗?还是我实际上会创建一个 AlertDialog 来列出菜单项并充当上下文菜单?
    【解决方案2】:

    好吧,事实证明这真的非常容易完成。在我的 listItemClicklistener 中,我只需为 contextMenu 注册 ListItem,然后打开上下文菜单:

        protected void onListItemClick(ListView l, View v, int position, long id) 
    {
    
        Cursor c = db.getAllItems(); 
        c.moveToFirst(); 
    
        //get and store the row that was clicked
        currentRow = position;    
    
        //register this item for a context menu 
        registerForContextMenu(l);
        //open the context menu
        openContextMenu(l);
    
    
    }
    

    并且必须将此行添加到我的 OnCreate 方法中:

    registerForContextMenu(getListView());
    

    很好,效果很好!

    【讨论】:

      猜你喜欢
      • 2013-07-15
      • 1970-01-01
      • 1970-01-01
      • 2018-11-14
      • 2016-10-05
      • 1970-01-01
      • 1970-01-01
      • 2020-11-08
      • 2010-09-15
      相关资源
      最近更新 更多