質問

I want to populate my listView with the data from SQLite Database. But before populating, I must create the database and populate it with some records.

I am having a sql file, which contains insert query and the respective values to be inserted as records.

Say, it contains query like, INSERT INTO 'tablename' (data1, data2) Values('1','2'),('3','4') etc.

So I just need to execute the same query in SQLite DB and inserting data in tables by mapping this query in SQLite DB form. For multiple records insertion I ran the following query in ThemeTourActivity.java, in which I want to show the ListView:

INSERT INTO 'tablename' SELECT 'data1' AS 'col1', 'data2' AS 'col2'
UNION SELECT 'value1', 'value2'
UNION SELECT 'value3', 'value4' 
and so on..

Below is my SQLiteOpenHelper class:

public class ToursOpenHelper extends SQLiteOpenHelper{
    public static final String DATABASE_NAME="WeTrip_DB";
    public static final int DATABASE_VERSION=1;
    //table's abstract implementations using BASECOLUMNS
    public static abstract class tourEntry implements BaseColumns
    {

        public static final String TABLE="tour";
        public static final String TOURTYPE="tour_type";
        public static final String TOURNAME="tour_name";
        public static final String NIGHTS="nights";
        public static final String DAYS="days";
        public static final String PIC="pic";
    }

    public static abstract class tourcatEntry implements BaseColumns
    {
        public static final String TABLE="tour_cat";
        public static final String TOURCAT_DESC="tourcat_desc";
        public static final String TOURCAT_NAME="tourcat_name";
        public static final String TOURCAT_IMG="tourcat_img";
    }

    public static abstract class tourdestEntry implements BaseColumns
    {
        public static final String TABLE="tour_dest";
        public static final String TOUR_ID="tour_id";
        public static final String DEST_ID="dest_id";
    }

    public static abstract class destEntry implements BaseColumns
    {
        public static final String TABLE="destination";
        public static final String DEST_NAME="dest_name";
    }

    //create DB TABLES
    public static final String CREATE_TABLE_TOUR="CREATE TABLE "+tourEntry.TABLE + 
            "(" 
            + tourEntry._ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
            tourEntry.TOURTYPE + " INTEGER ," +
            tourEntry.TOURNAME + " TEXT ," +
            tourEntry.NIGHTS + " INTEGER ," + 
            tourEntry.DAYS + " INTEGER ,"+
            tourEntry.PIC + " TEXT)";

    public static final String CREATE_TABLE_TOURCAT="CREATE TABLE "+tourcatEntry.TABLE + 
            "(" 
            + tourcatEntry._ID+" INTEGER PRIMARY KEY AUTOINCREMENT , "+
            tourcatEntry.TOURCAT_NAME + " TEXT ," +
            tourcatEntry.TOURCAT_DESC + " TEXT ," +
            tourcatEntry.TOURCAT_IMG + " TEXT)";

    public static final String CREATE_TABLE_TOURDEST="CREATE TABLE "+tourdestEntry.TABLE + 
            "(" 
            + tourdestEntry._ID+" INTEGER PRIMARY KEY AUTOINCREMENT , "+
            tourdestEntry.TOUR_ID + " INTEGER ," +
            tourdestEntry.DEST_ID + " INTEGER)";

    public static final String CREATE_TABLE_DEST="CREATE TABLE "+destEntry.TABLE + 
            "(" 
            + destEntry._ID+" INTEGER PRIMARY KEY AUTOINCREMENT , "+
            destEntry.DEST_NAME + " TEXT)";
    //DELETE DB TABLES
    public static final String DELETE_TABLE_TOUR="DROP TABLE "+tourEntry.TABLE + " IF EXISTS ";
    public static final String DELETE_TABLE_TOURCAT="DROP TABLE "+tourcatEntry.TABLE + " IF EXISTS ";
    public static final String DELETE_TABLE_TOURDEST="DROP TABLE "+tourdestEntry.TABLE + " IF EXISTS ";
    public static final String DELETE_TABLE_DEST="DROP TABLE "+destEntry.TABLE + " IF EXISTS ";

    public ToursOpenHelper(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_TOUR);
        db.execSQL(CREATE_TABLE_TOURCAT);
        db.execSQL(CREATE_TABLE_TOURDEST);
        db.execSQL(CREATE_TABLE_DEST);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL(DELETE_TABLE_TOUR);
        db.execSQL(DELETE_TABLE_TOURCAT);
        db.execSQL(DELETE_TABLE_TOURDEST);
        db.execSQL(DELETE_TABLE_DEST);
    }    
}

Below is my ThemeToursActivity.java in which listView of tours has to be populated from DataBase :

package com.example.travelplanner;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class ThemeTourActivity extends Activity implements OnItemClickListener {

    //ArrayList<ThemeTourModel> tour_details = themeTourResults();
    SQLiteDatabase db;
    ToursOpenHelper helper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_theme_tour);
        final ListView tourList = (ListView)findViewById(R.id.theme_tour_listview);
        tourList.setAdapter(new ThemeTourCustomAdapter(this,tour_details));
        tourList.setOnItemClickListener(this);

        helper = new ToursOpenHelper(this);
        db = helper.getWritableDatabase();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.theme_tour, menu);
        return true;
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub

    }

}

I am confused how to achieve this. I just want to create a database, insert multiple records in it and retrieve the data and populate the listView. It would be a great help if someone could guide me through this. I am stuck on it for the last couple of days.

役に立ちましたか?

解決

If you want to insert values then you can create function in SQLHelper class like this

public void InsertData(String Name,int Age,int Address)
    {
        ContentValues values = new ContentValues();  

        values.put("Name",Name); 
        values.put("Age", Age); 
        values.put("Address", Address);

        this.db.insert(Table_Name, null, values);
    }

(Here in Content values you will put Column Names and their values to store in Table);

and if you want to get this Values then do like this

public Cursor GetData() throws Exception 
    {   
        Cursor cursor;

        cursor = this.db.query(Table_Name,null, null, null, null, null, null,null );

        return cursor;
    }

if you want to get specific column values then you can pass column names array just after table name. It will return cursor so use cursor adapter for your list view.

Cursor c = db.GetData();


ListLayout mCursorAdapter = new ListLayout(
                        getApplicationContext(),                    // The application's Context object
                        R.layout.list_item,        // A layout in XML for one row in the ListView
                        c,                                          // The result from the query
                        new String[] {"Name"},      // A string array of column names in the cursor
                        new int[] { R.id.txtName },0);          // An integer array of view IDs in the row layout


                // Sets the adapter for the ListView
                lv.setAdapter(mCursorAdapter);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top