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(); 

            }
        });*/
    }
}
Ajay Patel
  • 5,298
  • 11
  • 55
  • 87

1 Answers1

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.

surhidamatya
  • 2,419
  • 32
  • 56
Robert
  • 1,272
  • 4
  • 21
  • 40
  • thanks robert ... now i am getting how to create database ,.... but how can i insert parsed xml data to sqlite database...
     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);
    
    see this code...
    – Ajay Patel Apr 19 '11 at 04: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 Apr 19 '11 at 05:39
  • yes correct ... ya i parsed it and generate list view... now i want to store all elements in sqlite... please help .. – Ajay Patel Apr 19 '11 at 06:11
  • see i am creating one app in which one php file on server side create xml file than using that xml file i wan to insert it xml data to local android device .. i think you know my concept ... one type of sync app between android device and server .. so i want to know how to store xml data to sqlite db – Ajay Patel Apr 19 '11 at 06:23
  • 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 Apr 19 '11 at 06:29
  • thanks a lot robert .. i think its batter to store first than display ... and also for short processAdd code.. thanks a lot will contact you via e mail if i need further help ...:) i am happy now – Ajay Patel Apr 19 '11 at 06:38
  • hay can you please write some more code ... about getting value of processAdd.. .. for (int j = 0; j < mylist.size();j++) { processAdd(mylist.get(j).get("id"), mylist.get(j).get("name"), mylist.get(j).get("id")); } and my function is private void processAdd(String string, String string2, String string3) { HOW TO GET VALUE HERE } – Ajay Patel Apr 19 '11 at 07:19
  • 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 Apr 19 '11 at 07:59
  • can you please chek your mail? – Ajay Patel Apr 19 '11 at 10:05