如何从onlongitemclick函数中删除数据库记录

时间:2014-03-19 06:05:41

标签: android

我在列表视图中打印了数据库值作为名称和电话号码。

我想删除onitemclick它也应该从数据库中删除。

这是我为委托数据库创建的方法是deleteContact。

我无法将其删除。

我该怎么做?

这是我的代码:

     // Deleting single contact
        public void deleteContact(Contact contact) {
            SQLiteDatabase db = this.getWritableDatabase();
            db.delete(TABLE_CONTACTS, KEY_ID + " = ?",
                    new String[] { String.valueOf(contact.getID()) });
            db.close();
        }



    public class MainActivity extends Activity {
        ArrayList<HashMap<String, String>> contactList;
        int id;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ListView list = (ListView) findViewById(R.id.listView1);

            contactList = new ArrayList<HashMap<String, String>>();
            DatabaseHandler db = new DatabaseHandler(this);

            db.addContact(new Contact("Ravi", "9100000000"));
            db.addContact(new Contact("Srinivas", "9199999999"));
            db.addContact(new Contact("Tommy", "9522222222"));
            db.addContact(new Contact("Karthik", "9533333333"));

            // Reading all contacts
            Log.d("Reading: ", "Reading all contacts..");
            List<Contact> contacts = db.getAllContacts();

            for (Contact cn : contacts) {
                String log = "Id: " + cn.getID() + " ,Name: " + cn.getName()
                        + " ,Phone: " + cn.getPhoneNumber();
                // Writing Contacts to log
                HashMap<String, String> contact = new HashMap<String, String>();

                contact.put("name", cn.getName());
                contact.put("phone", cn.getPhoneNumber());

                contactList.add(contact);
                Log.d("Name: ", log);

            }
            SimpleAdapter adapter = new SimpleAdapter(this, contactList,
                    R.layout.list_item, new String[] { "name", "phone" },
                    new int[] { R.id.name, R.id.mobile });
            list.setAdapter(adapter);

            list.setOnItemLongClickListener(new OnItemLongClickListener() {

                @Override
                public boolean onItemLongClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    Toast.makeText(MainActivity.this,
                            "Item in position " + position + " clicked",
                            Toast.LENGTH_LONG).show();
db.deleteContact(new Contact(position) );
                    // Return true to consume the click event. In this case the
                    // onListItemClick listener is not called anymore.

                    return true;
                }
            });

        }

    }

1 个答案:

答案 0 :(得分:0)

你好我在这里发布完整的代码在这长按点击会询问是否要删除?如果按是,则删除所选记录

listview.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                str_namedelete = listimage.get(arg2) + "";
                alertbox(str, "Are you sure to Remove Note ?");
                return false;
            }
        });



  protected void alertbox(String title, String mymessage) {

            AlertDialog dialog = new AlertDialog.Builder(this).setTitle(title)
                    .setPositiveButton(android.R.string.ok, this)
                    .setMessage(mymessage).show();
            TextView textView = (TextView) dialog
                    .findViewById(android.R.id.message);
            TextView textView1 = (TextView) dialog.findViewById(android.R.id.title);
            dialog.setCancelable(true);

            // textView.setTextSize(20);
            textView.setTypeface(typeface);
            // textView1.setTypeface(typeface);
        }


 @Override
        public void onClick(DialogInterface arg0, int arg1) {

            DataBaseHelper dbhelper = new DataBaseHelper(getApplicationContext());
            db = dbhelper.getWritableDatabase();

            String rawQuery = "UPDATE tablename" + " SET noteflag=0 , note = '" + ""+ "' where image = '" + str_namedelete + "' ";
            db.execSQL(rawQuery);
            db.close();
            onCreate(new Bundle());

        }