繁体   English   中英

SQLite数据库中的ListView

[英]ListView from SQLite Database

所以我试图每次用户在Dialog上按SAVE按钮时将项目添加到ListView 我认为除了为该SAVE Button编码onClickListener之外,我所做的一切都对。

如果有人可以帮助我,我将不胜感激。

这是我的行布局:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:background="#cccccc"
        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Ime predmeta"
            android:layout_gravity="center"
            android:layout_marginLeft="15dp"
            android:textSize="20sp"
            android:textColor="#424242"
            android:id="@+id/textViewRowListaPredmetiImePredmeta"/>

    </LinearLayout>

数据库助手类:

public class PredmetiDodajDBHelper extends SQLiteOpenHelper{

    public static final int DATABASSE_VERSION = 3;
    public static final String DATABASE_NAME = "dodaj_predmet.db";
    public static final String TABLE_NAME = "predmeti";
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_IMEPREDMETA = "imepredmeta";


    public PredmetiDodajDBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, DATABASE_NAME, factory, DATABASSE_VERSION);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        String query = "CREATE TABLE " + TABLE_NAME + "(" +
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_IMEPREDMETA + " TEXT NOT NULL " +
                ");";
        db.execSQL(query);

    }



    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);

    }

}

数据操作类

public class dataoperation {

    SQLiteDatabase database_ob;
    PredmetiDodajDBHelper openHelper_ob;
    Context context;


    public dataoperation(Context c) {
        // TODO Auto-generated constructor stub

        context=c;

    }

    public dataoperation opnToRead() {
        openHelper_ob = new PredmetiDodajDBHelper(context,
                openHelper_ob.DATABASE_NAME, null, openHelper_ob.DATABASSE_VERSION);
        database_ob = openHelper_ob.getReadableDatabase();
        return this;

    }

    public dataoperation opnToWrite() {
        openHelper_ob = new PredmetiDodajDBHelper(context,
                openHelper_ob.DATABASE_NAME, null, openHelper_ob.DATABASSE_VERSION);
        database_ob = openHelper_ob.getWritableDatabase();
        return this;

    }

    public void Close() {
        database_ob.close();
    }

    public long insertData(String fname) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(openHelper_ob.COLUMN_IMEPREDMETA, fname);
        opnToWrite();
        long val = database_ob.insert(openHelper_ob.TABLE_NAME, null,
                contentValues);
        Close();
        return val;

    }

    public Cursor readdata() {
        String[] cols = { openHelper_ob.COLUMN_ID, openHelper_ob.COLUMN_IMEPREDMETA };
        opnToWrite();
        @SuppressWarnings("static-access")
        Cursor c = database_ob.query(openHelper_ob.TABLE_NAME, cols, null,
                null, null, null, null);

        return c;

    }
    public Cursor queryAll(int nameId) {
        String[] cols = { openHelper_ob.COLUMN_ID, openHelper_ob.COLUMN_IMEPREDMETA};
        opnToWrite();
        Cursor c = database_ob.query(openHelper_ob.TABLE_NAME, cols,
                openHelper_ob.COLUMN_ID + "=" + nameId, null, null, null, null);

        return c;

    }
}

以及来自DialogListView活动代码部分:

 editTextDodajPredmetImePredmeta = (EditText)dialog. findViewById(R.id.editTextDodajPredmetImePredmeta);
                    buttonSpremiPredmet = (Button)dialog. findViewById(R.id.buttonSpremiPredmet);
                    imePredmeta = editTextDodajPredmetImePredmeta.getText().toString();


                    lv=(ListView)findViewById(R.id.lista_predmeti);
                    //bt=(Button)findViewById(R.id.buttonSpremiPredmet);
                    adapter_ob = new dataoperation(Ocijene.this);

                    String[] from = { PredmetiDodajDBHelper.COLUMN_IMEPREDMETA };
                    int[] to = { R.id.editTextDodajPredmetImePredmeta };
                    cursor = adapter_ob.readdata();
                    SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(Ocijene.this,
                            R.layout.row_lista_predmeti, cursor, from, to);
                    lv.setAdapter(cursorAdapter);

                    buttonSpremiPredmet.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            lv.addView(editTextDodajPredmetImePredmeta);


                        }
                    });

该EditText和所有其他内容均来自以下xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="26dp"
    android:paddingRight="26dp"
    android:paddingTop="15dp"
    android:paddingBottom="25dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/dialog_predmeti_ime_predmeta"
        android:layout_marginBottom="5dp"
        android:id="@+id/textView5" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editTextDodajPredmetImePredmeta"
        android:layout_marginBottom="15dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/dialog_predmeti_vrsta_ispita"
        android:layout_marginBottom="5dp"
        android:id="@+id/textView6" />

    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/spinnerPredmetiLayoutDialogDodajVrsteIspita"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save"
        android:id="@+id/buttonSpremiPredmet" />

</LinearLayout>

我认为lv.addView(editTextDodajPredmetImePredmeta); 是造成此问题的原因,但是我应该用什么替换呢?

感谢和抱歉的全部代码!

因此,您的目标是将数据保存到数据库并将其添加到Listview,您可以调用已编写的insert方法,并在调用此方法后调用notify数据集更改的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM