【问题标题】:How to create dialog box by clicking on specific ListView items?如何通过单击特定的 ListView 项目来创建对话框?
【发布时间】:2017-10-31 21:59:46
【问题描述】:

我有一个列表视图,其中包含通过其他活动添加到其中的项目。我的目标是在特定列表视图项目被点击时,它会启动一个特定于所点击项目的对话框

这是列表视图

<?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();


    }

该列表未填充,因为它只能通过其他活动填充,而不是包含列表视图的活动。

【问题讨论】:

    标签: android string listview adapter onclicklistener


    【解决方案1】:

    使用视图模式

    Example

    在您的 getView 方法中,您可以将 onclicklistener 设置为您的视图并为每一行启动您的对话框。

    【讨论】:

      【解决方案2】:

      首先,您可以使用 RecyclerView 代替 Listview,它在内存管理和性能方面更有效。

      正如@ghost 所说,您可以使用 ViewHolder 模式并将您的活动上下文推送到您的视图持有者和适配器。您可以简单地将您的点击逻辑放在您的 viewHolder(适配器内部)中。

      【讨论】:

      • 我能得到一个例子,或者一些伪代码吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-13
      • 2023-02-16
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多