我有一個ListView
,其中一個CheckBox
和一個EditText
。我想在每行中檢查CheckBox
時看到EditText
。目前,我設置的EditText
知名度,GONE
當點擊同一行中的另一項時,更改列表查看項目visibilty
這是我的適配器類:
private class MyCustomAdapter extends ArrayAdapter<Cheese> {
private ArrayList<Cheese> cheeseList;
public MyCustomAdapter(Context context, int textViewResourceId,
ArrayList<Cheese> cheeseList) {
super(context, textViewResourceId, cheeseList);
this.cheeseList = new ArrayList<Cheese>();
this.cheeseList.addAll(cheeseList);
}
private class ViewHolder {
EditText code;
CheckBox name;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.cheese_info, null);
holder = new ViewHolder();
holder.code = (EditText) convertView
.findViewById(R.id.quantity);
holder.name = (CheckBox) convertView
.findViewById(R.id.checkBox1);
convertView.setTag(holder);
holder.name.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
Cheese country = (Cheese) cb.getTag();
Toast.makeText(
getApplicationContext(),
"Clicked on Checkbox: " + cb.getText() + " is "
+ cb.isChecked(), Toast.LENGTH_LONG)
.show();
country.setSelected(cb.isChecked());
ViewHolder holder1 = (ViewHolder)v.getTag();
holder1.code.setVisibility(View.VISIBLE);
}
});
} else {
holder = (ViewHolder) convertView.getTag();
}
Cheese country = cheeseList.get(position);
holder.name.setText(country.getName());
holder.name.setChecked(country.isSelected());
holder.name.setTag(country);
return convertView;
}
}
我嘗試添加
ViewHolder holder1 = (ViewHolder)v.getTag();
holder1.code.setVisibility(View.VISIBLE);
內CheckBox
點擊。但是,應用程序越來越應聲給錯誤
java.lang.ClassCastException:com.example.viewpagersample.Cheese 不能轉換到 com.example.viewpagersample.MainActivity $ MyCustomAdapter $ ViewHolder
嘗試通過您的奶酪對象操作的知名度和使用setOnItemClickListener您的適配器之外! –
我認爲問題是與此行holder.name.setTag(國家); – Vishwa
你正在嘗試在標記而不是視圖中分配集合Cheese對象 – Vishwa