0
votes

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

1 Answers

2
votes

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.