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?
Asked
Active
Viewed 1.1k times
-3

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
-
1Stack 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 Answers
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
-
Thank you Shweta jain.. Actually 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:20
-
do you want to modify and update that data in to SQlite DB or Server DB? – shweta_jain Apr 25 '14 at 06:24
-
-
-
ya i created . for inserting data..again i updated to server Db that inserted data , then am having that data by searching .and displayed in listview..how to modifying and delete that searched data.. – user3454183 Apr 25 '14 at 06:31
-
-
you mean you asking for modifying ah? no i dont have that webservice.am having webservice for inserting and searching only – user3454183 Apr 25 '14 at 06:36
-
So you can modify and update data on server DB using that web services. now what you want? – shweta_jain Apr 25 '14 at 06:37
-
you mean you asking for modifying ah? no i dont have that webservice.am having webservice for inserting and searching only – user3454183 Apr 25 '14 at 06:41
-
So how can you modify and update server DB without Web Services. Its not possible. – shweta_jain Apr 25 '14 at 06:44
-
-
No way without WebServices. please tell me do you have Webservices for Delete DB from server? – shweta_jain Apr 25 '14 at 06:49
-
-
Ok so you can do this steps - 1) you search that data from server and store that data in to listview . 2) Then you modify that data and store that data in to SQLite DB 3) Then call Delete Webservices for clear all data from Server. 4) Then again insert Sqlite Database Entry on to server using webservices. But Please note this is not good practise for modify the data but if you dont have any way so you can do that way – shweta_jain Apr 25 '14 at 06:54
-
1st and 4th steps am having alredy..2nd and 3rd i dont have sample code..will you pls send me? – user3454183 Apr 25 '14 at 07:04
-
-
For Modify data please follow my above code snippet, And for Delete from server you have to use Webservices – shweta_jain Apr 25 '14 at 07:12
-
hmm ok thank you so much ,.i will catch you after doing that process – user3454183 Apr 25 '14 at 07:15
-
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