Ama*_*nni 14 arrays android listview view android-arrayadapter
我有一个带有两个按钮的自定义ListView,当我点击任何一行上的任何一个按钮时,我想在Listview上获得文本标签,现在只需弹出一个Toast.到目前为止没有任何工作我继续得到我的数组中的最后一项.
这是一个屏幕截图,让您更好地了解我的意思
这是我的自定义ListView的Adapter子类
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;
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:
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();
Run Code Online (Sandbox Code Playgroud)
Bar*_*rak 14
容易做到:
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();
}
});
Run Code Online (Sandbox Code Playgroud)
使用View的setTag属性..............
如
Button call = (Button) rowView.findViewById(R.id.button1);
call.setTag(position);
Run Code Online (Sandbox Code Playgroud)
和
call.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int which = -1;
Obejct obj = v.getTag();
if(obj instaceof Integer){
which = ((Integer)obj).intValue();
}
if(which >-1){
String name = values[which];
Toast.makeText(CustomListView.this, name, Toast.LENGTH_SHORT).show();
}
}
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
26201 次 |
最近记录: |