滚动我的自定义ListView后,CheckBoxes取消选中

时间:2015-08-08 10:14:12

标签: android android-listview

我创建了一个自定义ListView,并在其中放入了一些CheckBox 首先,我检查所有CheckBox,然后向下滚动ListView,然后CheckBoxes取消选中。

我该怎么办?

CustomListItem.java

public class HomeListAdapter extends ArrayAdapter<HashMap<String, Object>> {

    //new

    //private final boolean[] mCheckedState;
   // private final Context mContext;
    boolean[] itemChecked;
    //
    private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();



    private DisplayImageOptions options;
        public HomeListAdapter(Context context, ArrayList<HashMap<String, Object>> dataMap) {

            super(context, 0, dataMap);
//new
            itemChecked = new boolean[dataMap.size()];
          //  mContext = context;
     //
            options = new DisplayImageOptions.Builder()
                    .showImageOnLoading(R.drawable.com_facebook_profile_picture_blank_square)
                    .showImageForEmptyUri(R.drawable.com_facebook_profile_picture_blank_square)
                    .showImageOnFail(R.drawable.log)
                    .cacheInMemory(true)
                    .cacheOnDisk(true)
                    .considerExifParams(true)
                    .displayer(new RoundedBitmapDisplayer(20)).build();
           this.notifyDataSetChanged();
        }
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            HashMap<String, Object> dataMap = getItem(position);
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item1, parent, false);
            }
            TextView description = (TextView) convertView.findViewById(R.id.description);
            description.setText(dataMap.get(Home1.TAG_CNAME).toString());
            TextView email = (TextView) convertView.findViewById(R.id.company);
            email.setText(dataMap.get(Home1.TAG_EMAIL).toString());

         final CheckBox check= (CheckBox) convertView.findViewById(R.id.ck);
            //new
            /*
check.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (check.isChecked())
            itemChecked[position] = true;
        Toast.makeText(getContext(),itemChecked,);
        else
            itemChecked[position] = false;

    }
});*/
check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (check.isChecked())
            itemChecked[position] = true;
       // Toast.makeText(getContext(),itemChecked,);
        else
        itemChecked[position] = false;

    }

});


            //
            if (dataMap.get(Home1.TAG_LIKE).toString().equals("true")) {
                check.setChecked(true);
            }else{
                check.setChecked(false);
            }
            ImageView logo = (ImageView) convertView.findViewById(R.id.logo);
            ImageLoader.getInstance().displayImage(dataMap.get(Home1.TAG_IMAGE).toString(),logo, options, animateFirstListener);
            return convertView;
        }

这是我的适配器类请帮助我们解决这个问题....

    @Override
    public void onCreate(Bundle savedInstanceState) {

        //Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
        session = new SessionManager(getApplicationContext());

        // get user data from session
        HashMap<String, String> user = session.getUserDetails();


        // apikey
        apikey = user.get(SessionManager.KEY_api);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);

        ImageView back = (ImageView) findViewById(R.id.sel);
        back.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
//                Intent intent = new Intent(Home1.this, Selected.class);
//                finish();
//                startActivity(intent);
                try {
                    ListView lv = getListView();
                    ids = "";
                    int count = 0;
                    String text = "";
                    //CheckBox cbView;

                    TextView textView;
                    int total = lv.getCount();
                    for (int i = 0; i < total; i++) {
                        if (lv.getChildAt(i) != null) {
                            textView = (TextView) lv.getChildAt(i).findViewById(R.id.company);
                            text = textView.getText().toString();

                            cbView = (CheckBox) lv.getChildAt(i).findViewById(R.id.ck);




                            if (cbView.isChecked()) {
                                count += 1;
                                ids += text + ", ";
                            }
                        }
                    }
                    ids = ids.substring(0, ids.length()-2);
                    //Toast.makeText(getApplicationContext(), "Company emails: " + ids + " Total Checked: " + count, Toast.LENGTH_SHORT).show();
                    new DownloadOperation().execute();
                }
                catch(Exception ex){
                    System.out.println("My Message");
                    ex.printStackTrace();
                    System.out.println(ex.getMessage());
                }
            }

        });

1 个答案:

答案 0 :(得分:0)

您可以使用ChoiceMode中的CHOICE_MODE_MULTIPLE ListView选项public class List11 extends ListActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, GENRES)); final ListView listView = getListView(); listView.setItemsCanFocus(false); listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); } private static final String[] GENRES = new String[] { "Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama", "Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller" }; } 轻松解决此问题 -

HashMap<String, Object>

修改 替代您的背景 - 使用isChecked list添加一个额外的布尔对象,并在填充CheckBox时将其初始化为false。

getView()方法中的check.setChecked(Boolean.parseBoolean(dataMap.get("isChecked")); 设为 -

OnCheckedChangeListener()

现在,在监听器dataMap中,相应地设置//////////////////////////// /// CONSTANTS //////////////////////////// private static final String PREF_NAME = "pref_name"; public static final String IS_STARTUP = "is_startup"; //////////////////////////// /// FIELDS //////////////////////////// private SharedPreferences settings; @Override public void onWindowFocusChanged(boolean hasFocus) { settings = getSharedPreferences(PREF_NAME, MODE_PRIVATE); if (settings.getBoolean(IS_START_UP, true)) { test_text.startAnimation(test_anim); settings.edit().putBoolean(IS_START_UP, false).commit(); } } 的值为true。