我有一个自定义的ListView有两个按钮,我当我点击我想要得到的文本标签上的ListView和现在只是弹出一个烤面包,它的任何行任一按钮。到目前为止,一切都没有工作,我不断收到在我的数组中的最后一个项目。
下面是一个截屏给你一个更好地了解我的意思
下面是我的适配子为我定制的ListView
静态最终的String []名称=
新的String [] {约翰,迈克,玛丽亚,米盖尔};
类MyArrayAdapter扩展ArrayAdapter<字符串> {
私人最终上下文的背景下;
INT其中;
公共MyArrayAdapter(上下文的背景下,的String [] pValues){
超(背景下,R.layout.main,pValues);
this.context =背景;
值= pValues;
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
LayoutInflater充气=(LayoutInflater)上下文
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.main,父母,假);
TextView中的TextView =(TextView中)rowView.findViewById(R.id.label);
ImageView的ImageView的=(ImageView的)rowView.findViewById(R.id.logo);
按钮调用=(按钮)rowView.findViewById(R.id.button1);
按钮聊天=(按钮)rowView.findViewById(R.id.button2);
textView.setText(值[位置]);
//更改图标基于名称
S =数值[立场]
其中=位置;
call.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
字符串名称=值[它]
Toast.makeText(CustomListView.this,姓名,Toast.LENGTH_SHORT).show();
}
});
返回rowView;
}
}
编辑:
字符串名称= textView.getText()的toString()。
RelativeLayout的LL =(RelativeLayout的)v.getParent();
TextView的=(的TextView)ll.findViewById(R.id.label);
Toast.makeText(CustomListView.this,姓名,
Toast.LENGTH_SHORT).show();
解决方案
很简单:
call.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
RelativeLayout的RL =(RelativeLayout的)v.getParent();
TextView的电视=(TextView中)rl.findViewById(R.id.label);
字符串文本= tv.getText()的toString()。
Toast.makeText(CustomListView.this,文字,Toast.LENGTH_SHORT).show();
}
});
I have a custom ListView with two button and I when I click either button on any row I want to get the text label on the Listview and for now just popup a toast with it. So far nothing has worked I keep getting the last item in my array.
Here is a screen shot to give you a better idea of what i mean
Here is my Adapter subclass for my custom ListView
static final String[] Names =
new String[] { "John", "Mike", "Maria", "Miguel"};
class MyArrayAdapter extends ArrayAdapter<String> {
private final Context context;
int which;
public MyArrayAdapter(Context context, String[] pValues) {
super(context, R.layout.main, pValues);
this.context = context;
values = pValues;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.main, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
Button call = (Button) rowView.findViewById(R.id.button1);
Button chat = (Button) rowView.findViewById(R.id.button2);
textView.setText(values[position]);
// Change icon based on name
s = values[position];
which = position;
call.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String name = values[which];
Toast.makeText(CustomListView.this, name, Toast.LENGTH_SHORT).show();
}
});
return rowView;
}
}
Edit:
String name = textView.getText().toString();
RelativeLayout ll = (RelativeLayout)v.getParent();
textView = (TextView)ll.findViewById(R.id.label);
Toast.makeText(CustomListView.this, name,
Toast.LENGTH_SHORT).show();
解决方案
Easy to do:
call.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
RelativeLayout rl = (RelativeLayout)v.getParent();
TextView tv = (TextView)rl.findViewById(R.id.label);
String text = tv.getText().toString();
Toast.makeText(CustomListView.this, text, Toast.LENGTH_SHORT).show();
}
});