ListView ClickListener for Dialog子项

时间:2015-04-16 12:43:11

标签: android listview android-listview

我有一个带有ListView的对话框。在listview项目中有两个textview。我想在listview的点击监听器的两个文本视图中获取文本。是否可能?我尝试了下面的代码片段,但它仅提供第一个textview项目。我想在点击任何listview项目时关闭对话框。

代码:

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

                        Object item = lv.getAdapter().getItem(position);

                        Toast.makeText(getApplicationContext(),
                                item.toString(), Toast.LENGTH_SHORT).show();
                        changeIngredient.dismiss();

                        ParameterName.setText(item.toString());


                    }
                });

这是我的适配器类:

public class CustomListAdapterDialog extends ArrayAdapter<String> {

private final Activity context;
private final String[] Name;
private final Double[] Price;
private final Typeface tf,tfb;
Dialog changeIngredient;

public CustomListAdapterDialog(Activity context, String[] NameArray,Double[] PriceArray, Typeface tf, Typeface tfb, Dialog changeIngredient) 
{
    super(context, R.layout.dialog_listitem,NameArray);

    this.context=context;
    this.Name=NameArray;
    this.Price=PriceArray;
    this.tf=tf;
    this.tfb=tfb;
    this.changeIngredient=changeIngredient;
}

public View getView(int position,View view,ViewGroup parent) 
{
    LayoutInflater inflater=context.getLayoutInflater();
    View rowView=inflater.inflate(R.layout.dialog_listitem, null,true);


    final TextView txtName = (TextView) rowView.findViewById(R.id.txt_IngredientName);
    final TextView txtPrice = (TextView) rowView.findViewById(R.id.txt_IngredientPrice);


    txtName.setText(Name[position]);
    txtName.setTypeface(tf);
    txtPrice.setTypeface(tf);
    String priceValue = String.valueOf(Price[position]);
    txtPrice.setText("$"+priceValue);

    /*txtName.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) 
        {
            Toast.makeText(context, txtName.getText(), Toast.LENGTH_SHORT).show();
            Toast.makeText(context, txtPrice.getText(), Toast.LENGTH_SHORT).show();
            changeIngredient.dismiss();
        }
    });
*/
    return rowView;

};

}

1 个答案:

答案 0 :(得分:0)

使用view方法的onItemClick第二个参数来访问两个TextView值:

 public void onItemClick(AdapterView<?> parent, View view,int position, long id) 
        {
            TextView txtViewOne = (TextView)view.findViewById(R.id.textview_one);
            TextView txtViewTwo = (TextView)view.findViewById(R.id.textview_two);
            String strTxtOne=txtViewOne.getText().toString();
         }});