2012-11-01 52 views
0

我正在使用cursoradapter從數據庫中獲取值並將其顯示在listview.I能夠顯示數據庫內容在ListView中也onclick perticular項目我正在獲取單擊Item.Now的價值現在我想要複選框也是這樣的oncheck我應該得到檢查項目的值(在這種情況下複選框點擊和listitemclick都應該工作)是否有可能?怎麼做?複選框與自定義列表視圖

private void displayListView() { 
     final 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.rownew, 
       cursor, 
       columns,  
       to); 
     View v = getLayoutInflater().inflate(R.layout.customdialog, null); 
     ListView listView = (ListView) v.findViewById(R.id.listChildren); 
     final EditText etChild = (EditText) v.findViewById(R.id.etChild); 
     listView.setAdapter(dataAdapter); 
     listView.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 
       name = ((TextView) view.findViewById(R.id.name)) .getText().toString(); 
       BirtDate = ((TextView) view.findViewById(R.id.birthdate)) .getText().toString(); 
       Log.d("*************", name); 

       Info=name+ " " +BirtDate; 
       Log.d("nameeeeeeeeeeeeeeee",Info); 
       etChild.setText(new StringBuilder().append(Info)); 
       topaste=etChild.getText().toString(); 
        etChild.setText(new StringBuilder().append(Info1)); 
       //     topaste1=etChild.getText().toString(); 
       //    } 


      } 
     }) 

我rownew.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:background="#FFFFFF" 
    android:gravity="center" 
    android:padding="6dp" > 

    <TextView 
     android:id="@+id/name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/text" 
     android:textStyle="bold" > 
    </TextView> 

    <TextView 
     android:id="@+id/birthdate" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dp" 
     android:text="@string/text" 
     android:textStyle="bold" > 
    </TextView> 


</LinearLayout> 

我Customdialog.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <EditText 
     android:id="@+id/etChild" 
     android:hint="" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" > 
    </EditText> 

    <ListView 
     android:id="@+id/listChildren" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#FFFFFF" > 
    </ListView> 
    </LinearLayout> 
+0

我在上面的類中充氣它View v = getLayoutInflater()。inflate(R.layout.customdialog,null); \t \t ListView listView =(ListView)v.findViewById(R.id.listChildren); – user1758835

+0

發佈您的自定義班級代碼或發佈完整的java代碼。 –

回答

0

你可以擴展SimpleCursorAdapter添加點擊監聽你的複選框。下面是我使用的代碼的片段:

public class MultiSelectCursorAdapter extends SimpleCursorAdapter { 

    public MultiSelectCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) { 
     super(context, layout, c, from, to, flags); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
    final int fposition = position; 
    View view = super.getView(position, convertView, parent); 
    CheckBox checkbox = (CheckBox) view.findViewById(R.id.list_checkbox); 
    checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      Log.d("CHECKER", "Something happened: " + fposition + " was clicked " + isChecked); 
      // TODO: store id of checked items in your model 
      } 

    }); 

    return view; 
    } 
} 

在你列表行佈局,你需要設置android:focusable="false"爲了讓列表項點擊各列表項的複選框,點擊。

<CheckBox 
     android:id="@+id/list_checkbox" 
     android:focusable="false" 
     ... > 
</CheckBox> 

最後,您需要一個模型或數據結構來存儲列表中的所有選中項,例如,一個數組或列表,存儲所有檢查項目的標識符。

+0

在我的主要課程中,我應該從哪裏編寫MultiSelectCursorAdapter – user1758835

+0

將此添加爲新類並簡單地將'SimpleCursorAdapter'替換爲'MultiSelectCursorAdapter'。您應該按照javadoc中的建議使用具有'flags'參數的構造函數。 –

+0

我是新來的這個概念,你可以給一些詳細的解決方案 – user1758835

相關問題