如何将String传递给ListView - Android

时间:2016-09-15 10:05:26

标签: android listview

我使用ListViewitemsqty制作了rate并从MySQL检索了数据,但我需要添加“服务费”和“网络”数量“到ListView,但我有”服务费“和”净金额“在MYSQL中的变量中如何将这两个插入ListView

这是我的代码:

public void getPostedJobsLocal(){

    String url=Config.GET_PAYMENT_BILL;
    String url1= local_job_id;
    String URL=url+url1;
    StringRequest stringRequest = new StringRequest(URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            showJSONPosted(response);
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

private void showJSONPosted(String response) {
    ParseJSONPayBillLocal pj = new ParseJSONPayBillLocal(response);
    pj.parseJSONPayBillLocal();
    CustomListPayBillLocal cl = new CustomListPayBillLocal(this, ParseJSONPayBillLocal.items, ParseJSONPayBillLocal.qty,ParseJSONPayBillLocal.rate);
    lview.setAdapter(cl);
}

那么如何将我作为变量的“服务费”和“净额”插入ListView

2 个答案:

答案 0 :(得分:1)

制作包含服务费净金额的模型类

public class MyModel {

    String serviceCharges;
    String netAmount;


    public MyModel (String serviceCharges, String netAmount){
        this.serviceCharges= serviceCharges;
        this.netAmount= netAmount;

    }

    public String getServiceCharges() {
        return serviceCharges;
    }

    public void setServiceCharges(String serviceCharges) {
        this.serviceCharges= serviceCharges;
    }

    public String getNetAmount() {
        return netAmount;
    }

    public void setNetAmount(String netAmount) {
        this.netAmount= netAmount;
    }
}

现在您可以创建MyModel类的ArrayList并添加服务费用和净额值:

ArrayList<MyModel> myModelArray = new ArrayList<MyModel>();
myModelArray.add(new MyModel("serviceCharge1","netAmount1"));
myModelArray.add(new MyModel("serviceCharge2","netAmount2"));
.......
.......

现在,您可以将此 myModelArray 传递给列表适配器,以便使用列表视图绑定数据。

按以下方式检索值:

myModelArray.get(position).getServiceCharges();
myModelArray.get(position). getNetAmount(); 

注意:转换到改装,它比凌空快4倍。

答案 1 :(得分:1)

它更像是一个黑客。您可以在将项目提供给适配器之前将其添加到String数组中。

像这样,

private void showJSONPosted(String response) {
    ParseJSONPayBillLocal pj = new ParseJSONPayBillLocal(response);
    pj.parseJSONPayBillLocal();

    ParseJSONPayBillLocal.items = append(ParseJSONPayBillLocal.items, "Service Charges");
    ParseJSONPayBillLocal.qty = append(ParseJSONPayBillLocal.qty, "your_qty");
    ParseJSONPayBillLocal.rate = append(ParseJSONPayBillLocal.rate, "your_rate");

    ParseJSONPayBillLocal.items = append(ParseJSONPayBillLocal.items, "Net Amount");
    ParseJSONPayBillLocal.qty = append(ParseJSONPayBillLocal.qty, "your_qty");
    ParseJSONPayBillLocal.rate = append(ParseJSONPayBillLocal.rate, "your_rate");

    CustomListPayBillLocal cl = new CustomListPayBillLocal(this, ParseJSONPayBillLocal.items, ParseJSONPayBillLocal.qty, ParseJSONPayBillLocal.rate);
    lview.setAdapter(cl);
}

public static <T> T[] append(T[] arr, T element) {
    final int N = arr.length;
    arr = Arrays.copyOf(arr, N + 1);
    arr[N] = element;
    return arr;
}