英文:
Why doesn't it get any data?
问题
Main:
ColegiListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, ColegDataEditActivity.class);
intent.putExtra("Id", Integer.parseInt(Long.toString(id)));
Log.i("Id", id + "");
startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(MainActivity.this).toBundle());
}
});
DATABASE HELPER:
public Cursor getSpecificData(int id) {
SQLiteDatabase db = this.getWritableDatabase();
Cursor data = db.rawQuery("SELECT * FROM " + TableName + " WHERE ID =" + id, null);
return data;
}
SECOND ACTIVITY:
Intent intent = getIntent();
int id = intent.getIntExtra("Id", 0);
Cursor data = mDatabase.getSpecificData(id);
Hi! I am a beginner with SQL and I don't know what I did wrong. I have a database with multiple columns and I need to get some data from it so then I can put those values in some TextViews. The problem is that the data I need needs to be a single row with the specific id from an item from a ListView (yeah, I know about row id, I solved that problem). Any ideas why isn't it working?
英文:
Main:
ColegiListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, ColegDataEditActivity.class);
intent.putExtra("Id", Integer.parseInt(Long.toString(id)));
Log.i("Id",id+"");
startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(MainActivity.this).toBundle());
}
});
DATABASE HELPER:
public Cursor getSpecificData(int id){
SQLiteDatabase db = this. getWritableDatabase();
Cursor data = db.rawQuery("SELECT * FROM "+ TableName + " WHERE ID ="+id, null);
return data;
}
SECOND ACTIVITY:
Intent intent = getIntent();
int id =intent.getIntExtra("Id",0);
Cursor data = mDatabase.getSpecificData(id);
Hi! I am a beginner with SQL and I don't know what I did wrong. I have a database with multiple columns and I need to get some data from it so then I can put those values in some TextViews. The problem is that the data I need needs to be a single row with the specific id from an item from a ListView (yeah, I now about row id, I solved that problem). Any ideas why isn't it working?
答案1
得分: 1
Your Database Helper
public Cursor getSpecificData(int id){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM " + TableName + " WHERE ID = " + id;
Cursor res = db.rawQuery(query, null);
return res;
}
In Activity make a setViews function
public void setViews() {
Cursor cursor = mDatabase.getSpecificData(id);
try {
if (cursor.getCount() != 0) {
if (cursor.moveToFirst()) {
textview1.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_1_NAME)));
textview2.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_2_NAME)));
//and so on
}
}
} finally {
if (cursor != null) {
cursor.close();
}
}
}
英文:
Your Database Helper
public Cursor getSpecificData(int id){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM "+ TableName + " WHERE ID = "+id;
Cursor res = db.rawQuery(query, null);
return res;
}
In Activity make a setViewsfunction
public void setViews() {
Cursor cursor = mDatabase.getSpecificData(id);
try {
if (cursor.getCount() != 0) {
if (cursor.moveToFirst()) {
textview1.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_1_NAME)));
textview2.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_2_NAME)));
//and so on
}
}
} finally {
if (cursor != null) {
cursor.close();
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论