在我的应用程序中,我使用它来实时计数选中的复选框,这意味着当选中该复选框时,以上计数将增加或减少。但是当向下滚动列表视图时,将取消选中该复选框。我的代码中有任何建议或问题吗?
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;
}
}
答案 0 :(得分:2)
在您的适配器对象中添加一个布尔参数,用于检查和取消检查。默认情况下,为列表中的每个项目设置false,如果选中,则在适配器中设置为true,然后调用notifyDataSetChanged()。
使用模型类
public class ContactModel {
String phone,name;
boolean sel;
public ContactModel(String phone, String name, boolean sel) {
this.phone = phone;
this.name = name;
this.sel = sel;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isSel() {
return sel;
}
public void setSel(boolean sel) {
this.sel = sel;
}
我的自定义适配器
public class ContactADAPTER extends BaseAdapter {
String phone,name;
boolean sel;
Activity act;
List<ContactModel> contactModels;
public ContactADAPTER(Activity act, List<ContactModel> contactModels) {
this.act = act;
this.contactModels = contactModels;
}
@Override
public int getCount() {
return contactModels.size();
}
@Override
public Object getItem(int i) {
return contactModels.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater inflater=LayoutInflater.from(context);
view=inflater.inflate(R.layout.phone_list_item,viewGroup,false);
TextView phone1= (TextView) view.findViewById(R.id.phone1);
TextView name1= (TextView) view.findViewById(R.id.name1);
CheckBox tick= (CheckBox) view.findViewById(R.id.tick);
phone1.setText(contactModels.get(i).getPhone());
name1.setText(contactModels.get(i).getName());
if(contactModels.get(i).isSel())
{
tick.setSelected(true);
}
else
{
tick.setSelected(true);
}
tick.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
contactModels.get(i).setSel(isChecked);
notifyDataSetChanged();
//for getting ticked count
int count=0;
for(ContactModel c:contactModels)
{
if(c.isSel())
{
count++;
}
}
// show count
act.getActionBar().setTitle(String.valueOf(count));
}
});
return view;
}
}
活动中
List<ContactModel> cmodelList= new ArrayList<>();
cmodelList.add(new ContactModel(phonenumber, name, false));
cmodelList.add(new ContactModel(phonenumber2, name2, false));
cmodelList.add(new ContactModel(phonenumber3, name3, false));
ContactADAPTER contactAdapter=new ContactADAPTER(Phone_Contact_List.this,cmodelList);
listView.setAdapter(contactList);
答案 1 :(得分:1)
我猜问题出在不使用holder
像@Athira所说的那样制作bean
和adapter
然后在getView的适配器内部尝试
@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;
}
}
答案 2 :(得分:0)
您必须维护所选复选框的列表,然后根据该列表$ rpm -q --requires python3-setuptools | grep python
/usr/bin/python3
python(abi) = 3.6
$ rpm -q --requires python2-setuptools | grep python
/usr/bin/python2
python(abi) = 2.7
或function keyLoop() {
let content = vm.comparisonThumbnail.list;
let receiver = [];
let courseCounter = 0;
content.forEach(a => {
let cid = a.courseContentId;
if (cid == undefined) {
cid = ' ';
}
vm.loopKey = cid;
var pass = vm.loopKey;
receiver[courseCounter] = pass;
courseCounter++;
});
console.log(receiver);
}
保留复选框。
因此,假设您有set
列表,其中包含选定复选框的列表。
unset
所以您可以做的是:
selectedcheckBox
然后在适配器中填充listView时,请使用ArrayList<> selectedcheckBox = new ArrayList<>();
列表进行检查。
如果在列表中设置了值,则lstdept.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (view != null) {
selectedcheckBox.add(//Add item to the list)
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);
}
});
复选框为selectedcheckBox
。
之所以会这样,是因为listView在移出视图并再次变得可见时会回收自身。因此,所有值都将再次设置为可见。因此,应该保持适当的检查以扩大视图的位置。
答案 3 :(得分:0)
您的适配器将具有一个“ getView()”方法,您将完成填充单元格数据的所有工作,使用getItem(position)获取该项,然后在该getView()中更新单元格中的所有视图方法