В моем приложении я использую это для подсчета отмеченного флажка в реальном времени, означающего, что когда флажок установлен, счет выше увеличится или уменьшится.но при прокрутке вниз по списку установленный флажок будет снят.какие-либо предложения или проблемы в моих кодах?
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;
}
}