Android Studio新手:如何将自定义文本添加到ListView / ListAdapter结果中?

时间:2016-03-18 05:33:48

标签: android listview

我这里有一个愚蠢的问题。我也是新手,请尽可能显示一些代码。

Android Studio:如何将自定义文本附加到ListView / ListAdapter中?

  • 我想在原始库存ID之前添加额外的文字。
      

    示例:产品ID:1,   产品编号:2

  • 原始库存ID是使用php sql从数据库服务器获取的。

Screenshot of the app

我尝试使用以下行来执行此操作,但存在一些问题,因为我要在 onItemClick函数中引用 Config.TAG_ID 将其传递给下一个活动。

  

// employees.put(Config.TAG_ID," ID:" + id);

docker run -d nodebb

显示我的库存清单的功能

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Intent intent = new Intent(this, ViewStock.class);
    HashMap<String, String> map = (HashMap) parent.getItemAtPosition(position);
    String empId = map.get(Config.TAG_ID).toString();
    intent.putExtra(Config.STOCK_ID, empId);
    startActivity(intent);
}

已编辑:GreyWolf答案的错误图片: enter image description here

3 个答案:

答案 0 :(得分:1)

employees.put(Config.TAG_ID,“产品ID”+ id);

答案 1 :(得分:1)

public class ProductPogo {
String id;
String productName;
String price;

public String getId() {
    return id;
}

public String setId(String id) {
    this.id = id;
    return id;
}

public String getProductName() {
    return productName;
}

public String setProductName(String productName) {
    this.productName = productName;
    return productName;
}

public String getPrice() {
    return price;
}

public String setPrice(String price) {
    this.price = price;
    return price;
}

}

//in your doInBackground
JSONObject productJsonObject = myPriceJsonArray.getJSONObject(p);

id =productPogo.setId("Product ID"+productJsonObject.getString("id"));
productName = productPogo.setProductName(productJsonObject.getString("productName"));
 = productPogo.setPrice(productJsonObject.getString("price"));

yourArraylist.add(productPogo);

//InOnItemClickListner
Bundle bundle=new Bundle();
bundle.putString("product_id", yourArraylist.get(position).getId());

答案 2 :(得分:1)

您可以使用SimpleAdapter的setViewBinder()方法提供一个活页夹,将数据转换为要在视图中设置的文本字段。

按如下方式设置数据:

        HashMap<String, String> employees = new HashMap<>();
        employees.put(Config.TAG_ID, id);
        employees.put(Config.TAG_NAME, name);
        employees.put(Config.TAG_PRICE, price);
        list.add(employees);

您的适配器如下:

    SimpleAdapter adapter = new SimpleAdapter(
            ViewAllStock.this, list, R.layout.list_item,
            new String[]{ Config.TAG_ID, Config.TAG_NAME, Config.TAG_PRICE},
            new int[]{ R.id.id, R.id.name, R.id.price});
    adapter.setViewBinder(new SimpleAdapter.ViewBinder() {
        @Override
        public boolean setViewValue(View view, Object data, String textRepresentation) {
            if (view.getId() == R.id.id) {
                 TextView v = (TextView) view;
                 v.setText("Product ID: " + textRepresentation);
                 return true;
            } else if (view.getId() == R.id.name) {
                 TextView v = (TextView) view;
                 v.setText("Product Name: " + textRepresentation);
                 return true;
            } else if (view.getId() == R.id.price) {
                 TextView v = (TextView) view;
                 v.setText("Price: ($)" + textRepresentation);
                return true;
            }
            return false;
        }
    });

    listView.setAdapter(adapter);

现在数据值保持纯净,可以在您的活动意图中不加改变地传递。

相关问题