1

我面临一个问题,它是关于当我滚动列表时列表视图中的已检查项目消失并且我不知道该怎么做。

这是我的自定义适配器代码:

public class SPCKontrola extends Activity {
ArrayList<Integer> yourSelectedItems= new ArrayList<Integer>();

@Override
        public void onResume() {
            super.onResume();

            yourSelectedItems.clear();

        }
}

public class SPCMjereAdapter extends BaseAdapter 
    {
        private Context context;

        public SPCMjereAdapter(Context c) 
        {           
            context = c;                    
        }

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

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

        public long getItemId(int position) {
            return position;
        }
        public View getView(final int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            if (convertView == null) {
                convertView = inflater.inflate(R.layout.activity_list_row, null); 

            }

            TextView txtOpis = (TextView) convertView.findViewById(R.id.ColOpis); 
            txtOpis.setText(MyArrList.get(position).get("OpisMjere") );

            TextView txtRbMjere = (TextView) convertView.findViewById(R.id.ColCode);
            txtRbMjere.setText(MyArrList.get(position).get("RbMjere"));

            CheckBox Chk = (CheckBox) convertView.findViewById(R.id.ColChk);
            Chk.setTag(MyArrList.get(position).get("RbMjere"));



Chk.setOnCheckedChangeListener(new OnCheckedChangeListener() {               
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if(isChecked){
                            yourSelectedItems.add(position);
                        }else{
                            if(yourSelectedItems.contains(position)){                                               
                                yourSelectedItems.remove((position));                                   
                            }
                        }
                    }
                    });


            return convertView;

        }
}

我应该在代码中更改或添加什么?我找到了一些示例,但我不能重用它们并在我的代码中实现:/

4

7 回答 7

5

为此,您必须维护一个数组变量yourSelectedItems[]来存储您检查过的视图位置..并在您的getView()方法中放置这样的条件..

if(yourSelectedItems.contains((position))){
    check.setChecked(true);
}else{
    check.setChecked(false);
}

getView()您可以通过在您的..中编写此方法来管理您的数组

 check.setOnCheckedChangeListener(new OnCheckedChangeListener() {               
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(isChecked){
            yourSelectedItems.add(position);
        }else{
            if(yourSelectedItems.contains(position)){                                               
                yourSelectedItems.remove((position));                                   
            }
        }
    });
于 2013-09-28T17:45:29.470 回答
1

为避免 ListView 中的 CheckBoxes 出现问题,您可以在开始时采用一个初始化为 false 的布尔数组,然后在 ListView 中选中复选框的数组中的相应位置设为 true。当您在应用程序中向前和向后移动或滚动 ListView 时,这不会导致复选框的选中状态出现问题。您可以在此处查看示例代码:

Android复选框多选问题

于 2013-09-28T20:25:01.273 回答
1

试试这个带有复选框的列表视图示例:http: //lalit3686.blogspot.in/2012/06/today-i-am-going-to-show-how-to-deal.html

于 2013-09-28T17:50:45.880 回答
1

用这个改变你的 getview() ......

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

            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            if (convertView == null) {
                convertView = inflater.inflate(R.layout.activity_list_row, null); 

            }

            TextView txtOpis = (TextView) convertView.findViewById(R.id.ColOpis); 
            txtOpis.setText(MyArrList.get(position).get("OpisMjere") );

            TextView txtRbMjere = (TextView) convertView.findViewById(R.id.ColCode);
            txtRbMjere.setText(MyArrList.get(position).get("RbMjere"));

            CheckBox Chk = (CheckBox) convertView.findViewById(R.id.ColChk);


             if(yourSelectedItems.contains((position))){
                Chk.setChecked(true);
             }else{
                Chk.setChecked(false);
             }

            Chk.setOnCheckedChangeListener(new OnCheckedChangeListener() {               
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if(isChecked){
                            yourSelectedItems.add(position);
                        }else{
                            if(yourSelectedItems.contains(position)){                                               
                                yourSelectedItems.remove((position));                                   
                            }
                        }
                    }
                    });


            return convertView;

        }
于 2013-09-29T11:22:21.697 回答
1

这发生在我身上,我解决了这个问题,

1.创建一个全局布尔数组列表,并在构造函数上用 MyArrList.size() 填充它。

2.当您更改复选框值时,使用视图索引设置 arratlist。

3.改变这个:

 CheckBox Chk = (CheckBox) convertView.findViewById(R.id.ColChk);
 Chk.setTag(MyArrList.get(position).get("RbMjere"));

有了这个:

 CheckBox Chk = (CheckBox) convertView.findViewById(R.id.ColChk);
 Chk.setTag(MyArrList.get(position).get("RbMjere"));
 Chk.setChecked(commentTexts.get(position));
于 2013-09-28T21:24:01.860 回答
0

如果您在 listView.onItemSelectedListner 中使用了 getView,您将面临这个问题 listView 不会将每个对象放在不同的视图中,这就是为什么当您调用 getView(2) 时它会返回屏幕上列表的第二个对象的视图而不是列表的第二个对象的视图...您必须自己使用类中的一些布尔值或复选框来实现它

于 2013-09-28T18:41:48.303 回答
0

我想我得到了一些东西......而不是Chk.setOnCheckedChangeListener()我已经改为Chk.setOnClickListener(new OnClickListener()

public class SPCMjereAdapter extends BaseAdapter 
    {
        private Context context;


        public SPCMjereAdapter(Context c) 
        {           
            context = c;                    
        }

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

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

        public long getItemId(int position) {
            return position;
        }
        public View getView(final int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            if (convertView == null) {
                convertView = inflater.inflate(R.layout.activity_list_row, null); 

            }

            TextView txtOpis = (TextView) convertView.findViewById(R.id.ColOpis); 
            txtOpis.setText(MyArrList.get(position).get("OpisMjere") );

            TextView txtRbMjere = (TextView) convertView.findViewById(R.id.ColCode);
            txtRbMjere.setText(MyArrList.get(position).get("RbMjere"));

            final CheckBox Chk = (CheckBox) convertView.findViewById(R.id.ColChk);


            if(yourSelectedItems.contains((position))){
                Chk.setChecked(true);
             }else{
                Chk.setChecked(false);
             }

            Chk.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    if (Chk.isChecked())
                    {
                         yourSelectedItems.add(position);
                    } else
                    {
                        if(yourSelectedItems.contains(position)){                                               
                            yourSelectedItems.remove((position));                                   
                        }
                    }

                }
            });


            return convertView;
        }

 }

现在向上/向下滚动时它工作正常。但是还有另一个问题,我有点困惑。当滚动和随机检查/取消选中项目时,程序崩溃并且我得到这些错误:

FATAL EXCEPTION: main
java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
at java.util.ArrayList.remove(ArrayList.java:399)
at com.dem.spckontrola.SPCKontrola$SPCMjereAdapter$1.onClick(SPCKontrola.java:830)
at android.view.View.performClick(View.java:4084)
at android.widget.CompoundButton.performClick(CompoundButton.java:100)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method

)

好的。我通过添加解决了这个问题 yourSelectedItems.remove(yourSelectedItems.indexOf(position));

所以现在正确的解决方案是:

 public class SPCMjereAdapter extends BaseAdapter 
    {
        private Context context;


        public SPCMjereAdapter(Context c) 
        {           
            context = c;                    
        }

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

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

        public long getItemId(int position) {
            return position;
        }
        public View getView(final int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            if (convertView == null) {
                convertView = inflater.inflate(R.layout.activity_list_row, null); 

            }

            TextView txtOpis = (TextView) convertView.findViewById(R.id.ColOpis); 
            txtOpis.setText(MyArrList.get(position).get("OpisMjere") );

            TextView txtRbMjere = (TextView) convertView.findViewById(R.id.ColCode);
            txtRbMjere.setText(MyArrList.get(position).get("RbMjere"));

            final CheckBox Chk = (CheckBox) convertView.findViewById(R.id.ColChk);


            if(yourSelectedItems.contains((position))){
                Chk.setChecked(true);
             }else{
                Chk.setChecked(false);
             }

            Chk.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    if (Chk.isChecked())
                    {
                         yourSelectedItems.add(position);
                    } else
                    {
                        if(yourSelectedItems.contains(position)){                                               
                            yourSelectedItems.remove(yourSelectedItems.indexOf(position));                                   
                        }
                    }

                }
            });


            return convertView;
        }

 }

但现在我面临新问题:如何获取所有选中和未选中项目的列表,其中一些复选框(项目)由于列表视图中的许多项目而仅在向下滚动时可见?

于 2013-09-29T18:04:26.810 回答