所选项目列表视图中的警报对话框

时间:2011-01-04 14:42:04

标签: android listview alertdialog

我在使用alertdialog框时遇到了一些问题。我有一个包含一些字符串的列表视图,当我单击该框时,它会显示一个警告对话框,提供预订或取消选项(它是一个出租车应用程序)。我试图得到它所以所选项目的名称显示在警告对话框中。但每当我尝试它时会出现随机字母和数字。我会发布我的代码,因为它可能会让您更容易理解:

以下代码 -

public class TaxiMain extends ListActivity {
/** Called when the activity is first created. 
 * @return */

class Taxi {
    private String taxiName;
    private String taxiAddress;

    public String getName() {
        return taxiName;
    }

    public void setName(String name) {
        taxiName = name;
    }

    public String getAddress() {
        return taxiAddress;
    }

    public void setAddress(String address) {
        taxiAddress = address;
    }

    public Taxi(String name, String address) {
        taxiName = name;
        taxiAddress = address;
    }
}

public class TaxiAdapter extends ArrayAdapter<Taxi> {
    private ArrayList<Taxi> items;
    private TaxiViewHolder taxiHolder;

    private class TaxiViewHolder {
        TextView name;
        TextView address; 
    }

    public TaxiAdapter(Context context, int tvResId, ArrayList<Taxi> items) {
        super(context, tvResId, items);
        this.items = items;
    }

    @Override
    public View getView(int pos, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.taxi_list_item, null);
            taxiHolder = new TaxiViewHolder();
            taxiHolder.name = (TextView)v.findViewById(R.id.taxi_name);
            taxiHolder.address = (TextView)v.findViewById(R.id.taxi_address);
            v.setTag(taxiHolder);
        } else taxiHolder = (TaxiViewHolder)v.getTag(); 

        Taxi taxi = items.get(pos);

        if (taxi != null) {
            taxiHolder.name.setText(taxi.getName());
            taxiHolder.address.setText(taxi.getAddress());
        }

        return v;
    }
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final String[] taxiNames = getResources().getStringArray(R.array.taxi_name_array);
    final String[] taxiAddresses = getResources().getStringArray(R.array.taxi_address_array);

    ArrayList<Taxi> taxiList = new ArrayList<Taxi>();

    for (int i = 0; i < taxiNames.length; i++) {
        taxiList.add(new Taxi(taxiNames[i], taxiAddresses[i]));
    }

    setListAdapter(new TaxiAdapter(this, R.layout.taxi_list_item, taxiList));  

    final ListView lv = getListView();
    lv.setTextFilterEnabled(true);

        lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> a, View v, final int position, long id)
        {   

            final int selectedPosition = position;
            AlertDialog.Builder adb=new AlertDialog.Builder(TaxiMain.this); 
             adb.setTitle("Taxi Booking");
             adb.setMessage("You Have Selected: "+taxiNames); 
             adb.setPositiveButton("Book", new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int id) {
                     Intent intent = new Intent(TaxiMain.this, Booking.class);
                     intent.putExtra("booking",  taxiNames[selectedPosition]);
                     intent.putExtra("address",  taxiAddresses[selectedPosition]);
                     startActivity(intent);
                 }
             });
             adb.setNegativeButton("Cancel", null); 
             adb.show();
         }
     });

你会在代码的底部看到,那条线路无法正常工作。 -

adb.setMessage("You Have Selected: "+taxiNames);

如果有人可以帮助了解为什么这不会显示会有很大的帮助。

由于

2 个答案:

答案 0 :(得分:2)

taxiNames是一个数组,而不是一个字符串。你应该试试这个:

adb.setMessage("You Have Selected: "+taxiNames[selectedPosition]); 

答案 1 :(得分:1)

当您输入+taxiNames时,您正在执行taxiNames.toString()taxiNames是一个包含许多项的数组。您只需将其更改为+ taxiNames[position]即可。或者,为了使其与其他Taxi对象保持一致,您还可以使用+ taxiList.get(position).getName()

编辑:出于好奇,你为什么要为final int设置另一个selectedPosition?您已经在方法调用中传入了final int position