0

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
10
  • 1
    It is not a path. It's an object of ArrayList. You are getting lat and lon then what you want to do? Commented Aug 21, 2015 at 10:05
  • i Just want to know how to access the lat and lon from arraylist in another activity Commented Aug 21, 2015 at 10:10
  • I don't see you doing anything wrong nor can understand what you're trying to do. Commented Aug 21, 2015 at 10:11
  • post your LocationHelper class Commented Aug 21, 2015 at 10:11
  • 1
    why dont you just retrieve the ArrayList in the desired activity Commented Aug 21, 2015 at 10:12

4 Answers 4

1

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!

Sign up to request clarification or add additional context in comments.

1 Comment

thank you rajeev its working for me but can you tell me how to access the same from diff activity
1

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.

Comments

1

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

Comments

0

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

4 Comments

go override to get what you want and you are gonna be ok
It's not what @Nikhil wants. His question is so vague.
normally arraylist.get(i) gives you what data is present in that array at that postion but in my case i am getting my file path instead of values that i saved into it. i think there is something wrong with my code but not able to find it
YOU DON'T HAVE toString method on your class, so it returns package.Class@Hashcode as "string output" do you even read my answer? do you even try anything?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.