2014-03-19 104 views
0

我在列表视图中打印了数据库值作为名称和电话号码。如何从onlongitemclick函数中删除数据库记录

我想删除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; 
       } 
      }); 

     } 

    } 

回答

0

你好,我是张贴完整的代码在此onlong点击会问,如果你想删除或者不?如果按Yes,然后将删除所选的记录

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()); 

     } 
+0

如果您发现任何问题,然后问我 – Android

+0

是的,我的火腿有问题,我有另一个类在那里我有删除查询里面,我已经发布了我如何在LongItemclcik Implment方法plz帮助 – user3432979