如何获取 AlertDialog 中自定义列表视图的单击项的值?
Posted
技术标签:
【中文标题】如何获取 AlertDialog 中自定义列表视图的单击项的值?【英文标题】:How to get the value of clicked item of a custom listview inside the AlertDialog? 【发布时间】:2018-12-09 11:56:34 【问题描述】:我在 AlertDialog 中创建了一个自定义列表视图,并通过解析 list 来设置数据。我需要获取被点击的行的单个对象的点击值。但是 listView.setOnItemClickListener 不起作用。我试图解决这个问题,但找不到方法。请帮我解决这个问题。
谢谢..
这是我的代码
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();
);
这是我的适配器
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
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/match_result"
android:layout_
android:layout_
android:orientation="vertical">
<LinearLayout
android:layout_
android:layout_
android:padding="@dimen/activity_margin_10dp"
android:weightSum="3">
<TextView
android:id="@+id/customer_name"
android:layout_
android:layout_
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_
android:layout_
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_
android:layout_
android:layout_marginTop="@dimen/activity_margin_5dp"
android:background="@color/colorGrey" />
</LinearLayout>
【问题讨论】:
尝试删除:android:inputType="text"
用于 custom_row.xml 中的 TextView。希望有帮助!
是的,这是我的错误。实际上这有效。太感谢了。非常感谢您的回复。再次感谢。
【参考方案1】:
使用您的customers_data
列表获取点击值。
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();
);
【讨论】:
任何有凌空抽射的经验?需要帮助的朋友***.com/questions/50758847/… listView.setOnItemClickListener 不起作用,这就是问题所在。非常感谢您的回复。【参考方案2】:如果您可以发布非常有用的项目或至少是整个活动,您我不确定为什么您的代码不起作用,所以这就是您可以轻松在警报框中显示列表的方法。
// 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://***.com/a/43532478/9638167
【讨论】:
我知道这行得通。但我的情况与此不同。我需要在我的警报对话框中创建一个自定义适配器。不是这样简单的。感谢您的回复。但我可以保证这条线适用于不想在警报对话框中创建适配器的人。 给我活动的代码,我会让它工作。【参考方案3】:不要使用ListView,阅读这个:Should we use RecyclerView to replace ListView?和google上的其他资料,RecyclerView是要走的路
我认为你需要的是:
lv.setOnItemClickListener(new OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
//element selected is yourList.get(position);
);
【讨论】:
【参考方案4】:其实,这就是这个问题的答案。我已将 android: inputType="text"
添加到代码中。这就是问题所在。我删除了它,然后它完美地工作。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/match_result"
android:layout_
android:layout_
android:orientation="vertical">
<LinearLayout
android:layout_
android:layout_
android:padding="@dimen/activity_margin_10dp"
android:weightSum="3">
<TextView
android:id="@+id/customer_name"
android:layout_
android:layout_
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_
android:layout_
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_
android:layout_
android:layout_marginTop="@dimen/activity_margin_5dp"
android:background="@color/colorGrey" />
</LinearLayout>
答案由“I_A_Mok”给出,所有功劳都应该归他所有。
【讨论】:
【参考方案5】:请从您的 xml 中删除 android:inputType="text"
。这将解决您的问题。谢谢。
【讨论】:
以上是关于如何获取 AlertDialog 中自定义列表视图的单击项的值?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Xamarin Android 的 AlertDialog 中创建带有自定义适配器的列表视图