它显示数据库的第一个数据,但是我想显示所有数据,每个行的数字是3列表,然后点击按钮后打开它并在新活动中打开此列表
//Read Database
public void readDB(View v) {
SQLiteDatabase db2 = openOrCreateDatabase(" Result ", MODE_PRIVATE, null);
String strThree = "SELECT * FROM my_result";
Cursor c = db2.rawQuery(strThree, null);
c.moveToNext();
String grade = c.getString(c.getColumnIndex("Grade_Point"));
String ss = c.getString(c.getColumnIndex("Subject_Name"));
Toast.makeText(getApplicationContext(), " Subject Name is "+ss+" and Gragde point is"+grade , Toast.LENGTH_LONG).show();
}
答案 0 :(得分:0)
我建议您不要创建新活动,而是使用dialog
创建listview
,请按照以下教程之一进行操作:
http://envyandroid.com/creating-listdialog-with-images-and-text/
http://www.edumobile.org/android/custom-listview-in-a-dialog-in-android/
答案 1 :(得分:0)
将第3列中提取的值存储到变量中,最好是在arraylist中。然后单击您的按钮将其发送到您的活动,并在该活动的onCreate()方法中填充该活动中的列表视图。以下示例(未测试)
ArrayList<String> col_3 = new ArrayList<String>();
void readDB(View v){
SQLiteDatabase db2 = openOrCreateDatabase(" Result ", MODE_PRIVATE, null);
String strThree = "SELECT * FROM my_result";
Cursor c = db2.rawQuery(strThree, null);
while(c.moveToNext()!=null){
col_3.add(c.getString(2)) //since 3rd column
}
}
现在,在按钮的onClick上,通过意图将其发送到目标活动
Intent intent = new Intent(CurrentActivity.this, DestinationActivity.class);
intent.putStringArrayListExtra("col_3_data", col_3);
startActivity(intent);
目标Activity的onCreate()方法将是这样的
Intent i = new Intent();
ArrayList col_value =new ArrayList<String>();
col_value = i.getStringArrayListExtra("col_3_data");
ListView lv = (ListView)findViewById(R.id.my_lsitview); //my_listview is your listview where you want to display your data
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, col_value );
lv.setAdapter(arrayAdapter);