Domanda

I am making an application. I want to get some information out of a Json file on the web. This works. Now i want to have an onItemClickListener to the items of the listview with the content. The onItemClickListener must send the information of the clicked list to my new activity.

The code of my MainActiviy.java is:

package com.jitesh.androidjsonparser;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;


public class MainActivity extends ListActivity {
    private Context context;
    private static String url = "http://docs.blackberry.com/sampledata.json"; // URL VAN HET JSON BESTAND

    private static final String TAG_VTYPE = "vehicleType"; //VELDEN UIT DE JSON HALEN
    private static final String TAG_VCOLOR = "vehicleColor";
    private static final String TAG_FUEL = "fuel";
    private static final String TAG_TREAD = "treadType";
    private static final String TAG_OPERATOR = "approvedOperators";
    private static final String TAG_NAME = "name";
    private static final String TAG_POINTS = "experiencePoints";

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

    ListView lv ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new ProgressTask(MainActivity.this).execute();
    }

    private class ProgressTask extends AsyncTask<String, Void, Boolean> {
        private ProgressDialog dialog;

        private ListActivity activity;

        // private List<Message> messages;
        public ProgressTask(ListActivity activity) {
            this.activity = activity;
            context = activity;
            dialog = new ProgressDialog(context);
        }

        /** progress dialog to show user that the backup is processing. */

        /** application context. */
        private Context context;

        protected void onPreExecute() {
            this.dialog.setMessage("Progress start");
            this.dialog.show();
        }

        @Override
        protected void onPostExecute(final Boolean success) {
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
            ListAdapter adapter = new SimpleAdapter(context, jsonlist,
                    R.layout.list_item, new String[] { TAG_VTYPE, TAG_VCOLOR,
                            TAG_FUEL, TAG_TREAD }, new int[] {
                            R.id.vehicleType, R.id.vehicleColor, R.id.fuel,
                            R.id.treadType });

            setListAdapter(adapter);

            // selecting single ListView item
             lv = getListView();             
        }

        protected Boolean doInBackground(final String... args) {

            JSONParser jParser = new JSONParser();

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

            for (int i = 0; i < json.length(); i++) {

                try {
                    JSONObject c = json.getJSONObject(i);
                    String vtype = c.getString(TAG_VTYPE);

                    String vcolor = c.getString(TAG_VCOLOR);
                    String vfuel = c.getString(TAG_FUEL);
                    String vtread = c.getString(TAG_TREAD);

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

                    // adding each child node to HashMap key => value
                    map.put(TAG_VTYPE, vtype);
                    map.put(TAG_VCOLOR, vcolor);
                    map.put(TAG_FUEL, vfuel);
                    map.put(TAG_TREAD, vtread);
                    jsonlist.add(map);
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            return null;

        }


    }

}

Can anyone help me with the onItemClickListener and on which place i must set this onItemClickListener?

@Override protected void onListItemClick(ListView lv, View v, int position, long id) {       
         Intent i = new Intent(getApplicationContext(), single_list_item_view.class);     
         String vehicleType = ((TextView) v).getText().toString();   
         i.putExtra("TAG_VTYPE", vehicleType); 
         startActivity(i); 
 }
È stato utile?

Soluzione

You are extending ListActivity, so you do not need onItemClickListener. You can directly override onListItemClick(ListView l, View v, int position, long id). This method will be called when an item in the list is selected.

As you did with onCreate, you can do the same for onListItemClick

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    Intent i = new Intent(this, single_list_item_view.class);     
    HashMap<String, String> itemPressed = (HashMap<String, String>)l.getItemAtPosition(position); 
    String vehicleType = itemPressed.get(TAG_VTYPE);   
     i.putExtra("TAG_VTYPE", vehicleType); 
     startActivity(i); 
}

Altri suggerimenti

You need to create hashmap arraylist.

listview.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                  String name =urarraylist.get(position).get("name");// here you have to pas keynawhich is put inyour Hashmap arraylist

                  Intent i = new Intent(getBaseContext(),anotheractivity.class);
                  i.putExtra("name", name);

                  startActivity(i); 
                }
            });
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top