列表视图数据不断重复
Listview data keep duplicating
我正在尝试按照 this 教程学习如何在 SQLite 数据库中保留复选框状态。一切正常,但每次重新启动项目时 Listview
中的数据都会重复。如何防止这种情况发生?
Screenshot
适配器:
class CustomCursorAdapter extends CursorAdapter{
private SQLiteDatabase dh=DatabaseHelper.getInstance().getDb();
private LayoutInflater mInflater;
private Context mContext;
private Cursor cursor;
CustomCursorAdapter(Context context, Cursor c) {
super(context, c);
// TODO Auto-generated constructor stub
mInflater = LayoutInflater.from(context);
mContext = context;
cursor=c;
}
@Override
public void bindView(View view, Context context, final Cursor cursor) {
// TODO Auto-generated method stub
ViewHolder holder=(ViewHolder)view.getTag();
holder.setTextView((TextView)view.findViewById(R.id.textview));
holder.setCheckBox((CheckBox)view.findViewById(R.id.checkbox));
CheckBox cb=holder.getCheckBox();
holder.getTextView().setText( cursor.getString(cursor.getColumnIndex("username")));
cb.setTag(cursor.getPosition());
CompoundButton.OnCheckedChangeListener checkedChange= new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
ContentValues contentValues=new ContentValues();
Integer currentPosition = (Integer)buttonView.getTag();
String currentPositionString=Double.toString(currentPosition);
if(cursor.moveToPosition(currentPosition))
{
String rowID=cursor.getString(cursor.getColumnIndex("_id"));
if(isChecked){
contentValues.put("selected", "1");
dh.update(DatabaseHelper.USER_PASSWORD, contentValues, "_id=?", new String[]{rowID});
}else {
contentValues.put("selected", "0");
dh.update(DatabaseHelper.USER_PASSWORD, contentValues, "_id=?", new String[]{rowID});
}
}
}
};
cb.setOnCheckedChangeListener(checkedChange);
if(cursor.getString(cursor.getColumnIndex("selected")).compareTo("1")==0)
{
cb.setChecked(true);
}
else
{
cb.setChecked(false);
}
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
View convertView = mInflater.inflate(R.layout.custom, parent,false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
return convertView;
}
MainActivity.java:
public class MainActivity extends ListActivity{
Cursor cursor;
SQLiteDatabase dh;
CustomCursorAdapter myCursorAdapter;
ContentValues values;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidContext.setContext(this);
dh=DatabaseHelper.getInstance().getDb();
values = new ContentValues();
// Inserting some data in SQLite to populate list view
insertData("Avinash" , "123456");
insertData("Rakesh" , "qwerty");
insertData("Prateek", "onMobile");
insertData("Rajesh", "Symphony");
insertData("Rahul", "password123");
insertData("Kanishk", "_smi1234");
insertData("Ahmad", "asdfgh");
insertData("Akkie", "zxcvbn");
insertData("Ankur", "asdadd");
insertData("Rohit", "bigb");
insertData("Abhi", "rajjwe");
insertData("Sone", "qwerty");
createListView();
}
private void createListView() {
setContentView(R.layout.main);
cursor=dh.query(DatabaseHelper.USER_PASSWORD, new String[]{"_id","username","selected"}, null, null, null, null, "username ASC");
startManagingCursor(cursor);
myCursorAdapter= new CustomCursorAdapter(this,cursor);
this.getListView().setAdapter(myCursorAdapter);
}
private void insertData(String firstName ,String password){
if(values!= null){
values.clear();
}
if (values != null) {
values.put("username", firstName);
}
if (values != null) {
values.put("password", password);
}
dh.insert(DatabaseHelper.USER_PASSWORD, null, values);
}
public void clickHandler(View view){
if(view.getId() == R.id.checkbox){
cursor.requery(); /* to get the updated values from sqlite on changing the check of checkbox*/
}
}
您正在 activity 的 onCreate()
中插入新数据。
这应该只发生一次,在数据库创建之后。正确的位置是数据库打开助手的 onCreate()
.
我正在尝试按照 this 教程学习如何在 SQLite 数据库中保留复选框状态。一切正常,但每次重新启动项目时 Listview
中的数据都会重复。如何防止这种情况发生?
Screenshot
适配器:
class CustomCursorAdapter extends CursorAdapter{
private SQLiteDatabase dh=DatabaseHelper.getInstance().getDb();
private LayoutInflater mInflater;
private Context mContext;
private Cursor cursor;
CustomCursorAdapter(Context context, Cursor c) {
super(context, c);
// TODO Auto-generated constructor stub
mInflater = LayoutInflater.from(context);
mContext = context;
cursor=c;
}
@Override
public void bindView(View view, Context context, final Cursor cursor) {
// TODO Auto-generated method stub
ViewHolder holder=(ViewHolder)view.getTag();
holder.setTextView((TextView)view.findViewById(R.id.textview));
holder.setCheckBox((CheckBox)view.findViewById(R.id.checkbox));
CheckBox cb=holder.getCheckBox();
holder.getTextView().setText( cursor.getString(cursor.getColumnIndex("username")));
cb.setTag(cursor.getPosition());
CompoundButton.OnCheckedChangeListener checkedChange= new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
ContentValues contentValues=new ContentValues();
Integer currentPosition = (Integer)buttonView.getTag();
String currentPositionString=Double.toString(currentPosition);
if(cursor.moveToPosition(currentPosition))
{
String rowID=cursor.getString(cursor.getColumnIndex("_id"));
if(isChecked){
contentValues.put("selected", "1");
dh.update(DatabaseHelper.USER_PASSWORD, contentValues, "_id=?", new String[]{rowID});
}else {
contentValues.put("selected", "0");
dh.update(DatabaseHelper.USER_PASSWORD, contentValues, "_id=?", new String[]{rowID});
}
}
}
};
cb.setOnCheckedChangeListener(checkedChange);
if(cursor.getString(cursor.getColumnIndex("selected")).compareTo("1")==0)
{
cb.setChecked(true);
}
else
{
cb.setChecked(false);
}
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
View convertView = mInflater.inflate(R.layout.custom, parent,false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
return convertView;
}
MainActivity.java:
public class MainActivity extends ListActivity{
Cursor cursor;
SQLiteDatabase dh;
CustomCursorAdapter myCursorAdapter;
ContentValues values;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidContext.setContext(this);
dh=DatabaseHelper.getInstance().getDb();
values = new ContentValues();
// Inserting some data in SQLite to populate list view
insertData("Avinash" , "123456");
insertData("Rakesh" , "qwerty");
insertData("Prateek", "onMobile");
insertData("Rajesh", "Symphony");
insertData("Rahul", "password123");
insertData("Kanishk", "_smi1234");
insertData("Ahmad", "asdfgh");
insertData("Akkie", "zxcvbn");
insertData("Ankur", "asdadd");
insertData("Rohit", "bigb");
insertData("Abhi", "rajjwe");
insertData("Sone", "qwerty");
createListView();
}
private void createListView() {
setContentView(R.layout.main);
cursor=dh.query(DatabaseHelper.USER_PASSWORD, new String[]{"_id","username","selected"}, null, null, null, null, "username ASC");
startManagingCursor(cursor);
myCursorAdapter= new CustomCursorAdapter(this,cursor);
this.getListView().setAdapter(myCursorAdapter);
}
private void insertData(String firstName ,String password){
if(values!= null){
values.clear();
}
if (values != null) {
values.put("username", firstName);
}
if (values != null) {
values.put("password", password);
}
dh.insert(DatabaseHelper.USER_PASSWORD, null, values);
}
public void clickHandler(View view){
if(view.getId() == R.id.checkbox){
cursor.requery(); /* to get the updated values from sqlite on changing the check of checkbox*/
}
}
您正在 activity 的 onCreate()
中插入新数据。
这应该只发生一次,在数据库创建之后。正确的位置是数据库打开助手的 onCreate()
.