I want to send listview data to another activity in android

advertisements
package com.example.selflistview;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
 TextView text;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        connect();
    }
    private void connect() {
      String data;
      List<String> r = new ArrayList<String>();
      ArrayAdapter<String>adapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,r);
      ListView list=(ListView)findViewById(R.id.listView1);
        try {
            DefaultHttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet("http://localhost/listview.php");
            HttpResponse response = client.execute(request);
            HttpEntity entity=response.getEntity();
            data=EntityUtils.toString(entity);
            Log.e("STRING", data);
            try {

       JSONArray json=new JSONArray(data);
       for(int i=0;i<json.length(); i++)
       {
        JSONObject obj=json.getJSONObject(i);
        String name=obj.getString("name");
        String year=obj.getString("year");
        String age=obj.getString("age");
        Log.e("STRING", name);
        r.add(name);
        //r.add(year);
        //r.add(age);
        list.setAdapter(adapter);

        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
                // TODO Auto-generated method stub
                //final String item = (String) arg0.getItemAtPosition(arg2);

                Toast.makeText(getBaseContext(), "arg2 : "+arg2, Toast.LENGTH_SHORT).show();
                Intent view_det = new Intent(getBaseContext(),ViewDetail.class);
                view_det.putExtra("position",arg2);
                startActivity(view_det);

            }
        });
       }

      } catch (JSONException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }

        } catch (ClientProtocolException e) {
            Log.d("HTTPCLIENT", e.getLocalizedMessage());
        } catch (IOException e) {
            Log.d("HTTPCLIENT", e.getLocalizedMessage());
        }
    }
}

I want to send Year, name and age to another activity, i am able to get the positions at intent but not the other values. please help...

I am able to see those values in list., but not able to call at intent section.

Your help will be appreciated..


  1. first declare your JSONArray json as class variable.
  2. Initialize it when you get data new JSONArray(data);.
  3. When you get the list item position then access that object from that json array.
  4. get the attributes from the object and pass in intent to another activity.