Alex M. Alex M. - 1 month ago 8
JSON Question

How to display programmatically the id from a database using setOnItemClickListener on a ListView

I have created a ListView in which i want to display some records from a database. This is working fine. I also want when someone clicks an item from the ListView, the correct id and product name to be displayed. In my case, every time i click an item, it displayed only the last record. What can i do to solve this problem?

Here is my code:

public static final String ID = "id";
public static final String PRODUCT_NAME = "productName";

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, showProductsUrl, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("products");

for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
final String productId = jsonObject.getString("id");
final String productName = jsonObject.getString("productName");
HashMap<String, String> product = new HashMap<>();
product.put(ID, productId);
product.put(PRODUCT_NAME, productName);
productList.add(product);

String[] from = {PRODUCT_NAME};
int[] to = {R.id.productName};

ListAdapter adapter = new SimpleAdapter(getApplicationContext(), productList, R.layout.list_products_to_buy, from, to);

ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
System.out.println(productId + " " + productName);
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}


Thanks in advance!

Answer

Firstly, do not declare the adapter repeatedly inside the loop.

If you want the ID, you need to add it to the from array.

public static final String ID = "id";
public static final String PRODUCT_NAME = "productName";

String[] from = { ID, PRODUCT_NAME };
int[] to = { R.id.productId,  R.id.productName };

final ListAdapter adapter = new SimpleAdapter(YourActivity.this, productList, R.layout.list_products_to_buy, from, to);

ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Object clicked = adapter.getItem(position); // For example

        // TODO: Somehow extract that data from 'clicked'

        System.out.println(productId + " " + productName);
    }
});

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, showProductsUrl, new Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject response) {
        try {
            JSONArray jsonArray = response.getJSONArray("products");

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                final String productId = jsonObject.getString("id");
                final String productName = jsonObject.getString("productName");
                HashMap<String, String> product = new HashMap<>();
                product.put(ID, productId);
                product.put(PRODUCT_NAME, productName);
                productList.add(product);
            }

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

    }
}); 

// TODO: Add to Volley request queue