Android 如何通过单击特定的ListView项来创建对话框?
我有一个列表视图,其中包含通过其他活动添加的项目。我在这里的目标是,当单击特定于的listview项目时,它将启动特定于所单击项目的对话框 这是列表视图Android 如何通过单击特定的ListView项来创建对话框?,android,string,listview,adapter,onclicklistener,Android,String,Listview,Adapter,Onclicklistener,我有一个列表视图,其中包含通过其他活动添加的项目。我在这里的目标是,当单击特定于的listview项目时,它将启动特定于所单击项目的对话框 这是列表视图 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:l
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<ListView
android:id="@+id/inventoryListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dividerHeight="1dp"
android:headerDividersEnabled="true"
android:onClick="f">
</ListView>
</LinearLayout>
</LinearLayout>]
]
下面是适配器的活动
public class inventory extends AppCompatActivity {
public static ArrayList<String> items;
public static ArrayAdapter<String> adapter;
public static ListView inventoryListView;
SharedPreferences sharedPreferences;
protected void onCreate(Bundle savedInstanceSate){
super.onCreate(savedInstanceSate);
setContentView(R.layout.inventory);
inventoryListView = (ListView)findViewById(R.id.inventoryListView);
items = new ArrayList<String>();
adapter = new ArrayAdapter<String>(this,R.layout.inventory_list_view,items);
inventoryListView.setAdapter(adapter);
sharedPreferences = getSharedPreferences("my_prefs", 0);
updateInventory();
adapter.notifyDataSetChanged();
}
公共类资源清册扩展了AppCompative活动{
公共静态数组列表项;
公共静态阵列适配器;
公共静态ListView inventoryListView;
SharedReferences SharedReferences;
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceSate);
setContentView(R.layout.inventory);
inventoryListView=(ListView)findViewById(R.id.inventoryListView);
items=newarraylist();
适配器=新阵列适配器(此,R.layout.inventory\u list\u视图,项);
inventoryListView.setAdapter(适配器);
SharedReferences=GetSharedReferences(“我的首选项”,0);
updateInventory();
adapter.notifyDataSetChanged();
}
列表未填充,因为它只能通过其他活动填充,而不能通过保存listview的活动填充。使用viewholder模式
在您的
getView
方法中,您可以将onclicklistener设置为您的视图,并为每一行启动对话框。首先,您可以使用RecyclerView而不是Listview,它在内存管理和性能方面更有效
正如@ghost所说,您可以使用ViewHolder模式,将您的活动上下文推送到您的ViewHolder和适配器。您只需将您的单击逻辑放在ViewHolder(适配器内部)中即可。我可以得到一个示例或一些伪代码吗?