0

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

为了与数据库连接,我使用了一个类(DatanbankManager)。我显示我的代码。

我的 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("");
    }

}

我的问题活动!显示活动:

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 中的 Layout 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>
4

1 回答 1

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" />
于 2013-01-22T13:42:26.137 回答