2016-02-02 15 views
5

Tôi đang cố gắng triển khai danh sách xem có hộp kiểm để khi hộp kiểm được chọn, hàng danh sách sẽ bị xóa. Tôi đang populating listview với một con trỏ và điều này đang làm việc tốt và hộp kiểm đang được hiển thị.Android ListView Với hộp kiểm xóa/nhận ID hàng

Điều tôi đang gặp phải là tìm hiểu cách nhận _id của hàng của ô đã được chọn.

bất cứ ai có thể chỉ cho tôi làm thế nào để thực hiện một cái gì đó giống như

ListView này và CheckBox

 Cursor cursor = db.getAllItems(); 

    //String[] columns = new String[] {db.KEY_NAME, db.KEY_CODE, db.KEY_ROWID}; 
    String[] columns = new String[] {db.KEY_ITEM_NAME, db.KEY_MEASUREMENT, db.KEY_UNIT}; 

    int[] to = new int[] {R.id.ingredientName, R.id.ingredientMeasurement, R.id.ingredientUnit}; 

    final SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(this,R.layout.row4, cursor, columns, to, 0); 

    final ListView shoppingList = (ListView) findViewById(R.id.shoppingList); 
    shoppingList.setAdapter(myCursorAdapter); 


    CheckBox deleteCheck = (CheckBox)findViewById(R.id.checkBox1); 

    deleteCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(RadioGroup group, int checkedId) { 
      if (isChecked){ 
       // How do I get the list item clicked to delete row? 
      } 
     } 

    }); 

XML - Row.4.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="horizontal" 
android:weightSum="1"> 

<TextView 
    android:id="@+id/ingredientName" 
    android:layout_width="wrap_content" 
    android:textColor="#000000" 
    android:layout_height="wrap_content" 
    android:padding="5dp" 
    android:hint="wewewe"/> 

<TextView 
    android:id="@+id/ingredientMeasurement" 
    android:textColor="#000000" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@+id/ingredientName" 
    android:padding="5dp" 
    android:hint="fefefef"/> 

<TextView 
    android:id="@+id/ingredientUnit" 
    android:textColor="#000000" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@+id/ingredientMeasurement" 
    android:padding="5dp" 
    android:hint="qqqqq"/> 


<CheckBox 
    android:id="@+id/checkBox1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="right" 
    android:background="#fff" 
    android:text=""/> 
</LinearLayout> 
+0

tốt, bạn có thể tạo hai phương pháp để xóa các mục khỏi listview, một trong bộ điều hợp và thứ hai trong cơ sở dữ liệu. 'adapter.remove (item.getId (checkedId));' và gọi phương thức thứ hai từ cơ sở dữ liệu 'dataBase.remove (item.getId (checkedId));' Đó là cách bạn sẽ xóa mục khỏi adapter và cơ sở dữ liệu. –

+0

Cảm ơn bạn đã trả lời, chỉ một câu hỏi, hàm 'getId', điều này có nhận được' id' của hàng hoặc '_id' thực trong cơ sở dữ liệu không? – JJSmith

+0

'getId' sẽ nhận được id được chỉ định của đối tượng bạn đang chọn trong listView. –

Trả lời

4

Sử dụng con trỏ tùy chỉnh này adapter và xử lý onCheckedChanged ở đây trong bộ điều hợp này.

import android.content.Context; 
import android.database.Cursor; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.CheckBox; 
import android.widget.CompoundButton; 
import android.widget.SimpleCursorAdapter; 

public class CustomAdapter extends SimpleCursorAdapter implements CompoundButton.OnCheckedChangeListener { 

    private Context mContext; 
    private Context appContext; 
    private int layout; 
    private Cursor cr; 
    private final LayoutInflater inflater; 

    public CustomAdapter(Context context, int layout, Cursor c, String[] from, int[] to) { 
     super(context, layout, c, from, to); 
     this.layout = layout; 
     this.mContext = context; 
     this.inflater = LayoutInflater.from(context); 
     this.cr = c; 
    } 

    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
     return inflater.inflate(layout, null); 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 
     super.bindView(view, context, cursor); 
     CheckBox checkBox1 = (CheckBox) view.findViewById(R.id.checkBox1); 
     checkBox1.setOnCheckedChangeListener(this); 
     int columnIndex = cursor.getColumnIndex(your column here); 
     int columnvalue = cursor.getInt(columnIndex); 
     checkBox1.setTag(columnvalue); 

    } 

    @Override 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      int id = (Integer) buttonView.getTag(); 
    } 
} 
+0

Cảm ơn bạn đã trả lời, tôi hơi bối rối về cách thực hiện điều này trong hoạt động danh sách? – JJSmith

+0

bạn đang sử dụng SimpleCursorAdapter chỉ cần thay thế nó bằng CustomAdapter này, tôi hy vọng nó sẽ hoạt động. – Master

+0

Cũng đặt giá trị cho componentName, componentMeasurement và ingredientsUnit trong phương thức bindView và columnValue sẽ là id mà bạn muốn nhận khi hộp kiểm được chọn. – Master

Các vấn đề liên quan