[英]ListView from SQLite Database
So I'm trying to add items to ListView
every time users presses SAVE button on Dialog
. 所以我试图每次用户在
Dialog
上按SAVE按钮时将项目添加到ListView
。 I think I did everything right except coding the onClickListener
for that SAVE Button
. 我认为除了为该SAVE
Button
编码onClickListener
之外,我所做的一切都对。
I would really appreciate if someone could help me out. 如果有人可以帮助我,我将不胜感激。
Here's my row layout: 这是我的行布局:
<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>
Database Helper class: 数据库助手类:
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);
}
}
dataoperation class 数据操作类
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;
}
}
And the part of code from activity that contains the Dialog
and ListView
: 以及来自
Dialog
和ListView
活动代码部分:
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);
}
});
That EditText and all that stuff are from this xml: 该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>
I think that lv.addView(editTextDodajPredmetImePredmeta);
我认为
lv.addView(editTextDodajPredmetImePredmeta);
is causing the problem, but with what should I replace it? 是造成此问题的原因,但是我应该用什么替换呢?
Thanks and sorry for the whole bunch of code! 感谢和抱歉的全部代码!
因此,您的目标是将数据保存到数据库并将其添加到Listview,您可以调用已编写的insert方法,并在调用此方法后调用notify数据集更改的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.