如何使用ListView上的setOnItemClickListener以编程方式显示数据库中的id

时间:2016-11-01 23:12:12

标签: android json final

我创建了一个ListView,我想在其中显示数据库中的一些记录。这工作正常。我还想当有人点击ListView中的项目时,显示正确的ID和产品名称。在我的情况下,每次我点击一个项目,它只显示最后一条记录。我该怎么做才能解决这个问题?

这是我的代码:

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();
        }
    }
}

提前致谢!

1 个答案:

答案 0 :(得分:1)

首先,不要在循环内重复声明适配器。

如果您需要ID,则需要将其添加到from阵列。

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