我有以下带有自定义列表项的 ListView:
我想TextView
从单击的列表项中获取值ToggleButton
。
这是构建列表的适配器:
public class simpleAdapter extends SimpleAdapter {
public simpleAdapter(Context context,
ArrayList<HashMap<String, String>> list, int resource,
String[] from, int[] to) {
super(context, list, resource, from, to);
// TODO Auto-generated constructor stub
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
// return super.getView(position, convertView, parent);
LayoutInflater inflater = getLayoutInflater();
convertView = inflater.inflate(R.layout.user_campaigns_list_item, parent, false);
tb_campaigns_list_toggle = (ToggleButton) convertView
.findViewById(R.id.tb_campaigns_list_toggle);
tb_campaigns_list_toggle.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), String.valueOf(position), Toast.LENGTH_SHORT).show();
}
});
return super.getView(position, convertView, parent);
}
}
使用此代码,我可以为包含点击的position
内容list item
敬酒ToggleButton
。尝试使用此代码获取Text1
or值时:id1
TextView Text1 = (TextView)convertView.findViewById(R.id.Text1_id); //this line is outside the onClick(View v) method
String Text1_value = Text1.getText().toString();
Toast.makeText(getApplicationContext(), Text1_value, Toast.LENGTH_SHORT).show();
我总是Text1
从最后ListView
一项中获得价值。
有任何想法吗?