0

I created a listview in Android, but now I am trying to allow the listview to be clickable to a new activity while passing parameters. The data for the listview is coming from an api. Code is below.

private static String urlString;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_market_place);

    urlString = "http://websiteforapi/api/market";
    new ProcessJSON().execute(urlString);
}

private class ProcessJSON extends AsyncTask<String, Void, String> {
    protected String doInBackground(String...strings){
        String stream = null;
        String urlString = strings[0];

        HTTPDataHandler hh = new HTTPDataHandler();
        stream = hh.GetHTTPData(urlString);

        // Return the data from specified url
        return stream;
    }

    protected void onPostExecute(String stream){

        //..........Process JSON DATA................
        if(stream !=null){
            try {
                // Get the full HTTP Data as JSONObject
                JSONObject reader = new JSONObject(stream);

                JSONArray businessArray = reader.getJSONArray("data");
                ArrayList<String> businesses = new ArrayList<String>();
                for(int i = 0; i < businessArray.length(); i++) {
                    JSONObject business_object_0 = businessArray.getJSONObject(i);
                    final String businessname = business_object_0.getString("name");
                    final String business = business_object_0.getString("business");
                    businesses.add(businessname);
                }

                ArrayAdapter<String> adapter = new ArrayAdapter<String>(getListView().getContext(),android.R.layout.simple_list_item_1, businesses);
                getListView().setAdapter(adapter);

            }catch(JSONException e){
                e.printStackTrace();
            }

        } // if statement end
    } // onPostExecute() end
} // ProcessJSON class end

The JSON I am using from the API is this...

{"data":[{"id":"3","name":"My Test Business 3","phone":"6785555555","email":"business5@email.com","address":"123 This Avenue","city":"Atlanta","state":"GA","zip":"30308","business":"Technology 33333"},{"id":"4","name":"My Test Business 2 ","phone":"6783571004","email":"test100@test.com","address":"123 Some Street","city":"Lawrenceville","state":"GA","zip":"30043","business":"Website building"},{"id":"5","name":"My Test Business 3","phone":"6785555555","email":"business2@email.com","address":"123 This Avenue","city":"Atlanta","state":"GA","zip":"30308","business":"Technology 3"},{"id":"6","name":"My Test Business 5","phone":"6785555555","email":"business6@email.com","address":"123 This Avenue","city":"Atlanta","state":"GA","zip":"30308","business":"Technology 4"}]}

So far I can get the ListView to display, but so far I am unable to make them clickable to a new activity.

teej2542
  • 577
  • 3
  • 10
  • 27

2 Answers2

0

You can use OnItemClickListener:

  getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            startActivity(this, NewActivity.class);
        }
    });

Of course the newActivity need to be register in manifest.

yshahak
  • 4,996
  • 1
  • 31
  • 37
0

Try this as your requirement...

yourListView.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        // Code for start new Activity
                        Intent intent = new Intent(MainActivity.this, NewActivity.class);
                        startActivity(intent);
                    }
                });

But why aren't you using master detail !!!

Esty
  • 1,882
  • 3
  • 17
  • 36
  • Not sure what you mean by master detail. But the code work. Is there a way to pass the parameters of the selected arraylist item to the next activity? I tried using the following. Intent intent = new Intent(MarketPlaceActivity.this, SelectedBusinessActivity.class); intent.putExtra("name", businesses); startActivity(intent); – teej2542 Aug 16 '15 at 05:04
  • see this for passing data [link](http://stackoverflow.com/questions/3510649/how-to-pass-a-value-from-one-activity-to-another-in-android) – Esty Aug 16 '15 at 06:06