Android Dialog如何从Custom适配器Dialog适配器获取文本

时间:2013-12-27 02:43:09

标签: java android android-alertdialog

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

1 个答案:

答案 0 :(得分:2)

  

我通过将其添加到我的代码来解决这个问题。

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

我的适配器变量由ArrayAdapter<String> adapter = new ArrayAdapter<String>通过添加

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

哪个变量对应于对话框中项目的ID位置,并使用adapter.getItem()它可以调用项目的字符串值,因此我将其显示在我的按钮上。