Android OnClickListener a nueva actividad

I need help passing data and position from OnClickListener to a new activity. Below are my attempt. I cant seem to figure it out. Please assist. Thanks


public class MainActivity extends Activity {

    JSONObject json;
    JSONArray jsonarray;
    ListView listview;
    ProgressDialog pDialog;
    ListViewAdapter adapter;
    ArrayList<HashMap<String, String>> arraylist;
    static String ID = "id";
    static String RANK = "rank";
    static String COUNTRY = "country";
    static String POPULATION = "population";
    static String FLAG = "flag";

    public void onCreate(Bundle savedInstanceState) {

        new DownloadJSON().execute();

    // DownloadJSON AsyncTask
    private class DownloadJSON extends AsyncTask<Void, Void, Void> {

        protected void onPreExecute() {

            pDialog = new ProgressDialog(MainActivity.this);



        protected Void doInBackground(Void... params) {

            arraylist = new ArrayList<HashMap<String, String>>();

            json = JSONfunctions

            try {

                jsonarray = json.getJSONArray("worldpopulation");

                for (int i = 0; i < jsonarray.length(); i++) {
                    HashMap<String, String> map = new HashMap<String, String>();
                    json = jsonarray.getJSONObject(i);
                    map.put("id", String.valueOf(i));
                    map.put("rank", json.getString("rank"));
                    map.put("country", json.getString("country"));
                    map.put("population", json.getString("population"));
                    map.put("flag", json.getString("flag"));
            } catch (JSONException e) {
                Log.e("Error", e.getMessage());
            return null;

        protected void onPostExecute(Void args) {
            listview = (ListView) findViewById(;
            adapter = new ListViewAdapter(MainActivity.this, arraylist);

Clase de adaptador

    public class ListViewAdapter extends BaseAdapter {

    // Declare Variables
    Context context;
    LayoutInflater inflater;
    ArrayList<HashMap<String, String>> data;
    DownloadImageTask mTask;
    ImageLoader imageLoader;

    public ListViewAdapter(Context context,
            ArrayList<HashMap<String, String>> arraylistview) {
        this.context = context;
        data = arraylistview;
        imageLoader = new ImageLoader(context);


    public int getCount() {
        return data.size();

    public Object getItem(int position) {
        return null;

    public long getItemId(int position) {
        return 0;

    public View getView(int position, View convertView, ViewGroup parent) {
        TextView rank;
        TextView country;
        TextView population;
        ImageView flag;

        inflater = (LayoutInflater) context

        View itemView = inflater.inflate(R.layout.listview_item, parent, false);

        HashMap<String, String> result = new HashMap<String, String>();
        result = data.get(position);
        rank = (TextView) itemView.findViewById(; // title
        country = (TextView) itemView.findViewById(; // title
        population = (TextView) itemView.findViewById(; // artist
        flag = (ImageView) itemView.findViewById(; // artist

        imageLoader.DisplayImage(result.get(MainActivity.FLAG), flag);

        itemView.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {
                Intent intent = new Intent(context, SingleItemView.class);
                intent.putExtra(COUNTRY, country);
                intent.putExtra(POPULATION, population);
                intent.putExtra(FLAG, flag);


        return itemView;


public class SingleItemView extends Activity {

    // Declare Variables
        TextView txtrank;
        TextView txtcountry;
        TextView txtpopulation;
        ImageView imgflag;
        String rank;
        String country;
        String population;
        String flag;
        int position;

        public void onCreate(Bundle savedInstanceState) {

            Intent i = getIntent();

            position = i.getExtras().getInt("position");

            rank = i.getStringExtra("rank");

            country = i.getStringExtra("country");

            population = i.getStringExtra("population");

            flag = i.getStringExtra("flag");

            txtrank = (TextView) findViewById(;
            txtcountry = (TextView) findViewById(;
            txtpopulation = (TextView) findViewById(;



preguntado el 05 de mayo de 13 a las 18:05

¿Cuál parece ser el problema? -

i dont see any issue yet, consider posting SingleItemView.class -

I cant figure out this part intent.putExtra(MainActivity.RANK); it needs 2 strings and also passing the position -

can we see how RANK is defined -

please check my codes. thanks -

2 Respuestas

I cant figure out this part intent.putExtra(MainActivity.RANK); it needs 2 strings and also passing the position

putExtra() takes two arguments, because you have to pass in a value and a name.

You can do this to pass data:

intent.putExtra("Name", value);


How do you pass the position too?

What position? Your adapter only provides the View for each list row. If you want to get the position of the item in a ListView (or whatever) you have to set an onItemClickListener a su ListView.

contestado el 05 de mayo de 13 a las 19:05

how do you pass the position too? - KC Chai

can you do this in onClick:


contestado el 05 de mayo de 13 a las 18:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.