onCheckChangedリスナーは、CustomListViewの最初のチェックボックスでのみ機能します
-
25-10-2019 - |
質問
カスタムListViewのチェックボックスのリスナーは、最初のチェックボックスでのみ動作します。これはgetView()の位置と関係があると思います。この質問でコードを添付しています。この問題のために回避策を提案してください。
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();
if(convertView==null)
{
convertView = inflater.inflate(R.layout.custom_list, null);
holder = new ViewHolder();
holder.txtViewTitle = (TextView) convertView.findViewById(R.id.title_text);
holder.txtViewDescription = (TextView)convertView.findViewById(R.id.description_text);
holder.cb=(CheckBox) convertView.findViewById(R.id.cb);
convertView.setTag(holder);
holder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
String Channel=holder.txtViewTitle.getText().toString();
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(position==0)
{
//works
}
else
if(position==1)
{
//doesn't work
}
});
}
else
{
holder=(ViewHolder)convertView.getTag();
}
holder.txtViewTitle.setText(title[position]);
holder.txtViewDescription.setText(description[position]);
holder.txtViewDescription.setFocusable(false);
holder.txtViewTitle.setFocusable(false);
return convertView;
}
解決
convertViewはすべてのアイテムの温度であり、最初のコールNULLでのみ、次のような各アイテムのリスナーを追加する必要があります。
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();
if(convertView==null)
{
convertView = inflater.inflate(R.layout.custom_list, null);
holder = new ViewHolder();
holder.txtViewTitle = (TextView) convertView.findViewById(R.id.title_text);
holder.txtViewDescription = (TextView)
convertView.findViewById(R.id.description_text);
holder.cb=(CheckBox) convertView.findViewById(R.id.cb);
convertView.setTag(holder);
} else {
holder=(ViewHolder)convertView.getTag();
}
holder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
String Channel=holder.txtViewTitle.getText().toString();
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(position==0)
{
//works
}else if(position==1){
//doesn't work
}
});
holder.txtViewTitle.setText(title[position]);
holder.txtViewDescription.setText(description[position]);
holder.txtViewDescription.setFocusable(false);
holder.txtViewTitle.setFocusable(false);
return convertView;
}
所属していません StackOverflow