如何使用动态表名在SQLite中创建表并在ListView中显示?

时间:2013-01-23 14:15:57

标签: java android sqlite listview

我的Android应用程序有问题。对我的程序:我有一个包含许多活动的应用程序,我使用ListView和SQLite数据库。如果我单击ListView中的值,我会使用此值启动另一个Activity。如果下一个Activity开始,我会收到一个错误。

LogCat说:找不到来源

这是我的代码:

public class ShowDayActivity extends ListActivity {

    private SQLiteDatabase mDatenbank;
    private DatenbankManager mHelper; //My Helpclass

    private static String KLASSEN_SELECT_ROW;

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

        Intent intent = getIntent();

        ((TextView)(findViewById(R.id.schultag))).setText(intent.getStringExtra("iteid")); 

        String Tablename = ((TextView)findViewById(R.id.schultag)).getText().toString();

        mHelper = new DatenbankManager(this);

        mHelper.SQL_SELECT = "SELECT name FROM "+Tablename+"";

        mHelper.SQL_CREATE = "CREATE TABLE "+Tablename+"(" +
        "_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                "name TEXT NOT NULL, " +
        "info TEXT)";
    }

    @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();
    }

    public void addNew(View view)
    {
        EditText et = (EditText)findViewById(R.id.fach_name);

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

        String tbname = mHelper.TBNAME;

        mDatenbank.insert(tbname, null, werte);

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

        ladeDaten();
        et.setText("");


    }

    private void ladeDaten() {
String tbname = mHelper.TBNAME;

        KLASSEN_SELECT_ROW = "SELECT name FROM "+tbname+"";

        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);
    }

我对此活动的布局:

<?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/schultag"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        android:text="@string/schultag_titel"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#E1E1E1" />

        <EditText
            android:id="@+id/fach_name"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="0dp"
            android:ems="10"
            android:hint="Bitte Fach eingeben" >

            <requestFocus />
        </EditText>

        <Button
            android:layout_width="80dp"
            android:layout_height="40dp"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:background="@drawable/btn_default_normal_holo_light"
            android:drawableLeft="@drawable/icon_ok"
            android:text="@string/plus"
            android:onClick="addNew"
            ></Button>

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

    <ListView
        android:id="@android:id/android: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" >

    </ListView>

</LinearLayout>

0 个答案:

没有答案