Note that there are some explanatory texts on larger screens.

plurals
  1. POGet database ID from ListView
    primarykey
    data
    text
    <p>I get data from database (id, name) and I want to display (name) in a ListView. When user clicks I need to get database (id) to perform an action. I get it working but my solution seems complex for such a simple thing. I would like somehow to store (id) in the ListView in a hidden way and beeing able to retrieve it when item is selected by user. Here is my solution:</p> <pre><code>class Route { //structure to store the data in ListView public int id; public String name; public Route (int Id, String Name) { id = Id; name = Name; } } // Create a custom adapter, we also created a corresponding // layout (route_row) for each item of the ListView public class MySimpleArrayAdapter extends ArrayAdapter&lt;Route&gt; { private final Context context; private final String[] values; public MySimpleArrayAdapter(Context context, ArrayList&lt;Route&gt; routes) { super(context, R.layout.route_row, routes); this.context = context; //Get the list of string array to display in the ListView String[] values = new String[routes.size()]; //Loop around all the items to get the list of values to be displayed for (int i=0; i&lt;routes.size(); i++) values[i] = routes.get(i).id + " - " + routes.get(i).name; //We added route.id to route.name for debugging but route.id is not necessary this.values = values; //String array used to display data in the ListView } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View rowView = inflater.inflate(R.layout.route_row, parent, false); TextView textView = (TextView) rowView.findViewById(R.id.routeName); textView.setText(values[position]); return rowView; } } //output is a JSON array composed of JSON object routes void DisplayListView(String output) { (id, name) ListView listView = (ListView) findViewById(R.id.listView1); ArrayList&lt;Route&gt; list = new ArrayList&lt;Route&gt;(); //Convert the JSON to ArrayList&lt;Route&gt; try { JSONArray json = new JSONArray(output); //Get JSON array JSONObject jsonObj; int id; String name; for(int i=0;i&lt;json.length();i++) { jsonObj = json.getJSONObject(i); //Get each JSON object id = jsonObj.getInt("Id"); name = jsonObj.getString("Name"); list.add( new Route(id, name)); } } catch (Exception ex) { Log.w("Commute", ex.toString()); ex.printStackTrace(); } //Create ArrayAdapter MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(getApplicationContext(), list); // Assign adapter to ListView listView.setAdapter(adapter); //Set a listener listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) { Toast.makeText(getApplicationContext(), "Click ListItem Number " + ((Route)parent.getItemAtPosition(position)).id, Toast.LENGTH_LONG) .show(); //It works when user clicks we display route.id } }); } </code></pre> <p>Is there not a simpler way to do this ? I found similar questions but no simple or clear answer.<br/> Can I avoid the custom adapter ? I just want to display a simple ListView with a text for each line.<br/> Can I avoid to loop around the ArrayAdapter to create the String array for the adpater ? That seems really an unefficient way to do.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload