I have an adapter for the listview , I need to redo it so that when I click it, the link that is specified in the link open. Launching on the phone, shows Here is the Adapter code

  import android.app.ProgressDialog; import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Toast; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; import java.util.HashMap; public class MainActivity extends AppCompatActivity { private String TAG = MainActivity.class.getSimpleName(); private ProgressDialog pDialog; ListView lv; // URL to get contacts JSON private static String url = "Тут URL"; ArrayList<HashMap<String, String>> contactList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); contactList = new ArrayList<>(); lv = (ListView) findViewById(R.id.list); new GetContacts().execute(); } /** * Async task class to get json by making HTTP call */ private class GetContacts extends AsyncTask<Void, Void, Void> { @Override protected void onPreExecute() { super.onPreExecute(); // Showing progress dialog pDialog = new ProgressDialog(MainActivity.this); pDialog.setMessage("Please wait..."); pDialog.setCancelable(false); pDialog.show(); } @Override protected Void doInBackground(Void... arg0) { HttpHandler sh = new HttpHandler(); // Making a request to url and getting response String jsonStr = sh.makeServiceCall(url); Log.e(TAG, "Response from url: " + jsonStr); if (jsonStr != null) { try { JSONObject jsonObj = new JSONObject(jsonStr); // Getting JSON Array node JSONArray data = jsonObj.getJSONArray("data"); // looping through All Contacts for (int i = 0; i < data.length(); i++) { JSONObject c = data.getJSONObject(i); String id = c.getString("id"); String name = c.getString("name"); String link = c.getString("link"); // tmp hash map for single contact HashMap<String, String> contact = new HashMap<>(); // adding each child node to HashMap key => value contact.put("id", id); contact.put("name", name); contact.put("link", link); // adding contact to contact list contactList.add(contact); } } catch (final JSONException e) { Log.e(TAG, "Json parsing error: " + e.getMessage()); runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(), "Json parsing error: " + e.getMessage(), Toast.LENGTH_LONG) .show(); } }); } } else { Log.e(TAG, "Couldn't get json from server."); runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(), "Couldn't get json from server. Check LogCat for possible errors!", Toast.LENGTH_LONG) .show(); } }); } return null; } @Override protected void onPostExecute(Void result) { super.onPostExecute(result); // Dismiss the progress dialog if (pDialog.isShowing()) pDialog.dismiss(); /** * Updating parsed JSON data into ListView * */ ListAdapter adapter = new SimpleAdapter( MainActivity.this, contactList, R.layout.list_item, new String[]{"name", "link",}, new int[]{R.id.name, R.id.link}); lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse(link[position-1])); startActivity(i); } }); lv.setAdapter(adapter); lv.setAdapter(adapter); } } } 

Update That is, I have a parsed array, it has an id , name , link , I need to do so that when I click on for example the first name , the link will open. which is written in link . Inserted the full code, maybe that sticks to it.

Update 2 I wrote the code, only now when onItemClick crashes

  • Maybe I'm wrong, but there is no adapter code. - Nikotin N

2 answers 2

You can do differently. Either redefine the adapter, create your own, custom and in it in getView hang the listener of clicks on the markup, or hang the listener of clicks on the ListView itself like this:

 lb.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapter, View v, int position, long arg3) { String url = (String)adapter.getItemAtPosition(position); //запускаем браузер по ссылке Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse(url)); startActivity(i); } }); 
  • Does not work, maybe through getView try? - Shufler3
  • What does it mean does not work? - JuriySPb

It is better to write a custom adapter and in the ViewHoldere handle click