我正在开发一个android应用程序。我将有一个listview,并且我设置了一个上下文菜单,当listview项目被长按时出现。如何从listview项目中选择项目(比如listview中的文本) textview)选择了contextmenu的动作后我可以处理它吗? 这是一些代码:
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);
}
}
我想从列表项中的textview获取文本。我如何实现这一目标?
答案 0 :(得分:68)
您应该为上下文菜单注册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);
}
}
答案 1 :(得分:32)
要从ListView项目中选择项目,请参阅ContextMenuInfo对象(请参阅下面的上一个实现方法)。完整解决方案如下:
1)为ListActivity类中的上下文菜单注册ListView
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ...
getListView().setAdapter(mAdapter);
registerForContextMenu(getListView());
}
1a)如果你的列表中有复杂的View,你可能需要长时间点击Adapter类中的每个列表视图
@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);
}
}
答案 2 :(得分:2)
1)首先我们使用
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add("View Selected Text");
}
2) list - 如果ListView
,则为refregisterForContextMenu(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();
}
}
答案 3 :(得分: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();
}
答案 4 :(得分: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;
}
答案 5 :(得分: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
}
答案 6 :(得分:0)
以上答案非常准确,很明确。话虽如此,我是带着convertView
的{{1}}来到这里的,并为那些也被带到这里的人解答。
如果您的listview
使用LISTVIEW
并夸大了单独的布局(例如convertView
),请直接将list_MyItem.xml
修改为:
list_MyItem.xml
例如,如果用按钮填充列表视图,则将按钮修改为:
android:longClickable="true"