CheckBox将取消选中,并在向下滚动列表视图时进行检查 [英] CheckBox will uncheck and check when scrolling down the listview

查看:138
本文介绍了CheckBox将取消选中,并在向下滚动列表视图时进行检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我使用它来实时计数选中的复选框,这意味着在勾选该复选框时,上面的计数将增加或减少.但是当向下滚动列表视图时,将取消选中该复选框.我的代码中有任何建议或问题吗?

In my application I use this to count the checked checkbox in real time meaning when tick the box the count above will increase or decrease. but when scrolling down the listview the checked box will be uncheck. any suggestion or problem in my codes?

MainActivity

public class Main2Activity extends AppCompatActivity {
    ListView lstdept;
    CheckBox list_view_item_checkbox;
    SimpleAdapter ADAhere;
    Connection con;
    String un, pass, db, ip, z,country;
    int test = 0;
    ArrayList<String> selectedItems = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        country = getIntent().getStringExtra("country");
        SelectRes(country);
          lstdept.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

        lstdept.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (view != null) {
                    CheckBox checkBox = (CheckBox) view.findViewById(R.id.list_view_item_checkbox);
                    checkBox.setChecked(!checkBox.isChecked());
                    if (!checkBox.isChecked()) {
                        test = test - 1 ;
                    } else {
                        test = test + 1 ;
                    }
                }
               getSupportActionBar().setTitle(country + "              Total Count: " + lstdept.getCount()+"       " + test);
            }

        });

    }

用于填充列表视图

void SelectRes(String dept) {
        ip = "172.18.130.19";
        db = "DTRSPH";
        un = "moreface";
        pass = "moreface1234";
        try {
            con = connectionclass(un, pass, db, ip);        // Connect to database
            if (con == null) {
                toast.makeText(this,"Check Your Internet Access!",Toast.LENGTH_LONG).show();
            } else {
                String query = "SELECT FULLNAME FROM tblEPR where DEPT ='"+ dept +"'";
                Statement stmt = con.createStatement();
                ResultSet rs = stmt.executeQuery(query);
                List<Map<String, String>> data = null;
                data = new ArrayList<Map<String, String>>();

                while (rs.next()) {
                    Map<String, String> datanum = new HashMap<String, String>();
                    datanum.put("A", rs.getString("FULLNAME"));
                    data.add(datanum);
                }

                String[] fromwhere = {"A"};
                int[] viewswhere = {R.id.lblDept};
                ADAhere = new SimpleAdapter(Main2Activity.this, data,
                        R.layout.list_emp, fromwhere, viewswhere);
                lstdept = (ListView) findViewById(R.id.lstemployee);
                lstdept.setAdapter(ADAhere);
                con.close();
                getSupportActionBar().setTitle(dept + "              Total Count: " + lstdept.getCount());
            }
        } catch (Exception ex) {
            z = ex.getMessage();
        }
    }

与数据库的连接

@SuppressLint("NewApi")
    public Connection connectionclass(String user, String password, String database, String server) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        Connection connection = null;
        String ConnectionURL = null;
        try {
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            ConnectionURL = "jdbc:jtds:sqlserver://" + server + ";databaseName=" + database + ";user=" + user + ";password=" + password + ";";
            connection = DriverManager.getConnection(ConnectionURL);
        } catch (SQLException se) {
            Log.e("error here 1 : ", se.getMessage());
        } catch (ClassNotFoundException e) {
            Log.e("error here 2 : ", e.getMessage());
        } catch (Exception e) {
            Log.e("error here 3 : ", e.getMessage());
        }
        return connection;
    }
}

推荐答案

我猜问题出在不使用holder
像@Athira所说的那样制作beanadapter
然后在getView的适配器内部尝试

I guess the problem is not using holder
make a bean and adapter like @Athira says
then inside adapter in getView try this

@Override
        public View getView(int i, View view, ViewGroup viewGroup) {
   ViewHolder holder = null;

 if (view == null) {
            LayoutInflater inflater=LayoutInflater.from(context);
            view=inflater.inflate(R.layout.phone_list_item,viewGroup,false);
            holder.phone1= (TextView) view.findViewById(R.id.phone1);
            holder.name1= (TextView) view.findViewById(R.id.name1);
            holder.tick= (CheckBox) view.findViewById(R.id.tick);
 view.setTag(holder);
}else{
            holder = (ViewHolder) view.getTag();
        }
           holder.phone1.setText(contactModels.get(i).getPhone());
           holder.name1.setText(contactModels.get(i).getName());
            if(contactModels.get(i).isSel())
            {
                holder.tick.setSelected(true);
            }
            else
            {
                holder.tick.setSelected(true);
            }
     holder.tick.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    contactModels.get(i).setSel(isChecked);
                    notifyDataSetChanged();
                }
            });
            return view;
        }
 public class ViewHolder {
        TextView phone1,name1;
CheckBox tick;

    }
    }

这篇关于CheckBox将取消选中,并在向下滚动列表视图时进行检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆