在列表视图中的CheckBox问题有了不同布局的行 [英] CheckBox Issue in List View With Different Layout For Rows

查看:255
本文介绍了在列表视图中的CheckBox问题有了不同布局的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使列表视图具有的TextView 复选框。它工作得很好,但是当选择复选框在另一个鳞次栉比另一个复选框选择了。
例:如果我选中第一行和向下滚动,我发现选择过其他行

I tried make list view has TextView and CheckBox. It is worked fine but when select CheckBox in row another CheckBox in another row selected too. Example: if I checked first row and scroll down I found another row selected too.

这是我的适配器code

This my Adapter Code

public class ReadersrListAdapter extends BaseAdapter{
private static final int TYPE_SEPARATOR = 0;
private static final int TYPE_ITEM = 1;
private static final int TYPE_MAX_COUNT = 2;
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private LayoutInflater mInflater=null; 



public ReadersrListAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data=d;
    mInflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public int getCount() {
    return data.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

@Override
public boolean areAllItemsEnabled() {
    return false;
}

@Override
public boolean isEnabled(int position) {
    return !data.get(position).get(UtiliShare.KEY_TITLE).startsWith("-");
}

@Override
public int getItemViewType(int position) {
    return data.get(position).get(UtiliShare.KEY_TITLE).startsWith("-") ? TYPE_SEPARATOR : TYPE_ITEM;
}

@Override
public int getViewTypeCount() {
    return TYPE_MAX_COUNT;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;        
    ViewHolder2 holder2 = null;
    int type = getItemViewType(position);

    if (convertView == null) {
        //holder = new ViewHolder();
        switch (type) {
            case TYPE_ITEM:
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.reader_list_item, null);
                holder.textView = (TextView)convertView.findViewById(R.id.readerTitle);
                holder.textView.setTypeface(UtiliShare.getTf());
                convertView.setTag(holder);
                break;
            case TYPE_SEPARATOR:
                holder2 = new ViewHolder2();
                convertView = mInflater.inflate(R.layout.reader_list_devider, null);
                holder2.textView = (TextView)convertView.findViewById(R.id.readerTitle);
                holder2.textView.setTypeface(UtiliShare.getTf());
                convertView.setTag(holder2);
                break;
        }
        //convertView.setTag(holder);
    } else {
        if(type==TYPE_ITEM) holder = (ViewHolder)convertView.getTag();
        else holder2 = (ViewHolder2)convertView.getTag();
    }
    HashMap<String, String> curdata = new HashMap<String, String>();
    curdata = data.get(position);
    String txt = curdata.get(UtiliShare.KEY_TITLE);
    if(type == TYPE_SEPARATOR){
        txt = txt.replace("-", "");
        holder2.textView.setText(txt);
    }else{
        //if(position <= 100) System.out.println("getView " + position + " " + convertView + " type = " + type);
        holder.star = (CheckBox) convertView.findViewById(R.id.btn_Fav);
        holder.star.setOnCheckedChangeListener(((ReadersListActivity) this.activity).mStarCheckedChanceChangeListener);

        holder.textView.setText(txt);
    }
    //holder.textView.setText(txt);
    return convertView;
}



private static class ViewHolder {
    public CheckBox star;
    public TextView textView;
}

private static class ViewHolder2 {
    public TextView textView;
}

}

和本 OnCheckedChangeListener 活性

public OnCheckedChangeListener mStarCheckedChanceChangeListener = new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
         // TODO Cyril: Not implemented yet!
            final int position = list.getPositionForView(buttonView);
            Toast.makeText(ReadersListActivity.this, ""+position, Toast.LENGTH_SHORT).show();
        }
    };

我怎样才能解决这个问题?

How Can I fix this???

感谢。

推荐答案

正如你可能知道,你滚动布局重用,所以你需要跟踪每个行是否应检查自己。让我们一个新的成员添加到您的适配器:

As you may know the layouts are reused as you scroll, so you need to track whether each row should be checked yourself. Let's add a new member to your Adapter:

private SparseBooleanArray checked = new SparseBooleanArray();

和一种新的方法:

public void toggleCheck(int position) {
    boolean state = expanded.get(position, false); 
    expanded.put(!state);
}

现在调用 onCheckedChanged()我们的新方法

final int position = list.getPositionForView(buttonView);
adapter.toggleCheck(position);

最后我们修改 getView()来改变选中状态(以及其他一些东西):

Lastly let's tweak getView() to change the checked state (and a few other things):

// Don't create a new HashMap that you'll never use
//HashMap<String, String> curdata = new HashMap<String, String>();

HashMap<String, String> curdata = data.get(position);
String txt = curdata.get(UtiliShare.KEY_TITLE);
if(type == TYPE_SEPARATOR){
    txt = txt.replace("-", "");
    holder2.textView.setText(txt);
}else{
    //if(position <= 100) System.out.println("getView " + position + " " + convertView + " type = " + type);

    // These two lines should be in `if(convertView == null)` with the others
    holder.star = (CheckBox) convertView.findViewById(R.id.btn_Fav);
    holder.star.setOnCheckedChangeListener(((ReadersListActivity) this.activity).mStarCheckedChanceChangeListener);

    // Set the appropriate values
    holder.textView.setText(txt);
    holder.star.setChecked(expanded.get(position, false));
}

这篇关于在列表视图中的CheckBox问题有了不同布局的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆