Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to delete a row and save text in a custom ListView
    primarykey
    data
    text
    <p>I am trying to create a list of names using a custom <code>ListView</code>. Each row has an edit and delete button.</p> <p>The edit button works fine, but I don't know how to update the database after the user has pressed either the edit or delete button.</p> <p>This is what I want to do:</p> <ol> <li><p>I want to delete the row from the UI and the database.</p></li> <li><p>I want to save the editable text when the user presses the database save button in the options menu.</p></li> </ol> <p>Here is my code:</p> <p>EditList.java [main]</p> <pre><code>package edit.list; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.view.View; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.SimpleCursorAdapter; import android.widget.TextView; public class EditLIST extends Activity { private AndroidSQLite mySQLiteAdapter ; final static String EXTRA_MESSAGE = "edit.list.message"; public void onClick (View view) { Intent intent = new Intent(this,display.class); RelativeLayout relativeLayout = (RelativeLayout) view.getParent(); TextView textView = (TextView) relativeLayout.findViewById(R.id.textView1); // TextView textView=(TextView) findViewById(R.id.textView1); String message = textView.getText().toString(); intent.putExtra(EXTRA_MESSAGE,message); startActivity(intent); } public void delete (View view){ //[what i have to write here] } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_edit_list); ListView listContent = (ListView)findViewById(R.id.listView1); mySQLiteAdapter = new AndroidSQLite(this); mySQLiteAdapter.openToWrite(); mySQLiteAdapter.deleteAll(); mySQLiteAdapter.insert("umesh"); mySQLiteAdapter.insert("ramesh"); mySQLiteAdapter.insert("piyush"); mySQLiteAdapter.close(); mySQLiteAdapter = new AndroidSQLite(this); mySQLiteAdapter.openToRead(); Cursor cursor = mySQLiteAdapter.queueAll(); startManagingCursor(cursor); String[] from = new String[]{AndroidSQLite.KEY_FNAME}; int[] to = new int[]{R.id.textView1}; SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.button, cursor, from, to); listContent.setAdapter(cursorAdapter); } protected void onDestroy() { super.onDestroy(); mySQLiteAdapter.close(); } } </code></pre> <p>AndroidSQLite.java</p> <pre><code>package edit.list; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteDatabase.CursorFactory; public class AndroidSQLite { public static final String MYDATABASE_NAME = "abncd"; public static final String MYDATABASE_TABLE = "zxc"; public static final int MYDATABASE_VERSION = 1; public static final String KEY_ID = "_id"; public static final String KEY_FNAME = "FNAME"; public static final String KEY_CONTENT1 = "Content1"; //create table MY_DATABASE (ID integer primary key, Content text not null); private static final String SCRIPT_CREATE_DATABASE = "create table " + MYDATABASE_TABLE + " (" + KEY_ID + " integer primary key autoincrement, " + KEY_FNAME + " text not null);"; private SQLiteHelper sqLiteHelper; private SQLiteDatabase sqLiteDatabase; private Context context; public AndroidSQLite(Context c){ context = c; } public AndroidSQLite openToRead() throws android.database.SQLException { sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION); sqLiteDatabase = sqLiteHelper.getReadableDatabase(); return this; } public AndroidSQLite openToWrite() throws android.database.SQLException { sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION); sqLiteDatabase = sqLiteHelper.getWritableDatabase(); return this; } public void close(){ sqLiteHelper.close(); } public long insert(String fname){ ContentValues contentValues = new ContentValues(); contentValues.put(KEY_FNAME, fname); return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues); } public int deleteAll(){ return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null); } public Cursor queueAll(){ String[] columns = new String[]{KEY_ID, KEY_FNAME}; Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns, null, null, null, null, null); return cursor; } public void delete(long id) { sqLiteDatabase.delete(MYDATABASE_TABLE, KEY_ID+"="+id, null); } public void update_byID(int id, String v1) { ContentValues values = new ContentValues(); values.put(KEY_CONTENT1, v1); sqLiteDatabase.update(MYDATABASE_TABLE, values, KEY_ID+"="+id, null); } public class SQLiteHelper extends SQLiteOpenHelper { public SQLiteHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub db.execSQL(SCRIPT_CREATE_DATABASE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub } } } </code></pre> <p>display.java</p> <pre><code>package edit.list; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.widget.EditText; import android.widget.Toast; public class display extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.display); // Get the message from the intent Intent intent = getIntent(); String message = intent.getStringExtra(EditLIST.EXTRA_MESSAGE); // Create the text view EditText editText = new EditText(this); editText.setTextSize(40); editText.setText(message); // Set the text view as the activity layout setContentView(editText); } public boolean onCreateOptionsMenu (Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.activity_edit_list,menu); return true; } public boolean onOptionsItemSelected(MenuItem item) { //int id=et1.getId(); // Handle item selection switch (item.getItemId()) { case R.id.save: //[WHAT I HAVE 2 WRITE HERE] finish(); return true; case R.id.cancel: Toast.makeText(this, "Cancle clicked",Toast.LENGTH_SHORT).show(); return true; default: return super.onOptionsItemSelected(item); } } } </code></pre> <p>xml file here.. activity_edit_list.xml</p> <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/RelativeLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" &gt; &lt;ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="match_parent" android:drawSelectorOnTop="false"&gt; &lt;/ListView&gt; &lt;/RelativeLayout&gt; </code></pre> <p>button.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" &gt; &lt;Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:onClick="onClick" android:text="delete" /&gt; &lt;Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_toLeftOf="@+id/button1" android:onClick="Click" android:text="edit" /&gt; &lt;TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/button2" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_toLeftOf="@+id/button2" android:text=" " /&gt; &lt;/RelativeLayout&gt; </code></pre> <p><a href="https://lh6.googleusercontent.com/-NfeEPAeYSDo/UCtlQQepw2I/AAAAAAAAA-I/SnAUytNSO-Q/s640/Capture.JPG" rel="nofollow">https://lh6.googleusercontent.com/-NfeEPAeYSDo/UCtlQQepw2I/AAAAAAAAA-I/SnAUytNSO-Q/s640/Capture.JPG</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload