我有ListView
。我在其中显示Button
的列表,每行中有3个。我可以对Button
上的点击做出反应就好了,但现在我需要在点击它后将Button
的文本从“应用”更改为“已应用”。
我已在OnClickListener
中实现了此功能,但现在当我点击Button
时,它不仅会设置我点击的Button
的文字,而且还会更改文字其他Button
的。我不知道出了什么问题。
这是我的Adapter
:
public class InteractiveFederalArrayAdapter extends
ArrayAdapter<FederalProperties> {
Activity context;
List<FederalProperties> list;
public InteractiveFederalArrayAdapter(Activity context,
List<FederalProperties> list) {
super(context, R.layout.federal_row, list);
// TODO Auto-generated constructor stub
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView titleView;
protected TextView subTitleView;
protected Button phoneButton;
protected Button emailButton;
protected Button infoButton;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.federal_row, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.titleView = (TextView)view.findViewById(R.id.titleId);
viewHolder.subTitleView = (TextView) view
.findViewById(R.id.subTitleId);
viewHolder.phoneButton = (Button) view
.findViewById(R.id.phoneButton);
viewHolder.emailButton = (Button) view
.findViewById(R.id.emailButton);
viewHolder.infoButton = (Button) view.findViewById(R.id.infoButton);
view.setTag(viewHolder);
} else {
view = convertView;
}
final ViewHolder holder = (ViewHolder) view.getTag();
holder.infoButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
holder.infoButton.setText("Applied");
}
});
holder.titleView.setText(list.get(position).getFederalTitle());
holder.subTitleView.setText(list.get(position).getFederalSubTitle());
return view;
}
}
答案 0 :(得分:0)
尝试这样的事情:
public class InteractiveFederalArrayAdapter extends
ArrayAdapter<FederalProperties> {
Activity context;
List<FederalProperties> list;
public InteractiveFederalArrayAdapter(Activity context,
List<FederalProperties> list) {
super(context, R.layout.federal_row, list);
// TODO Auto-generated constructor stub
this.context = context;
this.list = list;
}
protected TextView titleView;
protected TextView subTitleView;
protected Button phoneButton;
protected Button emailButton;
protected Button infoButton;
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view = null;
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.federal_row, null);
titleView = (TextView)view.findViewById(R.id.titleId);
subTitleView = (TextView) view
.findViewById(R.id.subTitleId);
phoneButton = (Button) view
.findViewById(R.id.phoneButton);
emailButton = (Button) view
.findViewById(R.id.emailButton);
infoButton = (Button) view.findViewById(R.id.infoButton);
infoButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
infoButton.setText("Applied");
}
});
titleView.setText(list.get(position).getFederalTitle());
holder.subTitleView.setText(list.get(position).getFederalSubTitle());
return view;
}
}
你可以像这样维护一个ArrayList:
infoButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
infoButton.setText("Applied");
arraylist.set(position, "Applied");
}
});
btn.settext(arraylist.get(position));