获取数据库行中的所有数据以显示Android SQLite

时间:2012-04-03 14:10:08

标签: android database android-listview listactivity

我想让我的班级显示数据库中的所有数据。在目前它我只显示一组数据,这将是标题,我试图改变他的代码,我认为将显示所有的数据

public class WorkoutProgress extends ListActivity {
private DataBaseHelper datasource;
@Override
public void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.progress);

    datasource = new DataBaseHelper(this);
    datasource.open();
    fillData();
    datasource.close();
}
 private void fillData() {
        // Get all of the notes from the database and create the item list
        Cursor c = datasource.getAllTitles();
        startManagingCursor(c);

        String[] from = new String[] { DataBaseHelper.KEY_TITLE,DataBaseHelper.KEY_ISBN, DataBaseHelper.KEY_PUBLISHER };
        int[] to = new int[] { R.id.text1 };

        // Now create an array adapter and set it to display using our row
        SimpleCursorAdapter notes =
            new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to);
        setListAdapter(notes);
    }

    }

我认为我需要改变的代码是这一行

     String[] from = new String[] { DataBaseHelper.KEY_TITLE};

到这一行

        String[] from = new String[] { DataBaseHelper.KEY_TITLE,DataBaseHelper.KEY_ISBN, DataBaseHelper.KEY_PUBLISHER };

这是notes_row.xml

<?xml version="1.0" encoding="utf-8"?>
 <TextView android:id="@+id/text1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/text2"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/text3"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

1 个答案:

答案 0 :(得分:1)

您更改了from数组,但您还必须在列表行布局中to数组(ids的其他TextView中添加一些ID {{1你会在哪里显示其他列。)

R.layout.notes_row

其中String[] from = new String[] { DataBaseHelper.KEY_TITLE,DataBaseHelper.KEY_ISBN, DataBaseHelper.KEY_PUBLISHER }; int[] to = { R.id.text1, R.id.text2, R.id.text3 }; R.id.text1R.id.text2是来自R.id.text3的{​​{1}}的ID:

TextView