Reputation: 25
I wanted to display a specific row in my table. I have two Activities the first activity is to input the ID passed to the 2nd Activity which is going to display the row in my table based on the ID that I inputed.
Hope you can help with my problem. I don't know what is wrong in my code.
//MainActivity:
public class MainActivity extends Activity {
EditText et_id;
public void doView(View v){
String id = et_id.getText().toString();
if(!id.isEmpty()){
Intent i = new Intent(this.getApplicationContext(), ActivityView.class);
i.putExtra("id", id);
startActivity(i);
} else {
Dialog d = new Dialog(this);
d.setTitle("Message");
TextView tv = new TextView(this);
tv.setText("ID must be provided");
d.setContentView(tv);
d.show();
}
}
}
//ActivityView.class
public class ActivityView extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_view);
TextView tv_id = (TextView) findViewById(R.id.tvId);
TextView tv_name = (TextView) findViewById(R.id.tvName);
TextView tv_course = (TextView) findViewById(R.id.tvCourse);
Bundle extra = getIntent().getExtras();
String id = extra.getString("id");
DBase db = new DBase(this);
db.open();
String[] rec = db.getRecord(Integer.parseInt(id));
db.close();
if(rec[0]!=null){
tv_name.setText(rec[0]);
tv_course.setText(rec[1]);
} else {
Dialog d = new Dialog(this);
d.setTitle("Message");
TextView tv = new TextView(this);
tv.setText("There is no record");
d.setContentView(tv);
d.show();
}
}
}
//DBase.java
public String[] getRecord(int rid) throws SQLException{
String selectQuery = "SELECT * FROM "+DB_TABLE+"WHERE"+K_RID+"="+rid;
Cursor c = null;
c = dBase.rawQuery(selectQuery, null);
String[] data = new String[2];
if(c.moveToFirst()){
int indexName = c.getColumnIndex(K_NAME);
int indexCourse = c.getColumnIndex(K_COURSE);
data[0] = c.getString(indexName);
data[1] = c.getString(indexCourse);
}
return data;
}
Upvotes: 0
Views: 96
Reputation: 16691
I suspect the reason your code won't work is because your query is wrong:
String selectQuery = "SELECT * FROM "+DB_TABLE+"WHERE"+K_RID+"="+rid;
You don't have any white space, so your table name, where column, and id are all getting smashed together. Make sure there is a space between each item:
String selectQuery = "SELECT * FROM " + DB_TABLE + " WHERE " + K_RID + " = " + rid;
Upvotes: 1