简体   繁体   中英

Retrieving data from Database into an ArrayList

I am trying to store data into arraylist and retrieve it in another activity. I am able to get data from database but not able to retrieve it .I am getting the path name instead of the result i have posted my logcat below kindly give me any suggestion on how to get it done

DBhelper class:

public ArrayList<LocationHelper> getAlllocations()
{
    ArrayList<LocationHelper> array_list = new ArrayList<LocationHelper>();

    //hp = new HashMap();
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor c =  db.rawQuery( "select * from "+ TABLE_LOCATION, null );
    c.moveToFirst();

    while(c.isAfterLast() == false){
        LocationHelper locationHelper = new LocationHelper();
        locationHelper.setNum(c.getString(1));

        locationHelper.setPresent_lat(c.getString(4));
        Log.e("Database lat: ", locationHelper.getPresent_lat());

        locationHelper.setPresent_lon(c.getString(5));
        Log.e("Database lon: ", locationHelper.getPresent_lon());

        array_list.add(locationHelper);
        Log.e("Database Data: ", String.valueOf(array_list.get(i)));

        c.moveToNext();
        i++;
    }
    return array_list;
}

LocationHelper:

public class LocationHelper {
String num;

public String getNative_lat() {
    return native_lat;
}

public void setNative_lat(String native_lat) {
    this.native_lat = native_lat;
}

public String getNative_lon() {
    return native_lon;
}

public void setNative_lon(String native_lon) {
    this.native_lon = native_lon;
}

public String getPresent_lat() {
    return present_lat;
}

public void setPresent_lat(String present_lat) {
    this.present_lat = present_lat;
}

public String getPresent_lon() {
    return Present_lon;
}

public void setPresent_lon(String present_lon) {
    Present_lon = present_lon;
}

public String getNum() {
    return num;
}

public void setNum(String num) {
    this.num = num;
}

String native_lat;
String  native_lon;
String present_lat;
String Present_lon;
}

Logcat:

08-21 15:28:51.364  25317-25317/? E/dalvikvm﹕ >>>>>  
com.example.xxx.xxx[ userId:0 | appId:10309 ]
08-21 15:28:51.684  25317-25317/? E/Database lat:﹕ 9.08613
08-21 15:28:51.684  25317-25317/? E/Database lon:﹕ 76.4888
08-21 15:28:51.684  25317-25317/? E/Database Data:﹕ 
com.example.xxx.xxx.LocationHelper@4215d348
08-21 15:28:51.684  25317-25317/? E/Database lat:﹕ 9.08613
08-21 15:28:51.684  25317-25317/? E/Database lon:﹕ 76.9888
08-21 15:28:51.684  25317-25317/? E/Database Data:﹕ 
com.example.xxx.xxx.LocationHelper@4215d868
08-21 15:28:51.684  25317-25317/? E/Database lat:﹕ 9.58613
08-21 15:28:51.684  25317-25317/? E/Database lon:﹕ 76.4888
08-21 15:28:51.684  25317-25317/? E/Database Data:﹕  
com.example.xxx.xxx.LocationHelper@4215db50
08-21 15:28:51.684  25317-25317/? E/Database lat:﹕ 9.58613
08-21 15:28:51.684  25317-25317/? E/Database lon:﹕ 76.9888
08-21 15:28:51.684  25317-25317/? E/Database Data:﹕   
com.example.xxx.xxx.LocationHelper@4215de38

Try

Log.e("Database Data: ", String.valueOf(array_list.get(i).getPresent_lat())); 

or

Log.e("Database Data: ", String.valueOf(array_list.get(i).getPresent_lon()));

instead of

Log.e("Database Data: ", String.valueOf(array_list.get(i)));

You are trying to print arraylist object not values.

EDIT:

In first activity pass arraylist through Intent

intent.putExtra("array_list", array_list);

then in second activity get arraylist like this.

ArrayList<LocationHelper> myList = (ArrayList<LocationHelper>) getIntent().getSerializableExtra("array_list");

Hope this helps!

As @gmetax said it the hashcode which is getting printed. Your ArrayList is getting populated exactly as you want. You need to pass that list to another activity using an Intent (for eg: intent.addExtras(list)) and iterate the list in another activity.

Try this in your Activity

ArrayList<LocationHelper> locatioHelper = new ArrayList<LocationHelper>();
LocationHelper locHelper = new LocationHelper();
DBHelper db = new DBHelper();
locationHelper=db.getAlllocations();
for(int i=0;i<locationHelper.size();i++)
{
  Log.i("latvalue",locHelper.get(i).getPresent_lat());
  Log.i("lonvalue",locHelper.get(i).getgetPresent_lon());
}

Your problem is that you haven't override the toString method, you are retrieving the content from db fine.

the default toString method from the object class returns

package.Class@Hashcode

add to your LocationHelper class that code

@Override
public String toString() {
    return "lat : " + getPresent_lat() + ", lon : " + getPresent_lon();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM