单击以删除列表视图中的数据库

时间:2016-11-26 05:04:33

标签: android listview android-studio sql-delete

这是我的列表视图活动。我想删除列表视图中的特定项目,而不是删除表格中的所有项目。

我搜索过类似的问题,但我发现的所有解决方案都无法帮助我。我希望你们能提供帮助。

package com.example.asus.dogfeeder;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class ScheduleListActivity extends Activity {

    ListView listView;
    SQLiteDatabase sqLiteDatabase;
    SchedDbHelper schedDbHelper;
    Cursor cursor;
    ListDataAdapter listDataAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_schedule_list);

        listView =(ListView) findViewById(R.id.ListView);
        listDataAdapter = new ListDataAdapter(getApplicationContext(), R.layout.row_layout);
        listView.setAdapter(listDataAdapter);
        schedDbHelper = new SchedDbHelper(getApplicationContext());
        sqLiteDatabase = schedDbHelper.getReadableDatabase();
        cursor = schedDbHelper.getInformations(sqLiteDatabase);

        if(cursor.moveToFirst())
        {
            do{

                String day,time,amount;
                day = cursor.getString(0);
                time = cursor.getString(1);
                amount = cursor.getString(2);
                DataProvider dataProvider = new DataProvider(day,time,amount);
                listDataAdapter.add(dataProvider);
                registerForContextMenu(listView);

            }while(cursor.moveToNext());
        }
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_context_menu, menu);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        switch (item.getItemId())
        {
            case R.id.delete_id:
                sqLiteDatabase.delete(ScheduleContract.NewSchedInfo.TABLE_NAME,null,null);
                listDataAdapter.notifyDataSetChanged();
                Toast.makeText(getBaseContext(), "Deleted", Toast.LENGTH_SHORT).show();
                return true;
            default:
                return super.onContextItemSelected(item);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

做这样的事情

在SqLiteDatabase中创建删除方法,如下所示

 public void deleteRow(String id) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.execSQL("DELETE FROM " + TABLE_NAME + " WHERE " + KEY_ID + "='" + id + "'");
}

在删除按钮时调用此方法,如

sqLiteDatabase.deleteRow(id); // Pass id here and re-set adapter to listview

你的问题解决了......