35

我正在开发一个 android 应用程序。我将有一个列表视图,并且我设置了一个上下文菜单以在长按列表视图项目时出现。我如何从选定的列表视图项目中获取项目(比如来自列表视图文本视图的文本)之后选择了上下文菜单中的一个动作,以便我可以处理它?这是一些代码:

protected void onCreate(Bundle savedInstanceState) {
    -------
    lv1 = (ListView) findViewById(R.id.listings);

    registerForContextMenu(lv1);
    lv1.setOnItemClickListener(this);

}

和 onCreateContextMenu:

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

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
            .getMenuInfo();
    switch (item.getItemId()) {
    case R.id.watch:
        String name = "";
        return true;
    case R.id.buy:
        return true;
    default:
        return super.onContextItemSelected(item);
    }
}

我想从列表项中的文本视图中获取文本。我该如何实现?

4

7 回答 7

70

您应该LISTVIEW为上下文菜单注册。

这是来源。

对于onCreate()

 registerForContextMenu(lv);

并在长按期间访问所选项目:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
   if (v.getId() == R.id.lv) {
       ListView lv = (ListView) v;
       AdapterView.AdapterContextMenuInfo acmi = (AdapterContextMenuInfo) menuInfo;
       YourObject obj = (YourObject) lv.getItemAtPosition(acmi.position);

       menu.add("One");
       menu.add("Two");
       menu.add("Three");
       menu.add(obj.name);
   }
}
于 2013-09-05T09:24:42.130 回答
36

要从选定的 ListView 项目中获取项目,请参阅 ContextMenuInfo 对象(请参阅下面最后实现的方法)。完整解决方案如下:

1) 在 ListActivity 类中为上下文菜单注册 ListView

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // ...
    getListView().setAdapter(mAdapter);
    registerForContextMenu(getListView());
}

1a)如果您的列表中有复杂的视图,您可能需要在适配器类中启用长按每个列表视图

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (view == null) {
        RelativeLayout layout = (RelativeLayout) LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);
        itemLayout = layout;
        itemLayout.setLongClickable(true);
    }
    // ...
    return view;
}

2)在ListActivity类中实现onCreateContextMenu()和onContextItemSelected()

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
    String title = ((MyItem) mAdapter.getItem(info.position)).getTitle();
    menu.setHeaderTitle(title);

    menu.add(Menu.NONE, MENU_CONTEXT_DELETE_ID, Menu.NONE, DELETE_TEXT);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case MENU_CONTEXT_DELETE_ID:
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        Log.d(TAG, "removing item pos=" + info.position);
        mAdapter.remove(info.position);
        return true;
    default:
        return super.onContextItemSelected(item);
    }
}
于 2014-02-22T19:53:27.040 回答
2

1)首先我们使用

 @Override
 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.add("View Selected Text");
}

2) list--is ref 如果 ListView

registerForContextMenu(list);

3)

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);

    if(item.getTitle().equals("View Selected Text"))
    {
        AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
        Contact c=array.get(menuInfo.position);
        Toast.makeText(List.this, "Selected String is :-"+c.toString(), Toast.LENGTH_SHORT).show();
    }
}
于 2014-02-02T06:42:23.120 回答
1

首先使用 id 获取列表

Context context = getApplicationContext();
ComponentName component = new ComponentName(context.getPackageName(), TestReplaceHomeAppActivity.class.getName());

String packname = context.getPackageName();

Intent LaunchIntent = getActivity().getPackageManager().getLaunchIntentForPackage(packageName);
if(LaunchIntent != null){
    startActivity(LaunchIntent);
}
else {
    Toast.makeText(getActivity().getBaseContext(),"APPLICATION IN NOT AVAILABEL", Toast.LENGTH_SHORT).show();
}
于 2013-09-05T10:11:27.700 回答
0

使用这些方法,onCreateContextMenu 和 onContextItemSelected\

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    if (v.getId() == R.id.listview) {
        menu.setHeaderTitle("Delete");
        menu.add(Menu.NONE, 0, 0, "Delete from list");
    }
}

/**
 * Responding to context menu selected option
 * */

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
            .getMenuInfo();
    int menuItemIndex = item.getItemId();
    // check for selected option
    if (menuItemIndex == 0) {
        // user selected delete
        // delete the listrow
        ..(in your onitemclicklistener there is a parameter called as'postition' use this position and use some method to delete the data corresponding to the position value )
        // reloading same activity again
        Intent intent = getIntent();
        finish();
        startActivity(intent);
    }

    return true;
}
于 2013-09-05T09:23:31.113 回答
0

使用您使用的列表视图将其写入您的 longPressListener 中:

 ListView list = (ListView) findViewById(android.R.id.list);
 registerForContextMenu(list);

这是方法:

  @Override
   public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);

    AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
    Adapter adapter = getListAdapter();
    Object item = adapter.getItem(info.position);



    menu.setHeaderTitle("Choose");
    menu.add(0, v.getId(), 0, "Delete");

}


@Override
public boolean onContextItemSelected(MenuItem item) {

    if (item.getTitle() == "Delete") {
        deleteContact(item.getItemId());
    } else if (...) {
        // code
    } else {
        return false;
    }
    return true;

}

 public void deleteContact(int id){

// your code what to do for the clicked item
  }
于 2013-09-05T09:25:06.340 回答
0

上述答案非常准确,并且针对所提供的案例。话虽这么说,我被带到这里是用convertView我的,我listview正在为那些也被带到这里的人回答这个案子。

如果您LISTVIEW正在使用convertView和扩展单独的布局(例如list_MyItem.xml),请直接将其修改list_MyItem.xml为:

android:longClickable="true"

例如,如果列表视图正在填充按钮,请按如下方式修改按钮:

<Button
    android:id="@+id/myButton"
    .
    .
    .
    android:longClickable="true"
/>
于 2018-08-05T23:07:22.590 回答