简体   繁体   English

Android Listview删除SQLite行使用ContextMenu

[英]Android Listview delete SQLite row use ContextMenu

I develop an app for android:p So i have a listview with a context menu. 我为android:p开发了一个应用程序,所以我有一个带有上下文菜单的listview。 In the contextmenu is a button for delete the row!So i saw many tutorials but no one can help me:/ 在contextmenu中是一个用于删除行的按钮!所以我看到了很多教程,但是没有人可以帮助我:/

MainActivity.java MainActivity.java

        lv = (ListView) findViewById(R.id.memberList_id);
            registerForContextMenu(lv);


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

        }

        @Override
        public boolean onContextItemSelected(MenuItem item) {
            AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
            switch(item.getItemId()) {
                case R.id.bearbeiten:

                    memI

D_tv = (TextView)findViewById(R.id.member_id);
                memName_tv = (TextView)findViewById(R.id.member_name);

                String memberID_val = memID_tv.getText().toString();
                String memberName_val = memName_tv.getText().toString();

                Intent modify_intent = new Intent(getApplicationContext(),
                                                  Modify_member.class);
                modify_intent.putExtra("memberName", memberName_val);
                modify_intent.putExtra("memberID", memberID_val);
                startActivity(modify_intent);

                return true;
            case R.id.löschen:

                dbcon.deleteData(member_id);

                return true;
            default:
                return super.onContextItemSelected(item);
        }
    }

DBhelper.java DBhelper.java

    package com.studiocrew.lehrstellekontrolle;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBhelper extends SQLiteOpenHelper {

    // TABLE INFORMATTION
        public static final String TABLE_MEMBER = "member";
        public static final String MEMBER_ID = "_id";
        public static final String MEMBER_NAME = "name";

        // DATABASE INFORMATION
        static final String DB_NAME = "MEMBER.DB";
        static final int DB_VERSION = 1;

    // TABLE CREATION STATEMENT
    private static final String CREATE_TABLE = "create table "
            + TABLE_MEMBER + "(" + MEMBER_ID
            + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + MEMBER_NAME + " TEXT NOT NULL);";

    public DBhelper(Context context) {
        super(context, DB_NAME, null,DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

        db.execSQL("DROP TABLE IF EXISTS " + TABLE_MEMBER);
        onCreate(db);
    }
}

You just need a reference to the adapter that is being used by the listview. 您只需要引用列表视图正在使用的适配器。 If you are having trouble getting a references to the object that is selected, you can create a custom adapter that will allow you to get the currently selected item. 如果在获取对所选对象的引用时遇到麻烦,则可以创建一个自定义适配器,该适配器将允许您获取当前选定的项目。 So you would have something like: 因此,您将获得以下内容:

adapter.remove(adapter.getSelectedItem());

Then you could use the same reference to that item to remove it from your database. 然后,您可以使用对该项目的相同引用将其从数据库中删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM