[Solved] How to read and update SQLite database using ListView in Android?


Here Is Your Edited Working Code

MainActivityChampagne.java

package android.application.project.planes;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MainActivityChampagne {

    public static final String KEY_NAME = "title";
    public static final String KEY_NOTES = "lastcall";

    private static final String DATABASE_NAME = "Champagnedb";
    private static final String DATABASE_TABLE = "champagneTable";
    private static final int DATABASE_VERSION = 1;

    private DbHelper ourHelper;
    private final Context ourContext;
    private SQLiteDatabase ourDatabase;

    private static class DbHelper extends SQLiteOpenHelper {

        public DbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            // TODO Auto-generated constructor stub

        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub\
            db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_NAME
                    + " TEXT NOT NULL);");

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            db.execSQL("DROP TABLE IF EXIST " + DATABASE_TABLE);
            onCreate(db);

        }

    }

    public MainActivityChampagne(Context c) {
        ourContext = c;

    }

    public MainActivityChampagne open() {
        ourHelper = new DbHelper(ourContext);
        ourDatabase = ourHelper.getWritableDatabase();
        return this;

    }

    public void close() {
        ourHelper.close();

    }

    public long createEntry(String titles, String notes) {
        // TODO Auto-generated method stub
        ContentValues cv = new ContentValues();
        cv.put(KEY_NAME, titles);
        return ourDatabase.insert(DATABASE_TABLE, null, cv);

    }

    public ArrayList<String> getData() {
        // TODO Auto-generated method stub
        String[] columns = new String[] { KEY_NAME };
        Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null,
                null, null);
        String result = "";
        int iNotes = c.getColumnIndex(KEY_NAME);
        ArrayList<String> string_array = new ArrayList<String>();
        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
            result = c.getString(iNotes) + "\n";
            string_array.add(result);
        }
        return string_array;

    }

    public void updateEntry(String thetitles, String thenotes) {
        // TODO Auto-generated method stub
        ContentValues cvUpdate = new ContentValues();
        cvUpdate.put(KEY_NAME, thetitles);
        /* cvUpdate.put(KEY_NOTES, thenotes); */
        /*
         * ourDatabase.update(DATABASE_TABLE, cvUpdate, KEY_NAME + "=" +
         * thetitles + KEY_NOTES + "=" + thenotes, null);
         */
        ourDatabase.update(DATABASE_TABLE, cvUpdate, KEY_NAME + "='"
                + thetitles + "'", null);

    }

}

MainActivityPaper.java

package android.application.project.planes;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

public class MainActivityPaper extends Activity implements OnClickListener {

    TextView text1, title1;
    Button save1, update1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main_activity_paper);

        text1 = (TextView) findViewById(R.id.editText1);
        title1 = (TextView) findViewById(R.id.editText3);
        save1 = (Button) findViewById(R.id.button3);
        update1 = (Button) findViewById(R.id.button5);

        save1.setOnClickListener(this);
        update1.setOnClickListener(this);

    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

        switch (arg0.getId()) {
        case R.id.button3:

            String titles = title1.getText().toString();
            String notes = text1.getText().toString();

            MainActivityChampagne entry = new MainActivityChampagne(
                    MainActivityPaper.this);
            entry.open();
            entry.createEntry(titles, notes);
            entry.close();

            break;

        case R.id.button5:

            String thetitles = title1.getText().toString();
            String thenotes = text1.getText().toString();

            MainActivityChampagne up = new MainActivityChampagne(
                    MainActivityPaper.this);
            up.open();
            up.updateEntry(thetitles, thenotes);
            up.close();

            break;

        }

    }

}

MainActivityPlanes.java

package android.application.project.planes;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class MainActivityPlanes extends Activity {

    Button Add;
    ListView List;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main_activity_planes);

        Add = (Button) findViewById(R.id.button1);
        List = (ListView) findViewById(R.id.listView1);

        List.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
                Intent launchActivity = new Intent(MainActivityPlanes.this,
                        MainActivityPaper.class);
                startActivity(launchActivity);
            }
        });

        Add.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(),
                        MainActivityPaper.class);
                startActivityForResult(intent, 0);
            }
        });

        MainActivityChampagne info = new MainActivityChampagne(this);
        info.open();
        ArrayList<String> data = info.getData();
        info.close();
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, data);
        List.setAdapter(arrayAdapter);

    }

}

Here What is wrong in your code is SQLite Database queries. You are created only on row in that table and trying to compare two values while updating. You just check you code you will be able to modify according to that.
Hope this will help.

Add the Following code to make your list-view on-click working

Add the following code in MainActivityPlanes.java

        List.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            String str1 = List.getItemAtPosition(arg2).toString();
            System.out.println(str1);
            Bundle bucket = new Bundle();
            bucket.putString("my_key", str1);
            // TODO Auto-generated method stub
            Intent launchActivity = new Intent(MainActivityPlanes.this,
                    MainActivityPaper.class);
            launchActivity.putExtras(bucket);
            startActivity(launchActivity);
        }
    });

Add the Following code in MainActivityPaper.java

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main_activity_paper);


    text1 = (TextView) findViewById(R.id.editText1);

    String s = null;
    try{
        Bundle b = this.getIntent().getExtras();
        s= b.getString("my_key");

        }catch(Exception e){
            s="";
            e.printStackTrace();
        }finally{
            text1.setText(s.toString());
        }
    title1 = (TextView) findViewById(R.id.editText3);
    save1 = (Button) findViewById(R.id.button3);
    update1 = (Button) findViewById(R.id.button5);

    save1.setOnClickListener(this);
    update1.setOnClickListener(this);

}

Hope this will solve your problem.

11

solved How to read and update SQLite database using ListView in Android?