2013-06-17 3 views
0

SQLite의 목록보기에 체크 박스가 있습니다 .. 모두 정상적으로 작동하지만 하나 이상의 체크 박스를 클릭하고 상태를 변경하십시오. 체크 박스 상태 (목록보기는 처음 생성) 원래 값안드로이드를 스크롤 할 때 listview의 체크 박스가 상태를 저장하지 않습니다.

여기

내 코드로 다시 변경 .. 당신이 그 해결 방법을 말해 줄 수 있기를 바랍니다 : ListView에이 내부

 public void showSQL(){ 
       /* 
     * Open the same SQLite database 
     * and read all it's content. 
     */ 
    mySQLiteAdapter = new SQLiteAdapter(this); 
    mySQLiteAdapter.openToRead(); 

    Cursor cursor = mySQLiteAdapter.queueAll(); 
    startManagingCursor(cursor); 

    from = new String[]{SQLiteAdapter.NUMBER_CONTENT}; 



    to = new int[]{R.id.text}; 

    SimpleCursorAdapter cursorAdapter = 
      new SimpleCursorAdapter(this, R.layout.row, cursor, from , to); 

    cursorAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder(){ 
     public boolean setViewValue(View view, Cursor cursor, int columnIndex) { 
      String number ; 
      String is_star ; 
      if (columnIndex == cursor.getColumnIndex("numbers")) { 
       // If the column is IS_STAR then we use custom view. 
       number = cursor.getString(1); 
       is_star = cursor.getString(cursor.getColumnIndex("status")); 

       if (is_star.equals("true")) { 
        // set the visibility of the view to GONE 
        ((CheckBox) view).setText("  " + number); 
        ((CheckBox) view).setChecked(true); 
        return true; 
       }else{ 
        ((CheckBox) view).setText("  " + number); 
        ((CheckBox) view).setChecked(false); 
        return true; 
       } 
       //return true; 

      } 
      return false; 

     } 

    }); 
    stopManagingCursor(cursor); 


    listContent.setAdapter(cursorAdapter); 

    listContent.setOnItemClickListener(listContentOnItemClickListener); 

    mySQLiteAdapter.getAllNumbers(); 

    mySQLiteAdapter.close(); 

} 


private ListView.OnItemClickListener listContentOnItemClickListener 
     = new ListView.OnItemClickListener(){ 

    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, 
          long id) { 
     // TODO Auto-generated method stub 
     Cursor cursor = (Cursor) parent.getItemAtPosition(position); 
     int item_id = cursor.getInt(cursor.getColumnIndex(SQLiteAdapter.NUMBER_ID)); 
     String item_content1 = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.NUMBER_CONTENT)); 

     mySQLiteAdapter = new SQLiteAdapter(getBaseContext()); 
     item = (CheckBox) view; 

     mySQLiteAdapter.openToRead(); 
     String check = null; 
     Cursor c = mySQLiteAdapter.getNumberStatus(item_id); 
     if (c.moveToFirst()){ 
      check = c.getString(c.getColumnIndex(SQLiteAdapter.NUMBER_STATUS)); 
     } 
     mySQLiteAdapter.close(); 



     //The change color logic is here! 
     if(item.isChecked()) { 
      mySQLiteAdapter.openToWrite(); 
      mySQLiteAdapter.updateNumberStatus(item_id,"false"); 
      mySQLiteAdapter.close(); 
      // Toast.makeText(MainActivity.this, "deleted" +" "+ check, Toast.LENGTH_LONG).show(); 
      item.setChecked(false); 

     } 
     else { 
      mySQLiteAdapter.openToWrite(); 
      mySQLiteAdapter.updateNumberStatus(item_id,"true"); 
      mySQLiteAdapter.close(); 
      // Toast.makeText(MainActivity.this, item_content1 +" "+ check , Toast.LENGTH_LONG).show(); 
      item.setChecked(true); 


     } 





    }}; 
+0

시도해주세요. http://stackoverflow.com/questions/16685366/customised-listview-using-arrayadapter-class-in-android/16686623#16686623 – Raghunandan

답변

2

문제를 체크 박스 뷰가 재사용됨에 따라 뷰가 재활용 됨.

하려면 CheckBox의 상태를 유지하려면 Checkbox의 상태를 저장할 수 있어야합니다.

this 링크를 참조하십시오. 여기에 설명 된 아주 좋은 예입니다.

Link 1

+0

나는 그것을 할 수 없습니다. 그것은 나를 위해 조금 어렵습니다 (나는 초급자입니다.) 제 코드를 편집 할 수 있습니까? :) –

+0

@AndroidLearner 태그를 지정하고 변경 리스너를 설정하기 전에 확인란 상태를 설정하고있었습니다. 첫 번째 링크는 문제를 해결하는 데 도움이되었지만 여전히 잘못되었다는 것은 전혀 알 수 없습니다. 유용한 게시물 주셔서 감사합니다. :) – Sufian

+0

@Ibrahim Al-Eidan 당신이 코드를 부여 받았고, 그것을 실행하고 잘못 된 곳을 보았습니다. 포기하지 않는 자만 IT 분야에서 살아남을 수 있습니다. – Sufian

0

당신은 클래스가 얻을 체크 박스의 상태를 설정해야한다. 확인란의 상태를 저장하기 위해 arraylist로 사용됩니다. 체크 된 항목을 표시하는 데 사용하십시오.

+0

하지만 어떻게 할 수 있습니까 ?? –

+0

http://rdcworld-android.blogspot.in/2013/01/listview-with-checkbox-using.html – abhi

+0

나는 그것을 할 수 없었다 :( –

관련 문제