I'm experiancing a problem with CheckBox.It changes it's state while scrolling. After looking for a while for its solution I found Android: CursorAdapter, ListView and CheckBox where getView is preferred to bindView for CursorAdapters' optimization .So I thought of applying it in my case.So here is my code:
package com.example.ki;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
DBAdapter myDb;
ListView myList;
Cursor c;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
openDB();
display();
insert();
}
private void insert() {
myDb.insertRow("0",false);
myDb.insertRow("1",true);
myDb.insertRow("2",true);
myDb.insertRow("3",false);
myDb.insertRow("4",false);
myDb.insertRow("5",false);
myDb.insertRow("6",false);
myDb.insertRow("7",true);
myDb.insertRow("8",false);
myDb.insertRow("9",false);
myDb.insertRow("10",false);
myDb.insertRow("11",false);
myDb.insertRow("12",false);
myDb.insertRow("13",false);
myDb.insertRow("14",false);
myDb.insertRow("15",false);
myDb.insertRow("16",false);
myDb.insertRow("17",false);
myDb.insertRow("18",false);
myDb.insertRow("19",false);
myDb.insertRow("20",false);
myDb.insertRow("21",false);
myDb.insertRow("22",true);
Toast.makeText(this, "insett(); method called...", Toast.LENGTH_LONG).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
closeDB();
}
private void openDB() {
myDb = new DBAdapter(this, null, false);
myDb.open();
}
private void closeDB() {
myDb.close();
}
// Display ListView with CheckBoxes
private void display() {
c = myDb.getAllRows();
String[] fromFieldNames = new String[]{DBAdapter.KEY_NAME};
int[] toViewIDs = new int[]{R.id.textsv};
SimpleCursorAdapter myCursorAdapter;
myCursorAdapter= new SimpleCursorAdapter(getBaseContext(),R.layout.item_layoutt, c , fromFieldNames , toViewIDs ,0);
final ListView myList = (ListView) findViewById(R.id.listView1);
myList.setAdapter(myCursorAdapter);
}
public class MyDataAdapter extends SimpleCursorAdapter {
private Context context;
private ArrayList<String> myList = new ArrayList<String>();
private ArrayList<Boolean> itemChecked = new ArrayList<Boolean>();
private Cursor cur;
// itemChecked will store the position of the checked items.
public MyDataAdapter(Context context, int layout, Cursor c, String[] from,
int[] to,int k) {
super(context, layout, c, from, to, k);
this.cur = c;
this.context = context;
for (int i = 0; i < this.getCount(); i++) {
itemChecked.add(i, false); // initializes all items value with false
}
}
@Override
public View getView(final int pos, View inView, ViewGroup parent) {
if (inView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inView = inflater.inflate(R.layout.item_layoutt, null);
}
final CheckBox cBox = (CheckBox) inView.findViewById(R.id.textcb); // your
// CheckBox
cBox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v.findViewById(R.id.textcb);
if (cb.isChecked()) {
itemChecked.set(pos, true);
// do some operations here
} else if (!cb.isChecked()) {
itemChecked.set(pos, false);
// do some operations here
}
}
});
cBox.setChecked(itemChecked.get(pos)); // this will Check or Uncheck the
// CheckBox in ListView
// according to their original
// position and CheckBox never
// loss his State when you
// Scroll the List Items.
return inView;
}}
}
Problems :
1. What's the use of private ArrayList myList = new ArrayList(); ?
2. private Cursor cur; isn't used. It shows error in Eclipse: The value of the field MainActivity.MyDataAdapter.cur is not used.
3. Why are there two CheckBoxes
final CheckBox cBox = (CheckBox) inView.findViewById(R.id.textcb); // your // CheckBox
andCheckBox cb = (CheckBox) v.findViewById(R.id.textcb);
called ?I don't to how to make this code work (I 'm new to Android).
Aucun commentaire:
Enregistrer un commentaire