我是这个Android开发的新手。我想要做的是将ID从MainActivity
传递给Details
。因此,我需要从MainActivity
获取ID,以便在Details
课程中显示某个人的详细信息。我已经可以传递id并显示它,但是我无法显示来自数据库值的特定id的详细信息。如何使用数据库函数Details
在getDetails(id);
类中显示该特定id的内容?我需要在Details
活动类的文本视图中显示它。请帮忙
数据库功能
public Cursor getDetails(String id) {
return database.query(TABLENAME, null, id + "=" + id, null, null,
null, null);
}
MainActivity.java
this.listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3){
TextView txtID = (TextView) view.findViewById(R.id.id);
String id = txtID.getText().toString();
Intent intent = new Intent(MainActivity.this, Details.class);
intent.putExtra("id", id);
startActivity(intent);
}
});
在Details.java
中,我想在此处显示数据库中ID的内容。我如何调用该函数并使用传递的意图中的ID进行显示?
Details.java
Intent i = getIntent();
String id = i.getStringExtra("id");
txtId = (TextView) findViewById(R.id.id);
txtName= (TextView) findViewById(R.id.name);
txtAbout = (TextView) findViewById(R.id.about);
txtId.setText(id);
txtName.setText(name);
txtAbout .setText(about);
答案 0 :(得分:0)
public HashMap<String,String> getDetails(String id) {
HashMap<String, String> hm = new HashMap<>();
SQLiteDatabase db = getWritableDatabase();
Cursor c = db.query(TABLENAME, null, "id=?", new String[]{id}, null, null, null);
if(c.getCount()>0){
c.moveToFirst();
hm.put(KEY, c.getString(c.getColumnIndex(COLUMN_NAME)));
// do this for all columns that you want.
}
c.close();
db.close();
return hm;
}
并在Details.java中
String id = getIntent().getExtras.getString("id");
HashMap<String, String> hm = (Your database class Object).getDetails(id);
if(hm.size()>0){
String value1 = hm.get(KEY);
// get all data and show according to you.
}