0
我有'行'視圖,其中有兩個文本視圖和一個複選框。我試圖實現它,以便當單擊該複選框時,它將在同一行中顯示兩個文本視圖的字符串。空吐司消息
我已經實現了一個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);
}
}
任何一個可以幫助?
嘗試這種情況:Toast.makeText(上下文, viewHolder.text.getText()的toString(),10000) .show(); – 2013-03-07 12:18:25
@泰米爾不工作得到空吐司 – hesham 2013-03-07 12:25:28