1

I have activity in which I show the list item and in list item I only want to show the name of places and I am able to show it but when I click on the item I want related to that place name it shows the detail on another activity i.e. location detail. so please help me to resolve my problem I am new in android.

here is my both activity code:

locationList=( ListView)findViewById(R.id.list_Surroundings);



    // Hashmap for ListView
    ArrayList<HashMap<String, String>> locationOfList = new ArrayList<HashMap<String, String>>();

    adapter = new SimpleAdapter(this, locationOfList,
            R.layout.list_item1,
            new String[] { TAG_NAME }, new int[] {
                    R.id.name});

    locationList.setAdapter(adapter);

 // Creating JSON Parser instance
    JSONParser jParser = new JSONParser();

 // getting JSON string from URL
    JSONObject json = jParser.getJSONFromUrl(url);

    try {
     // Getting Array of Category
    location=json.getJSONArray(TAG_LOCATION);

    //looping through all categories
    for(int i = 0; i < location.length(); i++){
        JSONObject c = location.getJSONObject(i);
        JSONObject c1=c.getJSONObject(TAG_LOCATION1);

        // Storing each json item in variable
            String LocationID = c1.getString(TAG_LOCATION_ID);
            String Name = c1.getString(TAG_NAME);
            String Phone = c1.getString(TAG_PHONE);
            String FormattedPhone = c1.getString(TAG_FORMATTED_PHONE);
            String Address = c1.getString(TAG_ADDRESS);
            String CrossStreet = c1.getString(TAG_CROSS_STREET);
            String Lat = c1.getString(TAG_LAT);
            String Lng = c1.getString(TAG_LNG);
            String Distance = c1.getString(TAG_DISTANCE);
            String PostalCode = c1.getString(TAG_POSTAL_CODE);
            String City = c1.getString(TAG_CITY);
            String State = c1.getString(TAG_STATE);
            String Country = c1.getString(TAG_COUNTRY);

        // creating new HashMap
           HashMap<String, String> map = new HashMap<String, String>();

        // adding each child node to HashMap key => value
           map.put(TAG_LOCATION_ID, LocationID);
           map.put(TAG_NAME, Name);
           map.put(TAG_PHONE, Phone);
           map.put(TAG_FORMATTED_PHONE, FormattedPhone);
           map.put(TAG_ADDRESS, Address);
           map.put(TAG_CROSS_STREET, CrossStreet);
           map.put(TAG_LAT, Lat);
           map.put(TAG_LNG, Lng);
           map.put(TAG_DISTANCE, Distance); 
           map.put(TAG_POSTAL_CODE, PostalCode);
           map.put(TAG_CITY, City);
           map.put(TAG_STATE, State);
           map.put(TAG_COUNTRY, Country);

           // adding HashList to ArrayList
           locationOfList.add(map);
    }
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }

    locationList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            // TODO Auto-generated method stub

             // getting values from selected ListItem
            String Name = ((TextView) view.findViewById(R.id.name)).getText().toString();
            String Distance = ((TextView) view.findViewById(R.id.distance_list1)).getText().toString();
            String Country = ((TextView) view.findViewById(R.id.country_list1)).getText().toString();
            String City = ((TextView) view.findViewById(R.id.city_list1)).getText().toString();
            String Phone = ((TextView) view.findViewById(R.id.phone_list1)).getText().toString();

         // Starting new intent
            Intent in = new Intent(getApplicationContext(), LocationDetails.class);
            in.putExtra(TAG_NAME, Name);
            in.putExtra(TAG_DISTANCE, Distance);
            in.putExtra(TAG_COUNTRY, Country);
            in.putExtra(TAG_CITY, City);
            in.putExtra(TAG_PHONE, Phone);

            startActivity(in);
        }
    });


public class LocationDetails extends Activity{

// JSON node keys
    private static final String TAG_NAME = "Name";
    private static final String TAG_PHONE = "Phone";
    private static final String TAG_ADDRESS = "Address";
    private static final String TAG_DISTANCE= "Distance";
    private static final String TAG_CITY = "City";
    private static final String TAG_COUNTRY= "Country";

    private TextView name,phone,address,distance,city,country;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.location_details);

        // getting intent data
        Intent in = getIntent();

        // Get JSON values from previous intent
        String Name = in.getStringExtra(TAG_NAME);
        String Phone = in.getStringExtra(TAG_PHONE);
        String Address = in.getStringExtra(TAG_ADDRESS);
        String Distance = in.getStringExtra(TAG_DISTANCE);
        String City = in.getStringExtra(TAG_CITY);
        String Country = in.getStringExtra(TAG_COUNTRY);

        // Displaying all values on the screen
        name= (TextView) findViewById(R.id.TxtView_name);
        phone=(TextView)findViewById(R.id.TxtView_PhoneField);
        address=(TextView)findViewById(R.id.TxtView_AddressField);
        distance=(TextView)findViewById(R.id.TxtView_DistanceField);
        city=(TextView)findViewById(R.id.TxtView_CityField);
        country=(TextView)findViewById(R.id.TxtView_CountryField);

        name.setText(Name);
        phone.setText(Phone);
        address.setText(Address);
        distance.setText(Distance);
        city.setText(City);
        country.setText(Country);
    }
}
CraigTeegarden
  • 8,173
  • 8
  • 38
  • 43
user2092132
  • 137
  • 2
  • 3
  • 13
  • 1
    What problem are you facing ? – Swayam Apr 13 '13 at 16:15
  • i write on the top actually i am able to get list view item but when i click on it i want related to that name it shows the detail like phone no. ,distance , country n all. – user2092132 Apr 13 '13 at 16:20

2 Answers2

1

In sending activity, try replacing

in.putExtra(TAG_NAME, Name);
in.putExtra(TAG_DISTANCE, Distance);
in.putExtra(TAG_COUNTRY, Country);
in.putExtra(TAG_CITY, City);
in.putExtra(TAG_PHONE, Phone);

with

Bundle extras = new Bundle();
extras.putString(TAG_NAME, Name);
extras.putString(TAG_DISTANCE, Distance);
extras.putString(TAG_COUNTRY, Country);
extras.putString(TAG_CITY, City);
extras.putString(TAG_PHONE, Phone);
extras.putExtras(extras);

In receiving activity, try replacing

// getting intent data
        Intent in = getIntent();

        // Get JSON values from previous intent
        String Name = in.getStringExtra(TAG_NAME);
        String Phone = in.getStringExtra(TAG_PHONE);
        String Address = in.getStringExtra(TAG_ADDRESS);
        String Distance = in.getStringExtra(TAG_DISTANCE);
        String City = in.getStringExtra(TAG_CITY);
        String Country = in.getStringExtra(TAG_COUNTRY);

with

Bundle extras = getIntent().getExtras();

if(extras != null) {

    String Name = extras.getString(TAG_NAME);
    String Phone = extras.getString(TAG_PHONE);
    String Address = extras.getString(TAG_ADDRESS);
    String Distance = extras.getString(TAG_DISTANCE);
    String City = extras.getString(TAG_CITY);
    String Country = extras.getString(TAG_COUNTRY);  

}
0

You are trying to get the data from the TextViews, which is not at all right.

You need to fetch the values of the selected item from the ArrayList by using locationOfList(position) inside the onClick() of the listItem.

Something like this :

 locationList.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // TODO Auto-generated method stub

        // getting values from selected ListItem
        HashMap<String, String> selectedMap =  locationOfList.get(position);           

     // Starting new intent
        Intent in = new Intent(getApplicationContext(), LocationDetails.class);
        in.putExtra(selectedMap);

        startActivity(in);
    }
});

And in your second Activity, use this example to retrieve the data from the HashMap.

Community
  • 1
  • 1
Swayam
  • 16,294
  • 14
  • 64
  • 102
  • i can't understand if you are able to do what you are try to explaining me then i an thankful to you @swayam – user2092132 Apr 13 '13 at 16:38
  • In your code, you are trying to get the data of the item which has been clicked by using the content of the TextViews. That is wrong. You need to identify the element which has been clicked and then look in your arrayList to find the map at that position and extract the info from there. I hope I made sense. – Swayam Apr 13 '13 at 16:43
  • if i know how to do this i am not asking here if you give me a code that you explaining me changes in above code then its better. – user2092132 Apr 13 '13 at 16:58
  • when i used your code it shows an error on locationOfList that "Cannot refer to a non-final variable locationOfList inside an inner class defined in a different method" so what to do next. @swayam – user2092132 Apr 14 '13 at 03:53
  • Yeah, that is because you have declared it inside your onCreate(). Declare it as a global class variable i.e. not inside any function. – Swayam Apr 14 '13 at 07:38
  • and on receiver side what to do actually i show that link in this Iterator use so what it actually do i dont have idea so please post according to that which you post above or directly give the code so that i apply it on receiver side @swayam – user2092132 Apr 14 '13 at 09:53
  • use `map.get(yourKey)` to the receiver's side to extract the info from the map. http://developer.android.com/reference/java/util/HashMap.html#get(java.lang.Object) – Swayam Apr 14 '13 at 16:04