选中复选框的问题

时间:2012-06-06 07:31:30

标签: java android

我使用this来设置listview中的复选框我按照给定的教程按照所有步骤进行操作,但是输出有一些关键问题,就是当我选择第一个复选框并向下滚动时它会更改选中项目并自动出现第3个。

所以我认为getview有问题。所以请帮帮我....

这是我的代码::

package com.AppFavorits;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.app.ListActivity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.RatingBar;

public class Favorites extends ListActivity {
    protected static final String TAG = "Favorites";
    CommentsDataSource datasource;
    ListView lstFavrowlistv;
    ArrayList alAppName;
    float[] rate;
    boolean[] bSelected;
    ArrayList<Comment> alPackagenm;
    Drawable[] alIcon;
    ViewHolder holder;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onResume() {
        super.onResume();
        datasource = new CommentsDataSource(this);
        datasource.open();

        alAppName = datasource.getAllComments();
        alPackagenm = datasource.getAllPackage();

        Log.i(TAG, "values >>>" + alAppName);
        Log.i(TAG, "values >>>" + alPackagenm);
        int inc = 0;
        alIcon = new Drawable[200];
        for (int i = 0; i < alPackagenm.size(); i++) {
            Log.i(TAG, "Appname >>>" + GetAllApp.lstpinfo.get(i).pname);
            for (int j = 0; j < GetAllApp.lstpinfo.size(); j++) {
                if (alPackagenm
                        .get(i)
                        .toString()
                        .equalsIgnoreCase(
                                GetAllApp.lstpinfo.get(j).pname.toString())) {
                    alIcon[inc] = GetAllApp.lstpinfo.get(j).icon;
                    Log.i("TAG", "sqlPackagename"
                            + alPackagenm.get(i).toString());
                    Log.i("TAG", "from getAllapp"
                            + GetAllApp.lstpinfo.get(j).pname.toString());
                    inc++;
                }

            }
        }

        ArrayList<RowModel> list = new ArrayList<RowModel>();
        ArrayList<Model> Mlist = new ArrayList<Model>();
        rate = new float[alAppName.size()];
        bSelected = new boolean[alAppName.size()];
        Iterator itr = alAppName.iterator();
        String strVal = null;
        while (itr.hasNext()) {
            strVal += itr.next().toString() + ",";

        }

        int lastIndex = strVal.lastIndexOf(",");
        strVal = strVal.substring(0, lastIndex);
        System.out.println("Output String is : " + strVal);
        String strAr[] = strVal.split(",");

        for (int i = 0; i < strAr.length; i++) {
            System.out.println("strAr[" + i + "] " + strAr[i]);
        }

        for (String s : strAr) {
            list.add(new RowModel(s));
        }
        for (String s : strAr) {
            Mlist.add(new Model(s));
        }
        setListAdapter(new RatingAdapter(list, Mlist));
        datasource.close();
    }

    class RowModel {
        String label;
        float rating = 0.0f;

        RowModel(String label) {
            this.label = label;
        }

        public String toString() {
            if (rating >= 3.0) {
                return (label.toUpperCase());
            }

            return (label);
        }
    }

    private RowModel getModel(int position) {
        return (((RatingAdapter) getListAdapter()).getItem(position));

    }

    class RatingAdapter extends ArrayAdapter<RowModel> {
        private ArrayList<Model> mlist;

        RatingAdapter(ArrayList<RowModel> list, ArrayList<Model> mlist) {
            super(Favorites.this, R.layout.outbox_list_item,
                    R.id.txvxFavrowiconappname, list);
            this.mlist = mlist;
        }

        public View getView(final int position, View convertView,
                ViewGroup parent) {
            View row = super.getView(position, convertView, parent);
            holder = (ViewHolder) row.getTag();
            if (holder == null) {
                holder = new ViewHolder(row);
                row.setTag(holder);

                RatingBar.OnRatingBarChangeListener l = new RatingBar.OnRatingBarChangeListener() {
                    public void onRatingChanged(RatingBar ratingBar,
                            float rating, boolean fromTouch) {
                        Integer myPosition = (Integer) ratingBar.getTag();
                        RowModel model = getModel(myPosition);

                        model.rating = rating;
                        rate[position] = rating;

                    }
                };
                holder.chkbxFavrowsel
                        .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                            @Override
                            public void onCheckedChanged(
                                    CompoundButton buttonView, boolean isChecked) {
                                Model element = (Model) holder.chkbxFavrowsel
                                        .getTag();
                                element.setSelected(buttonView.isChecked());
                                bSelected[position] = isChecked;

                            }
                        });
                holder.chkbxFavrowsel.setTag(mlist.get(position));
                holder.ratingBar1.setOnRatingBarChangeListener(l);
            } else {
                row = convertView;
                ((ViewHolder) row.getTag()).chkbxFavrowsel.setTag(mlist
                        .get(position));
            }

            RowModel model = getModel(position);
            ViewHolder holder = (ViewHolder) row.getTag();
            holder.ratingBar1.setTag(new Integer(position));
            holder.ratingBar1.setRating(model.rating);
            holder.imgvFavrowiconappicon.setImageDrawable(alIcon[position]);
            holder.txvxFavrowiconappname.setText(alAppName.get(position)
                    .toString());
            holder.chkbxFavrowsel.setChecked(mlist.get(position).isSelected());

            return (row);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

将以下代码用于MainActivity.java文件。

public class ListViewActivity extends Activity {

    ListView mLstView1;
    Button mBtn1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mLstView1 = (ListView) findViewById(R.id.mLstView1);
        String[] lv_items = { "Dipak", "Rahul", "Hiren", "Nandlal", "Keyur",
                "Kapil", "Dipak", "Rahul", "Hiren", "Nandlal", "Keyur",
                "Kapil" };
        ;
        mLstView1.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_multiple_choice, lv_items));
        mLstView1.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

        mBtn1 = (Button) findViewById(R.id.mBtn1);
        mBtn1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                // Using a List to hold the IDs, but could use an array.
                List<Integer> checkedIDs = new ArrayList<Integer>();
                // Get all of the items that have been clicked - either on or
                // off
                final SparseBooleanArray checkedItems = mLstView1
                        .getCheckedItemPositions();
                for (int i = 0; i < checkedItems.size(); i++) {
                    // And this tells us the item status at the above position
                    final boolean isChecked = checkedItems.valueAt(i);
                    if (isChecked) {
                        // This tells us the item position we are looking at
                        final int position = checkedItems.keyAt(i);
                        // Put the value of the id in our list
                        checkedIDs.add(position);
                        System.out.println("Position is:- " + position);
                    }
                }
            }
        });
    }
}

答案 1 :(得分:0)

我真的不明白你的代码正是你想要做的。我之前没有看到你正在检查holder == null应该是convertView == null的位置。如果您有滚动问题,可以查看我的blog post

 holder = (ViewHolder) row.getTag();
            if (holder == null) {
                holder = new ViewHolder(row);
                row.setTag(holder);
               ...
            }
           else {
                row = convertView;
                ((ViewHolder) row.getTag()).chkbxFavrowsel.setTag(mlist
                        .get(position));
            }