I want to store the list of item displayed in listview to android Sqlite Database [closed]

淺唱寂寞╮ 提交于 2020-01-30 13:25:29

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/23284889/i-want-to-store-the-list-of-item-displayed-in-listview-to-android-sqlite-databas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!