2014-11-02 1 views
0

내 데이터베이스에서 행을 삭제하려고하는데, 사용자 입력을 읽고 그 행을 삭제하고 싶습니다. 내가 문제를 겪고있는 것은 사용자 정보를 얻고이를 사용하여 행을 삭제하는 것입니다.안드로이드 DB에서 행을 삭제

이것은 행 정보를 읽는 코드입니다. 난 당신이 사용자 입력을받을 필요가

package com.example.rory.dbtest; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 

import com.pinchtapzoom.R; 


public class delete extends Activity { 

Button delete; 
Public EditText edit1; 

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

    delete = (Button) findViewById(R.id.button); 
    editi = (EditText) findViewById(R.id.edit1); 
    delete.setOnClickListener((android.view.View.OnClickListener) this); 

public void onClick(View v) { 
    //what code do i enter here to read in and send rowid to the db class 
} 

} 


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

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 
} 

이것은 나뿐만 아니라

//---deletes a particular record--- 
public boolean deleteContact(long rowId) 
{ 
    return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0; 
} 

답변

1

먼저 사용하고있는 DB 코드 적합한 장소입니다 온 클릭 방법 또는 날씨에 무엇을 쓰고 잘 모릅니다 KEY_ROWID 데이터 유형에 해당하는 유효한 값인지 확인합니다 (정수 또는 긴 경우).

이 행해져 Yout 가정 KEY_ROWID의 긴 번호가, 당신이 이런 짓을 할 것이다 :

public void onClick(View v) { 
    //what code do i enter here to read in and send rowid to the db class 
    //..this code: 
    String userInput = edit1.getText().toString(); 

    //if the ROW ID its a number (long) 
    try{ 
     long rowID = Long.parseLong(userInput); 
     //call the delete method 
     deleteContact(rowID); 
    }catch(NumberFormatException e){ 
     Log.e("INPUT ERROR","Input is not a number!",e); 
     //notify the user that the input is invalid. 
     Toast.makeText(this,"Invalid Value!",Toast.LENGTH_LONG).show(); 
    } 
} 
+0

당신은 너무 많은 영웅 감사합니다! 누군가가 그것을 밖으로 할 때 나는 생각했다. –