-3

I have list of items in ListView which was fetched from server. My question is: How to store that list of items displayed in listview to Android Sqlite local database?

Mike Kinghan
  • 55,740
  • 12
  • 153
  • 182
user3454183
  • 21
  • 1
  • 1
  • 6
  • simply get items from list and create db and run insert query – raj Apr 25 '14 at 05:37
  • 1
    Stack Overflow is not a free code writing service. You need to show some effort into solving the problem yourself before you can expect any assistance from us. As it stands your question is likely to be voted down and closed as not providing enough information and for not showing any research. – AdrianHHH Apr 25 '14 at 05:58
  • I dont Want full coding..Actually Am created one project already by own am created insert operation from webservice to sqlite.Again i updated to server and again i searched that data from server and displayed in listview..now i want to modifying and deleting that searched listview data and wants to update again.. is it posible or not? – user3454183 Apr 25 '14 at 06:08

2 Answers2

2

Try Below code -

First you have to create DatabaseHandler Class

DatabaseHandler.java-

package com.androidhive.androidsqlite;

import java.util.ArrayList;
import java.util.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.util.Log;

public class DatabaseHandler extends SQLiteOpenHelper {

    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "MyDatabase";

    // Database table name
    private static final String TABLE_LIST = "MyListItem";

    // Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_ListItem = "listitem";

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_LIST_TABLE = "CREATE TABLE " + TABLE_LIST + "(" + KEY_ID
                + " INTEGER," + KEY_ListItem + " TEXT" + ")";

        db.execSQL(CREATE_LIST_TABLE);
    }

    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_LIST);

        // Create tables again
        onCreate(db);
    }



    void addListItem(ArrayList<String> listItem) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        for (int i = 0; i < listItem.size(); i++) {

            Log.e("vlaue inserting==", "" + listItem.get(i));
            values.put(KEY_ListItem, listItem.get(i));
            db.insert(TABLE_LIST, null, values);

        }

        db.close(); // Closing database connection
    }

    Cursor getListItem() {
        String selectQuery = "SELECT  * FROM " + TABLE_LIST;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        return cursor;
    }

}

MainActivity.java

package com.androidhive.androidsqlite;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */

    ArrayList<String> your_list_arrayArrayList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        your_list_array ArrayList = new ArrayList<String>();

        your_list_arrayArrayList.add("Item1");
        your_list_arrayArrayList.add("Item2");
        your_list_arrayArrayList.add("Item3");
        your_list_arrayArrayList.add("Item4");
        your_list_arrayArrayList.add("Item5");

        DatabaseHandler db = new DatabaseHandler(this);

        db.addListItem(your_list_arrayArrayList);

        Cursor cursor = db.getListItem();

        Log.e("count", " " + cursor.getCount());
        if (cursor != null) {
            cursor.moveToNext();

            do {

                Log.e("value==", "" + cursor.getString(1));

            } while (cursor.moveToNext());
        }
    }
}

If its not working please let me know I will try to help you more.

Pranav P
  • 1,755
  • 19
  • 39
shweta_jain
  • 453
  • 1
  • 5
  • 19
0

Just get all the list of items and store it into SQLite database. For storing data to SQLite database go to here

Akash Goswami
  • 306
  • 3
  • 14
  • Actually Am having searched data in listview..That listview items wants to be stored in sqlite database. – user3454183 Apr 25 '14 at 05:47
  • Do you have access to those searched data , if you have it just you need to store it either in a list or a string array and then create a database and a table and then pass the variable and store those data to table.. at what point you are facing problem? – Akash Goswami Apr 25 '14 at 05:50
  • Am having searched data from server and presented in listview . now i want to modifying and deleting that searched listview data and wants to update again..This only i want is it posible doing directly ..please help me providing sample code – user3454183 Apr 25 '14 at 06:00
  • You mean to say that you have a listview with some items and you want those items to be edited in listview itself ..right? If you want to do so, go through this URL [link](http://stackoverflow.com/questions/9015853/android-how-to-make-edittext-editable-inside-a-listview) , this may help you out.. – Akash Goswami Apr 25 '14 at 06:09