0

I want to store the data which is shown by setListAdapter to sqlite .. how ca i ?? please help me

XML File http://p-xr.com/xml

package com.pxr.tutorial.xmltest;

import java.util.ArrayList;
import java.util.HashMap;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class Main extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listplaceholder);

        ArrayList> mylist = new ArrayList>();


        String xml = XMLfunctions.getXML();
        Document doc = XMLfunctions.XMLfromString(xml);

        int numResults = XMLfunctions.numResults(doc);

        if((numResults  map = new HashMap();    

            Element e = (Element)nodes.item(i);
            map.put("id", XMLfunctions.getValue(e, "id"));
            map.put("name", "Naam:" + XMLfunctions.getValue(e, "name"));
            map.put("Score", "Score: " + XMLfunctions.getValue(e, "score"));

            mylist.add(map);            
        }       

        ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main, 
                        new String[] { "id","name", "Score" }, 
                        new int[] {R.id.id, R.id.item_title, R.id.item_subtitle });

        setListAdapter(adapter);

       /* final ListView lv = getListView();
        lv.setTextFilterEnabled(true);  
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View view, int position, long id) {              
                @SuppressWarnings("unchecked")
                HashMap o = (HashMap) lv.getItemAtPosition(position);                   
                Toast.makeText(Main.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_LONG).show(); 

            }
        });*/
    }
}

1 Answer 1

2

I suggest you parse the data first then save it into database and then show.

If you didn't create database you have to do it first, sample code how to do it bellow

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

/**
 * 
 * @author Robert
 *
 */
public class DatabaseHelper extends SQLiteOpenHelper{

        /**
         * Default constructor- creates database
         * @param context
         */
        public DatabaseHelper(Context context) {
                super(context, DATABASE_NAME, null, 1);
        }

        /**
         * Implemented methood from SQLiteOpenHelper- called when
         * constructor of DatabaseHelper is called
         * Creates three tables (TRIP, POINT, TRANSFER) with relations
         * TRAVEL-POINT and POINT TRANSFER
         * @see SQLiteOpenHelper
         */
        @Override
        public void onCreate(SQLiteDatabase db) {
                db.execSQL("CREATE TABLE " + TRIP_TABLE_NAME
                                + "(_id INTEGER PRIMARY KEY AUTOINCREMENT, "
                                + FROM + " TEXT, "
                                + TO + " TEXT);");
                db.execSQL("CREATE TABLE " + TRANSFER_TABLE_NAME
                                + "(_id INTEGER PRIMARY KEY AUTOINCREMENT, "
                                + NOTE + " TEXT, "
                                + TIME + " TEXT, "
                                + DATE + " TEXT, "
                                + ADDRESS + " TEXT, "
                                + CITY + " TEXT, "
                                + COUNTRY + " TEXT, "
                                + LATITUDE + " TEXT, "
                                + LOGITUDE + " TEXT, "
                                + TRANFER_TYPE + " TEXT, "
                                + FK_POINT + " INTEGER NOT NULL REFERENCES "+ POINT_TABLE_NAME +"(_id)"
                                + "ON DELETE CASCADE);");
                db.execSQL("CREATE TABLE " + POINT_TABLE_NAME
                                + "(_id INTEGER PRIMARY KEY AUTOINCREMENT, "
                                + NAME + " TEXT, "
                                + ADDRESS + " TEXT, "
                                + CITY + " TEXT, "
                                + COUNTRY + " TEXT, "
                                + LATITUDE + " TEXT, "
                                + LOGITUDE + " TEXT, "
                                + FK_TRAVEL + " INTEGER NOT NULL REFERENCES "+ TRIP_TABLE_NAME +"(_id)"
                                + "ON DELETE CASCADE);");
        }

        /**
         * Implemented method from SQLiteOpenHelper- we don't use it now
         * @see SQLiteOpenHelper
         */
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        }

        /**
         * Private fields, mainly names of columns and tables in database
         */
        private static final String DATABASE_NAME = "TRAVELER_NOTEBOOK";

        //Table names
        private static final String TRIP_TABLE_NAME = "TRIP";
        private static final String POINT_TABLE_NAME = "POINT";
        private static final String TRANSFER_TABLE_NAME = "TRANSFER";

        //Columns names
        private static final String FROM = "FROM_PLACE";
        private static final String TO = "TO_PLACE";

        private static final String NAME = "NAME";
        private static final String ADDRESS = "ADDRESS";
        private static final String CITY = "CITY";
        private static final String COUNTRY = "COUNTRY";
        private static final String LATITUDE = "LATITUDE";
        private static final String LOGITUDE = "LOGITUDE";

        private static final String NOTE = "NOTE";
        private static final String TIME = "TIME";
        private static final String DATE = "DATE";
        private static final String TRANFER_TYPE = "TRANSFER_TYPE";

        //Foregin keys name
        private static final String FK_TRAVEL = "FK_TRAVEL_ID";
        private static final String FK_POINT = "FK_POINT_ID";

}

The next step is to get writable database in your activity, you can do it by creating

SQLiteDatabase database = (new DatabaseHelper(this).getWritableDatabase());

where DatabaseHelper is name of your database helper class. If you want to insert something into database just make someting simillar like bellow:

        private void processAdd(values_to_save) {
                ContentValues values = new ContentValues(number_of_values);
                values.put(column_name, value);
                values.put(column_name, value);
                if(database!=null){
                database.insert(TABLE_NAME, null, values);
                } else{
                    Log.e("MyAppError", "Database is null");
                }
            }

ContentValue is something like HashMap key-value where key is column name and value- that what you want insert. Remember that in each table you should have column "_id primary key auto increment" as I remember good you don't have to but it make's your project more complicated.

9
  • thanks robert ... now i am getting how to create database ,.... but how can i insert parsed xml data to sqlite database... <pre> ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main, new String[] { "id","name", "Score" }, new int[] {R.id.id, R.id.item_title, R.id.item_subtitle }); setListAdapter(adapter); </pre> see this code...
    – Ajay Patel
    Commented Apr 19, 2011 at 4:48
  • 1
    Ok so you already parsing xml and building listview, am i right? And tell me you need to put all of this elements or selected one?
    – Robert
    Commented Apr 19, 2011 at 5:39
  • yes correct ... ya i parsed it and generate list view... now i want to store all elements in sqlite... please help ..
    – Ajay Patel
    Commented Apr 19, 2011 at 6:11
  • 1
    In my opinion it'll be better to store it first and than generate view with elements;). As i see you have a list with hashmap so made database with table with 3 columns, made for example for(i = 0; i < mylist.size(); i++) inside put function processAdd with three parameters which you will have to put into ContentValue i'll looks like processAdd(mylist.get(i).get("id"), mylist.get(i).get("name"), mylist.get(i).get("id")); processAdd you should create according to function which I gave you up ;) As I said if you have any more question write me e-mail:)
    – Robert
    Commented Apr 19, 2011 at 6:29
  • 1
    private void processAdd(String str1, String str2, String str3) { ContentValues values = new ContentValues(3); values.put(column_name, str1); values.put(column_name, str2);values.put(column_name, str3) if(database!=null){ database.insert(TABLE_NAME, null, values); } else{ Log.e("MyAppError", "Database is null"); } } Better just send me code
    – Robert
    Commented Apr 19, 2011 at 7:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.