2017-07-15 45 views
-1

我创建了一个像联系人列表一样工作的程序。到目前为止,我已经能够添加或更新联系人,但我无法删除联系人。我不知道问题出在哪里。当我要进入DisplayContact类时,我遇到了ForceClose。从SQLite数据库中删除一个项目

我DisplayContact代码:

public class DisplayContact extends Activity { 

int from_Where_I_Am_Coming = 0; 
    private DBHelper mydb ; 

    TextView name ; 
    TextView phone; 
    TextView email; 
    TextView street; 
    TextView place; 
    int id_To_Update = 0; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_display_contact); 
     name = (TextView) findViewById(R.id.editTextName); 
     phone = (TextView) findViewById(R.id.editTextPhone); 
     email = (TextView) findViewById(R.id.editTextStreet); 
     street = (TextView) findViewById(R.id.editTextEmail); 
     place = (TextView) findViewById(R.id.editTextCity); 

     mydb = new DBHelper(this); 

     Bundle extras = getIntent().getExtras(); 
     if(extras !=null) { 
      int Value = extras.getInt("id"); 

      if(Value>0){ 
       //means this is the view part not the add contact part. 
       Cursor rs = mydb.getData(Value); 
       id_To_Update = Value; 
       rs.moveToFirst(); 

       String nam = rs.getString(rs.getColumnIndex(DBHelper.CONTACTS_COLUMN_NAME)); 
       String phon = rs.getString(rs.getColumnIndex(DBHelper.CONTACTS_COLUMN_PHONE)); 
       String emai = rs.getString(rs.getColumnIndex(DBHelper.CONTACTS_COLUMN_EMAIL)); 
       String stree = rs.getString(rs.getColumnIndex(DBHelper.CONTACTS_COLUMN_STREET)); 
       String plac = rs.getString(rs.getColumnIndex(DBHelper.CONTACTS_COLUMN_CITY)); 

       if (!rs.isClosed()) { 
        rs.close(); 
       } 
       Button b = (Button)findViewById(R.id.button1); 
       b.setVisibility(View.VISIBLE); 

       name.setText((CharSequence)nam); 
       name.setFocusable(true); 
       name.setClickable(true); 

       phone.setText((CharSequence)phon); 
       phone.setFocusable(true); 
       phone.setClickable(true); 

       email.setText((CharSequence)emai); 
       email.setFocusable(false); 
       email.setClickable(false); 

       street.setText((CharSequence)stree); 
       street.setFocusable(false); 
       street.setClickable(false); 

       place.setText((CharSequence)plac); 
       place.setFocusable(false); 
       place.setClickable(false); 
      } 

      Button b2 = (Button)findViewById(R.id.button2); 
      b2.setVisibility(View.VISIBLE); 

      name.setText((CharSequence)name); 
      name.setFocusable(true); 
      name.setClickable(true); 

      phone.setText((CharSequence)phone); 
      phone.setFocusable(true); 
      phone.setClickable(true); 

      email.setText((CharSequence)email); 
      email.setFocusable(false); 
      email.setClickable(false); 

      street.setText((CharSequence)street); 
      street.setFocusable(false); 
      street.setClickable(false); 

      place.setText((CharSequence)place); 
      place.setFocusable(false); 
      place.setClickable(false); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     Bundle extras = getIntent().getExtras(); 

     if(extras !=null) { 
      int Value = extras.getInt("id"); 
      if(Value>0){ 
       getMenuInflater().inflate(R.menu.display_contact, menu); 
      } else{ 
       getMenuInflater().inflate(R.menu.main_menu, menu); 
      } 
     } 
     return true; 
    } 

    public boolean onOptionsItemSelected(MenuItem item) { 
     super.onOptionsItemSelected(item); 
     switch(item.getItemId()) { 
      case R.id.Edit_Contact: 

       Button b = (Button)findViewById(R.id.button1); 
       b.setVisibility(View.VISIBLE); 

       name.setEnabled(true); 
       name.setFocusableInTouchMode(true); 
       name.setClickable(true); 

       phone.setEnabled(true); 
       phone.setFocusableInTouchMode(true); 
       phone.setClickable(true); 

       email.setEnabled(true); 
       email.setFocusableInTouchMode(true); 
       email.setClickable(true); 

       street.setEnabled(true); 
       street.setFocusableInTouchMode(true); 
       street.setClickable(true); 

       place.setEnabled(true); 
       place.setFocusableInTouchMode(true); 
       place.setClickable(true); 


      case R.id.Delete_Contact: 

       Button b1 = (Button)findViewById(R.id.button2); 
       b1.setVisibility(View.VISIBLE); 

       AlertDialog.Builder builder = new AlertDialog.Builder(this); 
       builder.setMessage(R.string.deleteContact) 
         .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int id) { 
           mydb.deleteContact(id_To_Update); 
           Toast.makeText(getApplicationContext(), "Deleted Successfully", 
             Toast.LENGTH_SHORT).show(); 
           Intent intent = new Intent(getApplicationContext(),MainActivity.class); 
           startActivity(intent); 
          } 
         }) 
         .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int id) { 

          } 
         }); 

       AlertDialog d = builder.create(); 
       d.setTitle("Are you sure"); 
       d.show(); 

       return true; 
      default: 
       return super.onOptionsItemSelected(item); 

     } 
    } 

    public void run(View view) { 
     Bundle extras = getIntent().getExtras(); 
     if(extras !=null) { 
      int Value = extras.getInt("id"); 
      if(Value>0){ 
       if(mydb.updateContact(id_To_Update,name.getText().toString(), 
         phone.getText().toString(), email.getText().toString(), 
         street.getText().toString(), place.getText().toString())){ 
        Toast.makeText(getApplicationContext(), "Updated", Toast.LENGTH_SHORT).show(); 
        Intent intent = new Intent(getApplicationContext(),MainActivity.class); 
        startActivity(intent); 
       } else{ 
        Toast.makeText(getApplicationContext(), "not Updated", Toast.LENGTH_SHORT).show(); 
       } 
      } else{ 
       if(mydb.insertContact(name.getText().toString(), phone.getText().toString(), 
         email.getText().toString(), street.getText().toString(), 
         place.getText().toString())){ 
        Toast.makeText(getApplicationContext(), "done", 
          Toast.LENGTH_SHORT).show(); 
       } else{ 
        Toast.makeText(getApplicationContext(), "not done", 
          Toast.LENGTH_SHORT).show(); 
       } 
       Intent intent = new Intent(getApplicationContext(),MainActivity.class); 
       startActivity(intent); 
      } 
     } 
    } 
} 
+2

显示'deleteContact()'方法 –

+0

@IntelliJAmiya我没有得到您的请求,这里是al l我与代码 –

+0

相关的代码显示** DBHelper **类的** deleteContact()**方法的代码。 – sHOLE

回答

0

不知道要删除什么,但在数据库中的辅助文件,我会作出这样的事。

public void deleteRecordID(int id){ 
    String query = "DELETE FROM "+ TABLE_NAME +" WHERE " + ID+" = "+id; 
    SQLiteDatabase db = this.getWritableDatabase(); 
    db.execSQL(query); 
    db.close(); 
} 

并传入值低于ID,我无法找到你的抓法,但是这是你如何从源码DATABSE删除

DatabaseHelper db; 
db = new DatabaseHelper(v.getContext()); 
db.deleteRecordID(id); 
+0

OP正在'DBHelper mydb'上删除。不知道它是什么,因为OP不会在问题中分享它。我想指出一个事实,即没有数据库帮助程序也可以这样做。 – Zoe

+0

示例我创建了一个联系人,现在我想删除它但它不起作用 –

+0

是的,但您需要验证如何创建联系人的方法。就像你要添加到数据库或使用更多的OOP并创建特定的联系人对象?我们不能删除我们无法找到的东西......你的deleteContact方法中有什么? –

0

我MainActivity代码:

public final static String EXTRA_MESSAGE = "MESSAGE"; 
private ListView obj; 
DBHelper mydb; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    mydb = new DBHelper(this); 
    ArrayList array_list = mydb.getAllCotacts(); 
    ArrayAdapter arrayAdapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1, array_list); 

    obj = (ListView)findViewById(R.id.listView1); 
    obj.setAdapter(arrayAdapter); 
    obj.setOnItemClickListener(new OnItemClickListener(){ 
     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) { 
      // TODO Auto-generated method stub 
      int id_To_Search = arg2 + 1; 

      Bundle dataBundle = new Bundle(); 
      dataBundle.putInt("id", id_To_Search); 

      Intent intent = new Intent(getApplicationContext(),DisplayContact.class); 

      intent.putExtras(dataBundle); 
      startActivity(intent); 
     } 
    }); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main_menu, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item){ 
    super.onOptionsItemSelected(item); 

    switch(item.getItemId()) { 
     case R.id.item1:Bundle dataBundle = new Bundle(); 
      dataBundle.putInt("id", 0); 

      Intent intent = new Intent(getApplicationContext(),DisplayContact.class); 
      intent.putExtras(dataBundle); 

      startActivity(intent); 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
    } 
} 

public boolean onKeyDown(int keycode, KeyEvent event) { 
    if (keycode == KeyEvent.KEYCODE_BACK) { 
     moveTaskToBack(true); 
    } 
    return super.onKeyDown(keycode, event); 
} 

}