-1

当我在列表视图中有更多项目并且我选择了一些复选框并将其向下滚动以选择更多复选框然后上面选择的复选框被取消选择。

请帮忙。我可以在哪里保存检查的状态和位置以及何时刷新列表。下面给出的代码。

     protected void onCreate(Bundle savedInstanceState) {

     dladapter = new DifferenceListAdapter(this,
                prodCodeArr, prodDescArr, serialBatchArr, expDtArr, qtyArr, serialNo, 0);
        diffeneceLv.setAdapter(dladapter);}

     static class ViewHolder {
    TextView srNotv, prodCodetv, prodDesctv, serialBatchNotv, expDatetv, qtytv;
    CheckBox consCheckBox;
}

     public class DifferenceListAdapter extends BaseAdapter{        
    Context c;
    String[] prodCode, prodDesc, expDate, qty, serialNo, serialBatchNo;
    //TextView srNotv, prodCodetv, prodDesctv, serialBatchNotv, expDatetv, qtytv;
    EditText consumedEt, disputeEt, OTCEt, patientnameEt;
    //final ArrayList<String> chkList = new ArrayList<String>();
    ArrayList<Boolean> chkState;
    //CheckBox consCheckBox;
    private boolean checked = false;
    ViewHolder viewHolder;
    View row;
    int f=0;


public DifferenceListAdapter(Context c, String[] prodCode, String[] prodDesc, String[] serialBatchNo, String[] expDate, String[] qty, String[] serialNo, int f){


        this.c = c;
        this.prodCode= prodCode;
        this.prodDesc= prodDesc;
        this.serialBatchNo= serialBatchNo;
        this.expDate = expDate;
        this.qty=qty;
        this.serialNo= serialNo;
        this.f= f;



        chkState = new ArrayList<Boolean>();

    }

public String[] getChecked(){


    return chkList.toArray(new String[chkList.size()]);
}   

    public int getCount() {
        // TODO Auto-generated method stub
        return prodCode.length;
    }

    public Object getItem(int arg0) {
        // TODO Auto-generated method stub

        return serialBatchNo[arg0];
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }
    public CheckBox getCheckBox() {
        return viewHolder.consCheckBox;
    }

    public void toggleChecked() {
        checked = !checked;

    }

    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        final int p= position;
        //if (convertView == null) {
        viewHolder=new ViewHolder();
        LayoutInflater inflater= (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row= inflater.inflate(R.layout.consumedstockitemrow, null);     
        viewHolder.consCheckBox = (CheckBox) row.findViewById(R.id.conschkbx);
        viewHolder.srNotv=(TextView) row.findViewById(R.id.rowcdiffSrNoTv);
        viewHolder.prodCodetv=(TextView) row.findViewById(R.id.rowcdiffProductCodeTv);
        viewHolder.prodDesctv=(TextView) row.findViewById(R.id.rowcdiffProdDescTv);
        viewHolder.serialBatchNotv=(TextView) row.findViewById(R.id.rowcdiffSerialBatchTv);
        viewHolder.expDatetv=(TextView) row.findViewById(R.id.rowcdiffExpDtTv);
        viewHolder.qtytv=(TextView) row.findViewById(R.id.rowcdiffQtyTv);

        viewHolder.consCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                 CheckBox cb = (CheckBox) buttonView;

                    if(cb.isChecked()== true)
                    {


                        if(chkList.contains(serialBatchNo[p]))
                        {

                        }
                        else
                        {
                        chkList.add(serialBatchNo[p]);
                        }

                    }
                    else
                    {

                        if(chkList.contains(serialBatchNo[p]))
                        {
                            chkList.remove(serialBatchNo[p]);
                        }


                    }

            }
        });


        viewHolder.srNotv.setText(String.valueOf(p+1));
        viewHolder.prodCodetv.setText(prodCode[p].toString());
        viewHolder.prodDesctv.setText(prodDesc[p].toString());
        viewHolder.serialBatchNotv.setText(serialBatchNo[p].toString());
        viewHolder.expDatetv.setText(expDate[p].toString());
        viewHolder.qtytv.setText(qty[p].toString());



        return row;
    }

}
4

6 回答 6

2

我发现一些解决方法不是最好的解决方案,但是如果视图(CheckBox)不再可见,则在复选框侦听器上执行其工作提供选中复选框的位置列表以及在 getView 上设置复选框的状态

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    if(buttonView.isShown()) // chek if checkbox is visible 
    {
        int position = buttonView.getId();  // get position of check box 
        _selected[position] = buttonView.isChecked(); // set true or false  in list of seleted checkboxes 

    }

}

getView 看起来像这样

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

    View view = convertView;
    if (view == null) {
        LayoutInflater inflater = activity.getLayoutInflater();
        view = inflater.inflate(R.layout.layout, null);
        ViewHolder viewHolder = new ViewHolder();
        viewHolder.tv = (TextView) rowView.findViewById(R.id.text);
        viewHolder.cb = (CheckBox) rowView.findViewById(R.id.check_box);
        viewHolder.cb.setOnCheckedChangeListener(this);
        view.setTag(viewHolder);
    }

    ViewHolder holder = (ViewHolder) view.getTag();
    holder.tv.setText("Text");
    holder.cb.setChecked(_selected[position]); //Geting state of check box from array
    holder.cb.setId(position); // set check box ID(position in adapter)

    return view;


}

视图持有者

class ViewHolder
{
    TextView tv;
    CheckBox cb;

}
于 2014-01-27T14:35:17.500 回答
1

下面是我使用的工作代码

使用 Array-adapter 在列表视图中显示复选框

MainActivity.java

package com.rdc.activity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

    private ListView mainListView = null;
    private Planet[] planets = null;
    private ArrayAdapter<Planet> listAdapter = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mainListView = (ListView) findViewById(R.id.mainListView);

        // When item is tapped, toggle checked properties of 
            //   CheckBox and Planet.

        mainListView.setOnItemClickListener(new
                         AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View item,
            int position, long id) {
        Planet planet = listAdapter.getItem(position);
        planet.toggleChecked();
        PlanetViewHolder viewHolder = 
                           (PlanetViewHolder) item
               .getTag();
                viewHolder.getCheckBox()
                     .setChecked(planet.isChecked());
                }
                });

        // Create and populate planets.
        planets = (Planet[]) getLastNonConfigurationInstance();
        if (planets == null) {
            planets = new Planet[] { new Planet("Mercury"),
                    new Planet("Venus"), new Planet("Earth"),
                    new Planet("Mars"), new Planet("Jupiter"),
                    new Planet("Saturn"), new Planet("Uranus"),
                    new Planet("Neptune"), new Planet("Ceres"),
                    new Planet("Pluto"), new Planet("Haumea"),
                    new Planet("Makemake"), new Planet("Eris") };
        }
        ArrayList<Planet> planetList = new ArrayList<Planet>();
        planetList.addAll(Arrays.asList(planets));

        // Set our custom array adapter as the ListView's adapter.
        listAdapter = new PlanetArrayAdapter(this, planetList);
        mainListView.setAdapter(listAdapter);
    }

    /** Holds planet data. */
    private static class Planet {
        private String name = "";
        private boolean checked = false;

        public Planet() {
        }

        public Planet(String name) {
            this.name = name;
        }

        public Planet(String name, boolean checked) {
            this.name = name;
            this.checked = checked;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public boolean isChecked() {
            return checked;
        }

        public void setChecked(boolean checked) {
            this.checked = checked;
        }

        public String toString() {
            return name;
        }

        public void toggleChecked() {
            checked = !checked;
        }
    }

    /** Holds child views for one row. */
    private static class PlanetViewHolder {
        private CheckBox checkBox;
        private TextView textView;

        public PlanetViewHolder() {
        }

        public PlanetViewHolder(TextView textView, CheckBox checkBox) {
            this.checkBox = checkBox;
            this.textView = textView;
        }

        public CheckBox getCheckBox() {
            return checkBox;
        }

        public void setCheckBox(CheckBox checkBox) {
            this.checkBox = checkBox;
        }

        public TextView getTextView() {
            return textView;
        }

        public void setTextView(TextView textView) {
            this.textView = textView;
        }
    }

    /** Custom adapter for displaying an array of Planet objects. */
    private static class PlanetArrayAdapter extends ArrayAdapter<Planet> {

        private LayoutInflater inflater;

        public PlanetArrayAdapter(Context context, List<Planet> planetList) {
            super(context, R.layout.simplerow, R.id.rowTextView, planetList);
            // Cache the LayoutInflate to avoid asking for a new one each time.
            inflater = LayoutInflater.from(context);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Planet planet = (Planet) this.getItem(position);
            CheckBox checkBox;
            TextView textView;

            // Create a new row view
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.simplerow, null);

                textView = (TextView) convertView
                        .findViewById(R.id.rowTextView);
                checkBox = (CheckBox) convertView.findViewById(R.id.CheckBox01);

                convertView.setTag(new PlanetViewHolder(textView, checkBox));

                // If CheckBox is toggled, update the planet it is tagged with.
                checkBox.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        CheckBox cb = (CheckBox) v;
                        Planet planet = (Planet) cb.getTag();
                        planet.setChecked(cb.isChecked());
                    }
                });
            }
            // Re-use existing row view
            else {

                PlanetViewHolder viewHolder = (PlanetViewHolder) convertView
                        .getTag();
                checkBox = viewHolder.getCheckBox();
                textView = viewHolder.getTextView();
            }

            // Tag the CheckBox with the Planet it is displaying, so that we can
            // access the planet in onClick() when the CheckBox is toggled.
            checkBox.setTag(planet);

            // Display planet data
            checkBox.setChecked(planet.isChecked());
            textView.setText(planet.getName());

            return convertView;
        }

    }

    public Object onRetainNonConfigurationInstance() {
        return planets;
    }
}

main.xml(用于列表视图)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/mainListView">
    </ListView>

</LinearLayout>

simplerow.xml(用于列表视图的行)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView
        android:id="@+id/rowTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textSize="16sp">
    </TextView>

    <CheckBox
        android:id="@+id/CheckBox01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:layout_alignParentRight="true"
        android:layout_marginRight="6sp"
        android:focusable="false">
    </CheckBox>

</RelativeLayout>

这是结果屏幕..

在此处输入图像描述

如果您对此仍有任何问题,请告诉我!

这里是完整教程

于 2013-01-03T06:54:56.837 回答
1

只需覆盖适配器的 getView 方法:

    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.row, parent, false);
            wrapper = new YourWrapper(convertView);
            convertView.setTag(wrapper);
        }
        else
        {
            wrapper = (YourWrapper) convertView.getTag();
        }

        wrapper.getChecker().setOnCheckedChangeListener(
                new OnCheckedChangeListener()
                {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView,
                            boolean isChecked)
                    {
                        // Get the position that set for the checkbox using setTag.
                        int getPosition = (Integer) buttonView.getTag();
                        // Set the value of checkbox to maintain its state.
                        list.get(getPosition).setCheck(buttonView.isChecked());
                    }
                });
        wrapper.getChecker().setTag(position);
        wrapper.getTags().setText(list.get(position).getTitle());
        wrapper.getChecker().setChecked(list.get(position).getCheck());

        return convertView;
    }
于 2013-01-03T06:03:41.760 回答
0

无论您要返回到 BaseAdapter 中的 getItem 的任何对象,请确保您在每个项目上使用一个选中的布尔对象。并且对于每个检查的项目,您可以将选中的布尔值设置为 true ..并在 getView 中检查项目是否被检查而不是检查复选框。

于 2013-01-03T05:57:37.773 回答
0

对于你的问题,我有一个解决方案。在我的项目中,我使用自定义适配器扩展基本适配器,在 getview 方法中使用此代码:

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

        if (convertView == null) {

            LayoutInflater li = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = li.inflate(R.layout.contact_row, null);

        }

        TextView tv_name = (TextView) convertView
                .findViewById(R.id.contactnameid);
        TextView tv_no = (TextView) convertView
                .findViewById(R.id.contactnoid);

        tv_name.setText(names[position]);
        tv_no.setText(numbers[position]);

        final CheckBox ch_bx = (CheckBox) convertView
                .findViewById(R.id.contactchkboxid);

        ch_bx.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

                CheckBox ch = (CheckBox) v
                        .findViewById(R.id.contactchkboxid);

                if (ch.isChecked()) {
                    itemchecked.set(position, true);
                    Final_SMS_Nos.add(numbers[position]);
                    Log.v("SMS No adding", "" + Final_SMS_Nos);

                } else if (!ch.isChecked()) {
                    itemchecked.set(position, false);
                    Final_SMS_Nos.remove(numbers[position]);
                    Log.v("SMS No deleting", "" + Final_SMS_Nos);
                }

            }
        });

        ch_bx.setChecked(itemchecked.get(position));

        return convertView;

    }
于 2013-01-03T05:59:46.923 回答
0

尝试setChecked()setOnCheckedChangeListener().

于 2019-08-19T19:28:27.100 回答