检查列表视图中的复选框,使其他随机的复选框选中过复选框、使其、视图、列表

2023-09-12 09:48:01 作者:动了情伤了心

每当我选中的复选框在我的列表视图,其他随机复选框得到检查过。这可能是由于产品回收由列表视图。

我也试着设置机器人:可调焦=假在我的布局复选框在一些地方建议,但仍是onListItemClick()不要求行时其复选框是checked.Only当我点击别的地方它被调用。

我要的是,只有用户选中的复选框应继续检查,直至用户取消选中它们。

我在下面提供的code这是完整的,可以直接运行。

whenever i check a checkbox in my listview , other random checkboxes get checked too . It could be due to item recycling by listview.

I also tried setting android:focusable="false" to checkbox in my layout as suggested in some places, but still the onListItemClick() is not called for a row when its checkbox is checked.Only when I click somewhere else it gets called.

What I want is that only the user-checked checkboxes should remain checked until the user unchecks them.关于word下划线换行的问题怎么办

I give below the code which is complete and could be run directly.

C- ProjActivity.java活动$ C $:

Activity code- ProjActivity.java:

public class ProjActivity extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    PackageManager pm = getPackageManager();
    List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);

    final CopyOfMyCustomAdapter a = new CopyOfMyCustomAdapter(this, packages);
    getListView().setAdapter(a);
}}

最后,自定义布局文件 - testlayout.xml

And finally, the custom layout file- testlayout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" 

>

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:text="CheckBox" 
    android:focusable="false"
    />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" 
    android:focusable="false"
    />

更新:我在下面的答案的建议后CustomAdapter:

UPDATE : My CustomAdapter after the suggestion in an answer below:

public class MyCustomAdapter extends ArrayAdapter<ApplicationInfo>  {

private List<ApplicationInfo> appInfoList;
private LayoutInflater mInflater;
private PackageManager pm;
ArrayList<Boolean> positionArray;
private Context ctx;
int[] visiblePosArray;
private volatile int positionCheck; 

public MyCustomAdapter(Context context, List<ApplicationInfo> myList) {
    super(context, NO_SELECTION);
    appInfoList = myList;
    ctx=context;
    mInflater =     (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    pm = context.getPackageManager();

    positionArray = new ArrayList<Boolean>(myList.size());
    for(int i =0;i<myList.size();i++){
        positionArray.add(false);
    }
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return appInfoList.size();
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    View row = convertView;
    Holder holder = null;

    if(row==null){
        row = mInflater.inflate(R.layout.testlayout, null); 
        //  visiblePosArray[position%visiblePosArray.length]=position;
        holder = new Holder();
        holder.appIcon = (ImageView)row.findViewById(R.id.imageView1);

        holder.ckbox =(CheckBox)row.findViewById(R.id.checkBox1);

        row.setTag(holder);
    } else {

        holder = (Holder) convertView.getTag();
    }

    holder.ckbox.setFocusable(false);
    holder.appIcon.setImageDrawable(appInfoList.get(position).loadIcon(pm));
    holder.ckbox.setChecked(positionArray.get(position));
    holder.ckbox.setText(appInfoList.get(position).loadLabel(pm));
    holder.ckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked ){
            System.out.println(position+"--- :)");
                positionArray.add(position, true);
            }else
                positionArray.add(position, false);
        }
    });

    return row;
}
static class Holder
{
    ImageView appIcon;
    CheckBox ckbox;

}

}

当我向上和向下滚动我可以看到随机指标变为真正的在我的布尔ArrayList中时syso他们。

When I scroll up and down I could see random indices changed to true in my boolean Arraylist when in syso them.

推荐答案

在一个列表视图回收的观点,它回收其连接到它present状态的以及的听众。在我的例子中,如果该复选框被选中,并具有onCheckedChangeListener组,既将保持基于位置再循环视图的一部分。所以这是我们的责任重置所有状态并删除previous听众。

所以,当我被取消选中循环来看,onCheckedChange监听器得到执行。 一行制作的节目很好地工作。侦听器去除:

When a listview recycles views , it recycles its present state as well as listeners attached to it. In my example, if the checkbox was checked and has a onCheckedChangeListener set, both will remain a part of recycled view based on position. So it is our responsibility to reset all states and remove previous listeners.

So when I was unchecking the recycled view, the onCheckedChange listener was getting executed. one line made the program work perfectly. The listener was removed by :

holder.ckbox.setOnCheckedChangeListener(null); 

下面是适配器的人谁可能偶然发现这一问题的工作code:

Below is the working code of Adapter for people who may stumble upon this problem:

public class MyCustomAdapter extends ArrayAdapter<ApplicationInfo>  {

private List<ApplicationInfo> appInfoList;
private LayoutInflater mInflater;
private PackageManager pm;
ArrayList<Boolean> positionArray;
private Context ctx;
int[] visiblePosArray;
private volatile int positionCheck; 

public MyCustomAdapter(Context context, List<ApplicationInfo> myList) {
    super(context, NO_SELECTION);
    appInfoList = myList;
    ctx=context;
    mInflater =     (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    pm = context.getPackageManager();

    positionArray = new ArrayList<Boolean>(myList.size());
    for(int i =0;i<myList.size();i++){
        positionArray.add(false);
    }
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return appInfoList.size();
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    View row = convertView;
    Holder holder = null;

    if(row==null){
        row = mInflater.inflate(R.layout.testlayout, null); 
        //  visiblePosArray[position%visiblePosArray.length]=position;
        holder = new Holder();
        holder.appIcon = (ImageView)row.findViewById(R.id.imageView1);

        holder.ckbox =(CheckBox)row.findViewById(R.id.checkBox1);

        row.setTag(holder);
    } else {

        holder = (Holder) convertView.getTag();
    holder.ckbox.setOnCheckedChangeListener(null);

    }

    holder.ckbox.setFocusable(false);
    holder.appIcon.setImageDrawable(appInfoList.get(position).loadIcon(pm));
    holder.ckbox.setChecked(positionArray.get(position));
    holder.ckbox.setText(appInfoList.get(position).loadLabel(pm));
    holder.ckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked ){
            System.out.println(position+"--- :)");
                positionArray.set(position, true);

            }else
                positionArray.set(position, false);
        }
    });

    return row;
}
static class Holder
{
    ImageView appIcon;
    CheckBox ckbox;

}

}

 
精彩推荐
图片推荐