- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我的应用中有两个布局文件。我也有 Activity 扩展 ListActivity。此 Activity 的每个项目看起来都考虑 item.xml 布局文件。长按项目时,我正在尝试获取上下文菜单,但我没有看到它。
在我的 Activity 中,我尝试 registerForContextMenu(getListView()) 并覆盖两个方法
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = this.getIntent().getExtras();
registerForContextMenu(getListView());
new PopulateAdapterTask().execute(ACTION_SELECT);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info;
try {
info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
} catch (ClassCastException e) {
return false;
}
long id = getListAdapter().getItemId(info.position);
Log.d(TAG, "id = " + id);
return true;
}
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</FrameLayout>
</LinearLayout>
</TabHost>
项目.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="15sp"
android:singleLine="true"
android:ellipsize="marquee"
android:scrollHorizontally = "true"
android:maxWidth="200dp"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="right"
>
<ImageButton
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="@null"
android:paddingRight="10dp"
android:paddingLeft="10dp"
/>
</LinearLayout>
</LinearLayout>
这一切都行不通。也许原因是在 LinearLayout 中?我也找到了类似的话题 Android: Context menu doesn't show up for ListView with members defined by LinearLayout?但我有更复杂的列表项。
在我的情况下如何获取上下文菜单?
此外,在我的 Activity 中,我有内部类扩展了 ArrayAdapter。在 getView 方法的这个类中,我可以在每个 View 上设置 OnCreateContextMenuListener,在出现上下文菜单之后,但我不知道如何处理项目点击。如果我试图在 onContextItemSelected 方法中执行此操作,item.getMenuInfo() 对象始终为空,我无法从中获取一些信息。
private class ChannelAdapter extends ArrayAdapter<Channel> {
private List<Channel> channels;
public ChannelAdapter(Context context, int textViewResourceId, List<Channel> objects) {
super(context, textViewResourceId, objects);
this.channels = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.station_item, null);
}
v.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
});
谢谢。希望得到您的帮助。
最佳答案
我得到了解决方案,我的 friend 帮助了我!希望这些信息对某人有所帮助。这是包含 ArrayAdapter 和复杂列表布局和上下文菜单的完整类代码。
public class ComplexListActivity extends ListActivity {
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ComplexObjectAdapter(this, R.layout.item, getComplexObjects()));
registerForContextMenu(getListView());
}
private List getComplexObjects() {
List<ComplexObject> list = new ArrayList<ComplexObject>();
list.add(new ComplexObject("1", "1", getResources().getDrawable(R.drawable.icon)));
list.add(new ComplexObject("2", "2", getResources().getDrawable(R.drawable.icon)));
list.add(new ComplexObject("3", "3", getResources().getDrawable(R.drawable.icon)));
list.add(new ComplexObject("4", "4", getResources().getDrawable(R.drawable.icon)));
list.add(new ComplexObject("5", "5", getResources().getDrawable(R.drawable.icon)));
list.add(new ComplexObject("6", "6", getResources().getDrawable(R.drawable.icon)));
list.add(new ComplexObject("7", "7", getResources().getDrawable(R.drawable.icon)));
list.add(new ComplexObject("8", "8", getResources().getDrawable(R.drawable.icon)));
list.add(new ComplexObject("9", "9", getResources().getDrawable(R.drawable.icon)));
return list;
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info;
try {
info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
} catch (ClassCastException e) {
Log.e("", "bad menuInfo", e);
return false;
}
long id = getListAdapter().getItemId(info.position);
Log.d("", "id = " + id);
Toast.makeText(this, "id = " + id, Toast.LENGTH_SHORT).show();
return true;
}
private class ComplexObjectAdapter extends ArrayAdapter<ComplexObject> implements View.OnCreateContextMenuListener {
private List<ComplexObject> objects;
public ComplexObjectAdapter(Context context, int textViewResourceId, List<ComplexObject> objects) {
super(context, textViewResourceId, objects);
this.objects = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.item, null);
}
final ComplexObject o = objects.get(position);
if (o != null) {
TextView textlInfo = (TextView) v.findViewById(R.id.info);
textlInfo.setText(o.getName());
ImageView channelIcon = (ImageView) v.findViewById(R.id.icon);
channelIcon.setAdjustViewBounds(true);
channelIcon.setMaxHeight(30);
channelIcon.setMaxWidth(30);
channelIcon.setImageDrawable(o.getLogo());
ImageButton button = (ImageButton) v.findViewById(R.id.button);
button.setImageResource(R.drawable.icon);
v.setOnCreateContextMenuListener(this);
}
return v;
}
public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {
// empty implementation
}
}
}
如果有人会找到更好的方法,请告诉我。谢谢!
关于android - 自定义 ListView 和上下文菜单。如何得到它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3972945/
我之前在论坛上发布过这个,但我想我不太清楚。我有一个侧边栏 ListView 和一个包含我的控件的主面板。我想在 oncreate() 中突出显示 listview 中的 Activity 项,因为每
这里有没有人知道我如何通过 onTap() 扩展 ListView 项(在本例中为卡片)的内容 - 以显示更多选项?您知道那里有可以提供帮助的酒吧吗? 我已经看过了,但我不知道要搜索什么?我相信你们都
我的 ListView 控件有问题。我希望我的奇数行和偶数行具有不同的颜色,但我想通过代码而不是 FXML 来实现。例如: 第一行 - 绿色 第二行 - 红色 第三行 - 绿色 第四行 - 红色 现在
我有一个 ListView 。我想让单元格背景透明。目前,我正在做以下事情: .list-cell { -fx-background-color: transparent; } 但是,细胞的颜
我想创建一个几乎无限的元素列表,但我想将列表的初始位置设置为某个特定元素。像这张图片: 其中索引 0 将是初始位置,并且此列表可能会也可能不会在两个方向上延伸很长。 我可以像这样创建我的元素: W
有没有办法在JavaFX中获取ListView的可见项?我想确定 JavaFX 应用程序中 ListView 显示的第一个可见项。 以下代码found here不适合我(仅适用于 TableView)
开发人员。 我尝试在水平方向拉伸(stretch) ListView 项目。我确实重置了 ItemContainerStyle ,并且水平对齐是拉伸(stretch)的。这是 MainPage.xam
有没有办法在JavaFX中获取ListView的可见项?我想确定 JavaFX 应用程序中 ListView 显示的第一个可见项。 以下代码found here不适合我(仅适用于 TableView)
我想问一下: 我有一个预定义顺序的数组。 var order=new Array(); order[0]="Tesco"; order[1]="Interspar"; order[2]="
我希望创建以下内容:当到达内部 Listview 的顶部或底部时,我想继续在顶部 Listview 中滚动。见动图: Gif of what I got so far 一个选项是在到达底部时将内部 L
我正在尝试在 ajax 发布后刷新 jQuery 移动 ListView ,我一直在尝试使用 .trigger("create") 来执行此操作,如下所示: Most Played
我真的不明白是什么导致了错误我检查了文档,这里有一个非常相似的例子是我的 views.py,我使用的应用程序下的 urls.py 包含,以及模板 View .py class SchoolListVi
我有这个父布局 parent_listview 的列表项具有这种布局 child_listview 的项目有这个布局
我在单击列表项时获取 listview 项 时遇到问题。我得到了 simple listview(Arrayadapter) 的 listview item,但我遇到了 custom listview
我有一个工作正常的 DropdownMenu,但我需要一个 ListView,但我无法转换它。 DropdownMenu 如下所示: // Create the List of devices t
我想实现一个可滚动(垂直)且其中包含元素的屏幕。所以我把它放在 listview.builder 上。问题是,其中一个元素是另一个水平滚动的 listview.builder。当我实现它时,horiz
帮助!我想不通! 为了帮助我学习 SwiftUI,我正在使用 ListView 制作一个简单的杂货 list 应用程序。点击列表中的每个项目时,有些按钮会变成绿色或红色。 在我向列表数组中添加太多项目
所以我知道我们可以等待 ListView 加载,然后显示它。但是,如果我们动态加载正在下载的图像,ListView 将加载但图像尚未加载,因此它们不会出现在 ListView 中。 可接受的等待 Li
我是 Android 的新手,正在努力提高编程技能。 在这里,我有自定义 ListView 适配器类,我曾在其中显示 ListView 项目,如 TextView 、图像等。 我需要做的是,我在 Li
So, what I want is to individually disable a button for the concerned item in the ListView when clic
我是一名优秀的程序员,十分优秀!