如何用Android中的SQLite数据库填充我的ListView和Records?

时间:2013-01-22 13:26:28

标签: android database sqlite listview listactivity

我有一个Android应用程序,我使用两个活动。第一个可以在sqlite数据库中创建一个记录,第二个可以在listview中显示这个记录。问题现在是在列表视图中显示我的记录;(

用于连接数据库我是一个类(DatenbankManager)。我展示了我的代码。

我的DatenbankManager课程:

public class DatenbankManager extends SQLiteOpenHelper {

    private static final String DB_Name = "Stundenplanname.db";
    private static final int DB_VERSION = 1;

    public static String STUNDENPLAN_CREATE; 

    private static final String KLASSEN_DROP = "DROP TABLE IF EXIST Stundenplan";

        //Konstruktor
        public DatenbankManager(Context context) {
            super(context, DB_Name, null, DB_VERSION);

        }

        //Methode wenn eine Datenbank erstellt werden muss
        @Override
        public void onCreate(SQLiteDatabase db) {

            db.execSQL(STUNDENPLAN_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

                db.execSQL(KLASSEN_DROP); // Löscht Tabelle wenn nicht vorhanden 
                onCreate(db); //Erstellt Tabelle
        }

}

我创建记录的第一个活动:

public class NewActivity extends Activity {

    private SQLiteDatabase mDatenbank;
    private DatenbankManager mHelper;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new);

        mHelper = new DatenbankManager(this);

        mHelper.STUNDENPLAN_CREATE = "CREATE TABLE Stundenplan(" +
                "_id INTEGER PRIMARY KEY AUTOINCREMENT, "+
                "name TEXT NOT NULL)";

    }

    @Override
    protected void onPause() {
        super.onPause();
        mDatenbank.close();

        Toast.makeText(this, 
                getResources().getString(R.string.db_close),
                Toast.LENGTH_SHORT).show(); 
    }

    @Override
    protected void onResume() {

        super.onResume();
        mDatenbank = mHelper.getReadableDatabase(); 

        Toast.makeText(this, 
                getResources().getString(R.string.db_open),
                Toast.LENGTH_SHORT).show(); 

    }

    public void addNewPlan(View view)
    {
        EditText et = (EditText)findViewById(R.id.schultag_name);

        ContentValues werte = new ContentValues();
        werte.put("name",et.getText().toString());

        mDatenbank.insert("stundenplan", null, werte);

        Toast.makeText(this, 
                getResources().getString(R.string.add),
                Toast.LENGTH_SHORT).show(); 

        et.setText("");
    }

}

我的问题活动! showActivity:

public class ShowActivity extends ListActivity {

    private SQLiteDatabase mDatenbank;
    private DatenbankManager mHelper;

    private static final String KLASSEN_SELECT_ROW = 
            "SELECT _id, name FROM Stundenplan";


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);

        mHelper = new DatenbankManager(this);
        mDatenbank = mHelper.getReadableDatabase(); 

        ladeDaten(); 

    }

    @Override
    protected void onPause() {
        super.onPause();
        mDatenbank.close();

        Toast.makeText(this, 
                getResources().getString(R.string.db_close),
                Toast.LENGTH_SHORT).show(); 
    }

    @Override
    protected void onResume() {     
        super.onResume();
        mDatenbank = mHelper.getReadableDatabase(); 

        Toast.makeText(this, 
                getResources().getString(R.string.db_open),
                Toast.LENGTH_SHORT).show(); 

        ladeDaten();
    }

    private void ladeDaten() {
        Cursor KlassenCursor = mDatenbank.rawQuery(KLASSEN_SELECT_ROW, null); 
        startManagingCursor(KlassenCursor); 

        android.widget.SimpleCursorAdapter KlassenAdapter = new android.widget.SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_1, 
                KlassenCursor, 
                new String[] {"name"},
                new int[] {
                android.R.id.text1
                });

        setListAdapter(KlassenAdapter);
    }


}

这是我的showActivity的布局xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#111111"
    android:baselineAligned="false"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:text="Stundenplanname"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#19630F"
        android:textSize="22sp" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="250dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_height="wrap_content"
        android:textColor="#19630F"
        android:layout_gravity="center" >

    </ListView>

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

如果您在Activity中扩展ListActivity,那么ListView必须有android:id="@android:id/list"而不是android:id="@+id/listView1"。将ListView xml更改为:

<ListView
    android:id="@android:id/list"
    android:layout_width="250dp"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:layout_height="wrap_content"
    android:textColor="#19630F"
    android:layout_gravity="center" />