首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在AlertDialog内部获取自定义列表视图的点击项的值?

如何在AlertDialog内部获取自定义列表视图的点击项的值?
EN

Stack Overflow用户
提问于 2018-06-30 23:10:51
回答 5查看 811关注 0票数 1

我已经在AlertDialog内创建了一个自定义列表视图,并通过解析列表来设置数据。我需要获取被单击的行的单个对象的被单击的值。但是listView.setOnItemClickListener不起作用。我试着解决这个问题,但找不到方法。请帮我解决这个问题。

感谢..

以下是我的代码

代码语言:javascript
复制
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
            builder.setTitle("Select A Customer");
            //insert array to constructor
            LayoutInflater inflater = getActivity().getLayoutInflater();
            View dialogLayout = inflater.inflate(R.layout.product_list_pop_up, null);
            final CustomerPopupAdapter testAdapter = new CustomerPopupAdapter(getContext(), customers_data);
            ListView listView = dialogLayout.findViewById(R.id.product_list_view);
            TextView cancel_btn = dialogLayout.findViewById(R.id.cancel_btn);
            TextView done_btn = dialogLayout.findViewById(R.id.done_btn);
            listView.setAdapter(testAdapter);

            builder.setView(dialogLayout);

            final AlertDialog alert = builder.create();
            alert.show();

            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    Log.d("selected Item", "ListView ");
//                    customerName = testAdapter.getItem(position);
//                    customer_name.setText((CharSequence) testAdapter.getItem(position));
                    alert.dismiss();
                }
            });
         alert.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
            WindowManager.LayoutParams lp = new WindowManager.LayoutParams();

            cancel_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    alert.dismiss();
                }
            });

            done_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    alert.dismiss();
                }
            });

这是我的适配器

代码语言:javascript
复制
public class CustomerPopupAdapter extends ArrayAdapter<Customer> {

    private List<Customer> list;

    public CustomerPopupAdapter(Context context, List<Customer> test) {
        super(context, R.layout.discount_popup_row, test);
        this.list = test;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(getContext());

        final Customer customer = list.get(position);
        View customRow = inflater.inflate(R.layout.customer_popup_row, parent, false);

        TextView customer_name = customRow.findViewById(R.id.customer_name);
        TextView customer_id = customRow.findViewById(R.id.customer_id);

        customer_id.setText(customer.getId());
        customer_name.setText(customer.getName());

        return customRow;
    }
}

这是custom_row.xml

代码语言:javascript
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/match_result"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="@dimen/activity_margin_10dp"
        android:weightSum="3">

        <TextView
            android:id="@+id/customer_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:text="Username"
            android:inputType="text"
            android:layout_weight="1"
            android:textColor="@color/colorBlack"
            android:textSize="@dimen/text_size_18sp" />


        <TextView
            android:id="@+id/customer_id"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:text="id"
            android:inputType="text"
            android:layout_weight="2"
            android:textColor="@color/colorBlack"
            android:textSize="@dimen/text_size_18sp" />
    </LinearLayout>

    <View
        android:id="@+id/bottom_border"
        android:layout_width="match_parent"
        android:layout_height="0.8dp"
        android:layout_marginTop="@dimen/activity_margin_5dp"
        android:background="@color/colorGrey" />

</LinearLayout>
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2018-08-09 12:01:33

实际上,这就是这个问题的答案。我已经在代码中添加了android: inputType="text"。这是问题所在。我去掉了它,然后它就完美地工作了。

代码语言:javascript
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/match_result"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="@dimen/activity_margin_10dp"
        android:weightSum="3">

        <TextView
            android:id="@+id/customer_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:text="Username"
            android:layout_weight="1"
            android:textColor="@color/colorBlack"
            android:textSize="@dimen/text_size_18sp" />


        <TextView
            android:id="@+id/customer_id"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:text="id"
            android:layout_weight="2"
            android:textColor="@color/colorBlack"
            android:textSize="@dimen/text_size_18sp" />
    </LinearLayout>

    <View
        android:id="@+id/bottom_border"
        android:layout_width="match_parent"
        android:layout_height="0.8dp"
        android:layout_marginTop="@dimen/activity_margin_5dp"
        android:background="@color/colorGrey" />

</LinearLayout>

答案是由"I_A_Mok“给出的,所有的荣誉都应该归功于他。

票数 0
EN

Stack Overflow用户

发布于 2018-06-30 23:17:46

使用您的customers_data列表获取单击的值。

代码语言:javascript
复制
           listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Log.d("selected Item", customers_data.get(position));
                    alert.dismiss();
                }
            });
票数 0
EN

Stack Overflow用户

发布于 2018-06-30 23:19:31

你我不确定为什么你的代码不能工作,如果你可以张贴的项目,将是非常非常有帮助或至少整个活动,所以这是如何你可以轻松地显示列表在警告框中。

代码语言:javascript
复制
// setup the alert builder
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Choose an animal");

// add a list
String[] animals = {"horse", "cow", "camel", "sheep", "goat"};
builder.setItems(animals, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        switch (which) {
            case 0: // horse
            case 1: // cow
            case 2: // camel
            case 3: // sheep
            case 4: // goat
        }
    }
});

// create and show the alert dialog
AlertDialog dialog = builder.create();
dialog.show();

检查完整的参考https://stackoverflow.com/a/43532478/9638167

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51116115

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档