我有一个“行”视图,它有两个文本视图和一个复选框。我正在尝试实现它,以便在单击复选框时,它会在同一行中显示两个文本视图的字符串。
我已经实现了一个 for 循环来获取复选框视图的父级,然后迭代所有父级视图子级并从它们内部获取字符串。但是,我收到的不是文本,而是一条空的 Toast 消息。
我的第一堂课代码:
public class InteractiveArrayAdapter extends ArrayAdapter<model> implements
OnClickListener {
private final List<model> list;
private final Activity context;
public InteractiveArrayAdapter(Activity context, List<model> list) {
super(context, R.layout.rep, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView text;
protected CheckBox checkbox;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.rep, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.TextView07);
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.CheckBox05);
view.getParent();
viewHolder.checkbox
.setOnClickListener(new CompoundButton.OnClickListener() {
@Override
public void onClick(View v) {
boolean h = ((CompoundButton) v).isChecked();
model element = (model) viewHolder.checkbox
.getTag();
element.setSelected(h);
ViewGroup row = (ViewGroup) viewHolder.checkbox
.getParent();
for (int itemPos = 0; itemPos < ((ViewGroup) row)
.getChildCount(); itemPos++) {
View view = ((ViewGroup) row)
.getChildAt(itemPos);
if (view instanceof TextView) {
viewHolder.text = (TextView) view; //Found it!
Toast.makeText(context,
viewHolder.text.getText(), 10000)
.show();
break;
}
}
}
});
view.setTag(viewHolder);
viewHolder.checkbox.setTag(list.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(list.get(position).getName());
holder.checkbox.setChecked(list.get(position).isSelected());
return view;
}
}
我的二等车型:
public class ListActivity1 extends ListActivity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_list);
// Create an array of Strings, that will be put to our ListActivity
InteractiveArrayAdapter adapter = new InteractiveArrayAdapter(this,
getModel());
setListAdapter(adapter);
}
private List<model> getModel() {
List<model> list = new ArrayList<model>();
list.add(get("Linux"));
list.add(get("Windows7"));
list.add(get("Suse"));
list.add(get("Eclipse"));
list.add(get("Ubuntu"));
list.add(get("Solaris"));
list.add(get("Android"));
list.add(get("iPhone"));
list.add(get("iPhone"));
list.add(get("iPhone"));
list.add(get("iPhone"));
list.add(get("iPhone"));
list.add(get("iPhone"));
list.add(get("iPhone"));
list.add(get("iPhone"));
list.add(get("iPhone"));
list.add(get("iPhone"));
list.add(get("iPhone"));
list.add(get("iPhone"));
// Initially select one of the items
list.get(1).setSelected(true);
return list;
}
private model get(String s) {
return new model(s);
}
}
任何人都可以帮忙吗?