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

Inicio

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";

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

        new DownloadJSON().execute();
    }

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

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            pDialog = new ProgressDialog(MainActivity.this);

            pDialog.setMessage("Loading...");
            pDialog.setIndeterminate(false);

            pDialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {

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

            json = JSONfunctions
                    .getJSONfromURL("http://www.site.com");

            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"));
                    arraylist.add(map);
                }
            } catch (JSONException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void args) {
            listview = (ListView) findViewById(R.id.listview);
            adapter = new ListViewAdapter(MainActivity.this, arraylist);
            listview.setAdapter(adapter);
            //setListAdapter(adapter);
            pDialog.dismiss();

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

    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    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
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        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(R.id.rank); // title
        country = (TextView) itemView.findViewById(R.id.country); // title
        population = (TextView) itemView.findViewById(R.id.population); // artist
        flag = (ImageView) itemView.findViewById(R.id.flag); // artist

        rank.setText(result.get(MainActivity.RANK));
        country.setText(result.get(MainActivity.COUNTRY));
        population.setText(result.get(MainActivity.POPULATION));
        imageLoader.DisplayImage(result.get(MainActivity.FLAG), flag);

        itemView.setOnClickListener(new OnClickListener() {

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

            }
        });

        return itemView;
    }
}

SingleItemView

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;

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

            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(R.id.rank);
            txtcountry = (TextView) findViewById(R.id.country);
            txtpopulation = (TextView) findViewById(R.id.population);


            txtrank.setText(rank);
            txtcountry.setText(country);
            txtpopulation.setText(flag);

            //imgflag.setImageResource(flag);
        }

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

Edit:

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:

 intent.putExtra(MainActivity.RANK,rank.getText());

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.