gpt4 book ai didi

java - 如何通过另一个 Activity 显示数据库中的特定行?

转载 作者:行者123 更新时间:2023-12-01 11:35:57 26 4
gpt4 key购买 nike

我想显示表格中的特定行。我有两个 Activity ,第一个 Activity 是输入传递给第二个 Activity 的 ID,第二个 Activity 将根据我输入的 ID 在表中显示行。

希望您能帮助解决我的问题。我不知道我的代码有什么问题。

//主要 Activity :

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;
}

最佳答案

我怀疑您的代码无法工作的原因是您的查询错误:

String selectQuery = "SELECT * FROM "+DB_TABLE+"WHERE"+K_RID+"="+rid;

你没有任何空格,所以你的表名、所在列和 id 都被打乱了。确保每个项目之间有一个空格:

String selectQuery = "SELECT * FROM " + DB_TABLE + " WHERE " + K_RID + " = " + rid;

关于java - 如何通过另一个 Activity 显示数据库中的特定行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29991596/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com