删除由自定义游标适配器填充的listview行

时间:2014-03-27 12:19:01

标签: android listview android-actionbar android-cursoradapter

我有一个自定义游标适配器CAdapter,其代码如下:

public class CAdapter extends CursorAdapter {

    public CAdapter(Context context, Cursor c) {
        super(context, c);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {

        TextView titleTV = (TextView) view.findViewById(R.id.listTitle);
        titleTV.setText("#"+cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1)))+" "+
                cursor.getString(cursor.getColumnIndex(cursor.getColumnName(2)))); 
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {

        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View retView = inflater.inflate(R.layout.row_item_layout, parent, false); 
        return retView;
    }

}

row_item_layout.xml文件是

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

    <TextView
        android:id="@+id/listTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="2dp"
        android:layout_toRightOf="@+id/heartIV"
        android:focusable="false"
        android:gravity="center_vertical"
        android:text="Example application"
        android:textSize="26sp" 
        android:paddingBottom="3dp"/>

    <ImageView
        android:id="@+id/heartIV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignTop="@+id/listTitle"
        android:src="@drawable/heart"
        android:paddingRight="5dp" 
        android:paddingLeft="2dp"/>

</RelativeLayout>

活动FavListActivity显示列表。它的布局文件是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.xkcdreader.FavListActivity"
    tools:ignore="MergeRootFrame" >

    <ListView
        android:id="@+id/favL"
        android:layout_width="match_parent"
        android:layout_height="452dp"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/headingTV" >

    </ListView>

    <TextView
        android:id="@+id/headingTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="8dp"
        android:text="Favourites"
        android:textSize="25dp" />

</RelativeLayout>

我想在选择行时显示包含删除选项的Actionbar(通过长按)。在按下删除按钮时,该行应该消失,数据库更新,并且列表应该相应地刷新。这该怎么做。我在网上搜索但找不到满意的答案。

1 个答案:

答案 0 :(得分:0)

尝试类似的东西:

Listview点击监听器

favL.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                General.deleteRecordID = DatabaseHelper.idArrayList.get(arg3);
                Log.i("Item Click", "Item Clicked " + arg3);
                itemClickDialog();
                return false;
            }
        });

警报对话框:

public void itemClickDialog() {

    AlertDialog.Builder builder = new AlertDialog.Builder(context);

    LayoutInflater inflater = LayoutInflater.from(context);
    View customView = inflater.inflate(R.layout.itemclick_dialogbox,
            (ViewGroup) findViewById(R.layout.activity_main));

    builder.setView(customView);

    final Dialog dialog = builder.create();

    dialog.show();
    TextView tvEdit = (TextView) dialog.findViewById(R.id.tvEdit);
    TextView tvPaidBill = (TextView) dialog.findViewById(R.id.tvPaidBill);
    TextView tvDeleteReminder = (TextView) dialog
            .findViewById(R.id.tvDeleteReminder);

    tvEdit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent intentAddItemActivity = new Intent(context,
                    AddItemActivity.class);
            startActivity(intentAddItemActivity);
            dialog.cancel();
            // Toast.makeText(context, "Clicked Edit", Toast.LENGTH_SHORT)
            // .show();
        }
    });

    tvPaidBill.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            // Toast.makeText(context, "Clicked Paid Bill",
            // Toast.LENGTH_SHORT)
            // .show();
        }
    });

    tvDeleteReminder.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            dbHelper.deleteRecordFromUpcomingBills(General.deleteRecordID);
            dialog.cancel();
            Intent intentUpcomingBillsActivity = new Intent(context,
                    UpcomingBillsActivity.class);
            startActivity(intentUpcomingBillsActivity);
            finish();
        }
    });

}

删除记录查询:

public void deleteRecordFromUpcomingBills(String deleteRecordID) {
    Log.i("deleteRecordID", deleteRecordID);

    SQLiteDatabase db = this.getWritableDatabase();

        String sqlQuery="DELETE FROM Upcoming_Bills WHERE ID="+ deleteRecordID;
    Log.e("sqlQuery", sqlQuery);
    Cursor cursor = db.rawQuery(sqlQuery, null);
    while (cursor.moveToNext()) {

    }
    db.close();

}