2017-05-08 1 views
0

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*/ 
    } 
} 
+0

어댑터 클래스를 게시 할 수 있습니까 –

+0

두 번 저장 했습니까? –

+0

여기에 활동 클래스를 입력하십시오 ... –

답변

0

당신은 활동의 onCreate() 새로운 데이터를 삽입한다.

데이터베이스를 만든 후에이 작업을 한 번만 수행해야합니다. 올바른 위치는 데이터베이스 열기 도우미 onCreate()입니다.

-1

나는 이미지, 텍스트와 체크 박스와 어댑터를 가지고있다. 이 코드를 참조하고 오류를 수정하십시오.

사용이 어댑터 :

public class CLVCustomAdapter extends BaseAdapter { 

private Context myContext; 
private CLVListPojo myList; 
private ArrayList<CLVListPojo> myData; 
private boolean[] aBoolean; 


public CLVCustomAdapter(Context context, ArrayList<CLVListPojo> aData) { 
    this.myContext = context; 
    this.myData = aData; 
    aBoolean = new boolean[aData.size()]; 
} 

@Override 
public int getCount() { 
    if (myData.size() <= 0) 
     return 1; 
    return myData.size(); 
} 

@Override 
public CLVListPojo getItem(int i) { 
    return myData.get(i); 
} 

@Override 
public long getItemId(int i) { 
    return 0; 
} 

/** 
* @param position 
* @param convertView 
* @param parent 
* @return 
*/ 
@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 

    ViewHolder aViewHolder; 

    aViewHolder = new ViewHolder(); 
    LayoutInflater aLayoutInflater = (LayoutInflater) myContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
    convertView = aLayoutInflater.inflate(R.layout.custom_listview, null); 

    aViewHolder.aImageView = (ImageView) convertView.findViewById(R.id.custom_listview_IMG); 
    aViewHolder.aTextView = (TextView) convertView.findViewById(R.id.custom_listview_TXT); 
    aViewHolder.aCheckBox = (CheckBox) convertView.findViewById(R.id.custom_listview_CHECKBOX); 
    convertView.setTag(aViewHolder); 


    if (aBoolean[position]) { 
     aViewHolder.aCheckBox.setChecked(true); 
    } else { 
     aViewHolder.aCheckBox.setChecked(false); 

    } 

    aViewHolder.aCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton compoundButton, boolean b) { 
      if (b) { 
       aBoolean[position] = true; 
      } else { 
       aBoolean[position] = false; 

      } 

      notifyDataSetChanged(); 
     } 
    }); 


    myList = getItem(position); 
    Picasso.with(myContext).load(myList.getUrl()).into(aViewHolder.aImageView); 


    aViewHolder.aTextView.setText(myList.getUser()); 

    return convertView; 
} 


public ArrayList<String> getSelectedValues() { 
    ArrayList<String> aSelecList = new ArrayList<>(); 

    for (int i = 0; i < aBoolean.length; i++) { 
     if (aBoolean[i]) { 
      aSelecList.add(getItem(i).getUser()); 
     } 

    } 
    return aSelecList; 
} 

public class ViewHolder { 
    ImageView aImageView; 
    TextView aTextView; 
    CheckBox aCheckBox; 
} 
} 
+0

@ nbokmans를 열 때마다 반복적으로 코드를 유지할 수 없다. 그래서 예제 코드가 추가되었습니다. 라이트? –

관련 문제