setListAdapter(new MyAdapter(this, android.R.layout.simple_list_item_1,
R.id.textView1,
(String[]) myArr.toArray(new String[]{})));
}
class MyAdapter extends ArrayAdapter<String>{
final int which;
public MyAdapter(Context context, int resource,
int textViewResourceId, String[] string) {
super(context, resource, textViewResourceId, string);
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int which=position;
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.custom_view, parent,false);
TextView tv = (TextView) row.findViewById(R.id.textView1);
ImageView iv = (ImageView) row.findViewById(R.id.imageView1);
ToggleButton tb = (ToggleButton) row.findViewById(R.id.toggleButton1);
tv.setTag(position);
iv.setTag(position);
tb.setTag(position);
tv.setText(myArr.get(position).toString());
tb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(tb.isChecked()){
which=(Integer) tb.getTag();
}else{
}
}
});
return row;
}
}
}
您好我正在尝试使用setTag视图方法来检测按下的项目。我的布局由一个列表组成,每行包含一个文本字段,一个togglebutton和一个imageview。我想更改包含按下按钮的行的图像。我走了这么远,我坚持如何继续,我可以得到一些帮助吗?
答案 0 :(得分:0)
您可以简单地将位置传递给getView(final int position, View convertView, ViewGroup parent)
。只需将其设为最终版并直接访问它而无需制作标签。
但是,您应该使用视图标记来处理android提供的可滚动视图。
class MyAdapter extends ArrayAdapter<String>{
final int which;
ArrayList<Boolean> mListImageStates = new ArrayList<Boolean>();
public MyAdapter(Context context, int resource,
int textViewResourceId, String[] string) {
super(context, resource, textViewResourceId, string);
for(int i = 0; i<string.length;i++)
{
mListImageStates.add(new Boolean(false));
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int which=position;
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ChildViewHolder holder = new ChildViewHolder();
if(convertView == null){
convertView = inflater.inflate(R.layout.custom_view, parent,false);
holder.mTv = (TextView) row.findViewById(R.id.textView1);
holder.mIv = (ImageView) row.findViewById(R.id.imageView1);
holder.mTb = (ToggleButton) row.findViewById(R.id.toggleButton1);
convertView.setTag(holder)}
else
{
holder = (ChildViewHolder) convertView.getTag();
}
holder.mTv.setText(myArr.get(position).toString());
holder.mTb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
boolean listState = mListImageStates.get(position);
mListImageStates.set(position,new Boolean(!listState));
notifyDataSetChanged();
}});
if(mListImageStates.get(position) == false)
{
//TODO
}else
{
//TODO
}
return convertView;
}
class ChildViewHolder
{
public TextView mTv;
public ImageView mIv;
public ToggleButton mTb;
}