gpt4 book ai didi

java - Android Dialog如何从自定义适配器Dialog适配器获取文本

转载 作者:行者123 更新时间:2023-12-02 06:23:30 24 4
gpt4 key购买 nike

this is my code in my adapter.. the int which indicates the id of the item, but how can i get the text value of the item? i want to display it to my button after selecting the item from the dialog..  

builder.setSingleChoiceItems(adapter,-1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch(which) {
case 0:
}
dialog.dismiss();
}
});

有办法使用这个代码吗?但根据我的研究, onItemSelected 只能实现到 listview 和 spinner ..但就我而言,如何在自定义警报对话框适配器中使用它?

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String label = parent.getItemAtPosition(position).toString();

// Showing selected spinner item
Toast.makeText(parent.getContext(), "You selected: " + label, Toast.LENGTH_LONG).show();
}

最佳答案

I solve this by adding this to my code.

 builder.setSingleChoiceItems(adapter,-1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String itemName=adapter.getItem(which);
button.setText(itemName);
dialog.dismiss();
}
});

my adapter variable is declared by ArrayAdapter<String> adapter = new ArrayAdapter<String> by adding

String itemName=adapter.getItem(which);
button.setText(itemName);

The which variable corresponds to the ID position of the item within the dialog and using adapter.getItem() it can call the string value of the item so i display it to my button.

关于java - Android Dialog如何从自定义适配器Dialog适配器获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20794016/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com