2012-10-26 125 views
1

我已經編寫了代碼以從數據庫獲取值並將該值綁定到listview.For爲此我現在根據需求使用了自定義列表視圖我希望複選框爲我的每個項目在list.How做爲了實現自定義列表視圖的複選框

image=(ImageView)findViewById(R.id.image); 
     note=(ImageButton)findViewById(R.id.note); 
     tick=(ImageButton)findViewById(R.id.tick); 
     cross=(ImageButton)findViewById(R.id.cross); 
     Intent intent = getIntent(); 
     Bitmap photo = (Bitmap) intent.getParcelableExtra("photooo"); 
     image.setImageBitmap(photo); 
     if(photo!=null) 
     { 
      dbHelper = new RecordsDbAdapter(this); 
      dbHelper.open(); 
      displayListView(); 
     } 

    } 
    private void displayListView() { 
     Cursor cursor = dbHelper.fetchAllRecords(); 
     String[] columns = new String[] { 
       RecordsDbAdapter.KEY_NAME, 
       RecordsDbAdapter.KEY_BIRTHDAY, 

     }; 
     int[] to = new int[] { 
       R.id.name, 
       R.id.birthdate, 
     }; 
     dataAdapter = new SimpleCursorAdapter(
       this, R.layout.row, 
       cursor, 
       columns, 
       to); 
     ListView listView = (ListView) findViewById(R.id.list); 
     listView.setAdapter(dataAdapter); 
    } 
} 

回答

1

像你以前去R.layout.row的XML文件,並把這段代碼完成的:

<CheckBox 
    android:id="@+id/checkbox" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true" 
    android:text="checkbox" /> 

,然後編輯你這樣的代碼: -

private void displayListView() { 
    Cursor cursor = dbHelper.fetchAllRecords(); 
    String[] columns = new String[] { 
      RecordsDbAdapter.KEY_NAME, 
      RecordsDbAdapter.KEY_BIRTHDAY, 
      RecordsDbAdapter.KEY_CHECKBOX 

    }; 
    int[] to = new int[] { 
      R.id.name, 
      R.id.birthdate, 
      R.id.checkbox 
    }; 
    dataAdapter = new SimpleCursorAdapter(
      this, R.layout.row, 
      cursor, 
      columns, 
      to); 
    ListView listView = (ListView) findViewById(R.id.list); 
    listView.setAdapter(dataAdapter); 
} 
5
  • 使用LayoutInflater添加複選框使用SetTag梅索德後到列表視圖
  • 添加Tagvalue到複選框
  • 這tagvalue獨立的複選框中的每一項在列表視圖 下面的代碼示例:`

LayoutInflater inflater = getLayoutInflater();

convertView = inflater.inflate(R.layout.home1, null); 

ViewHolder holder1 = new ViewHolder(); holder1.text =(TextView)convertView.findViewById(R.id.textView1);

holder1.ch=(CheckBox)convertView.findViewById(R.id.checkBox1); 
    holder1.ch .setTag(position); 
相關問題