So I'm trying to add items to ListView every time users presses SAVE button on Dialog. I think I did everything right except coding the onClickListener for that SAVE Button.
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:
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:
<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); is causing the problem, but with what should I replace it?
Thanks and sorry for the whole bunch of code!
No comments:
Post a Comment