显示来自远程服务器的随机复选框,如果用户选中了复选框,则将该值存储在sqlite数据库中,并在下次加载时检查数据库中是否存在该值,如果存在,则默认情况下将检查该值复选框。我的问题是每当我尝试向sqlite插入一个值时,它总是采用第一个选中的复选框值。但是,如果我使用toast来检查我的代码,我会得到相应的复选框值。但这不适用于sqlite
以下是如何在clicklistener上显示复选框和设置
rl = (LinearLayout) getView().findViewById(R.id.linearmain);
HashMap<String, String> resultp = new HashMap<String, String>();
sqlcon = new SQLController(context);
sqlcon.open();
CheckBox[] cb = new CheckBox[arraylist.size()];
Cursor c = sqlcon.readEntry();
int rows = c.getCount();
int cols = c.getColumnCount();
for(int i = 0; i < arraylist.size(); i++) {
resultp = arraylist.get(i);
cb[i] = new CheckBox(getActivity());
cb[i].setText(resultp.get(Fltrsubfragment.SUB));
cb[i].setId(i);
cb[i].setOnClickListener(handleOnClick(cb[i]));
rl.addView(cb[i]);
for ( int ikv = 0; ikv < rows; ikv++) {
// inner for loop
for (int j = 0; j < cols; j++) {
String iv;
iv=c.getString(j);
if(iv==null){
Toast.makeText(context, " Empty " + rows, Toast.LENGTH_LONG).show();
}
else if(iv.equals(cb[i].getText().toString())){
cb[i].setChecked(true);
Toast.makeText(context, " Checked " + rows, Toast.LENGTH_LONG).show();
}
else{
cb[i].setChecked(false);
}
}
}
View.OnClickListener handleOnClick(final CheckBox button) {
return new View.OnClickListener() {
public void onClick(View v) {
if(button.isChecked()){
if(barraylist.contains(button.getText().toString())){
Toast.makeText(context, " Already added " + button.getText().toString(), Toast.LENGTH_LONG).show();
}
else {
brandarraylist.add(button.getText().toString());
name=button.getText().toString();
new MyAsync().execute();
Toast.makeText(context, " Stored " + button.getText().toString(), Toast.LENGTH_LONG).show();
}
}
else{
if(barraylist.contains(button.getText().toString()))
{
barraylist.remove(button.getText().toString());
name=button.getText().toString();
new MyAsyncS().execute();
sqlcon.deleteTData(button.getText().toString());
Toast.makeText(context, "Removed this " + button.getText().toString(), Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(context, "Unchecked this " + button.getText().toString(), Toast.LENGTH_LONG).show();
}
}
}
};
插入值的AsyncTask代码
private class MyAsync extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
PD = new ProgressDialog(context);
PD.setMessage("Loading...");
PD.setCancelable(false);
PD.show();
}
@Override
protected Void doInBackground(Void... params) {
if(name==null){
return null;
}
// inserting data
else{
sqlcon = new SQLController(context);
sqlcon.open();
sqlcon.insertData(name);
sqlcon.close();
// BuildTable();
return null;
}
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
PD.dismiss();
}
}
这里是insertData代码
public void insertData(String name) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(MyDbHelper.LT_VAL, name);
database.insert(MyDbHelper.LTE, null, cv);
}
请建议在哪里犯错误。
答案 0 :(得分:0)
在new MyAsync().execute();
内置name
:
我建议在所有用户输入上使用.trim()
。您可以轻松地将其链接到:
name=button.getText().toString().trim();
new MyAsync().execute(name);
然后使用:
获取它 @Override
protected String doInBackground(String... params) {
String name = params[0];
我们通过将代码更改为:
来排除上下文@Override
protected String doInBackground(String ... params) {
String name = params[0];
if(name==null){
return null;
}else{
return name;
}
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
sqlcon = new SQLController(context);
sqlcon.open();
sqlcon.insertData(result);
sqlcon.close();
// BuildTable();
PD.dismiss();
}