user6644103
user6644103

Reputation:

How to pass String into ListView - Android

I have made an ListView with items, qty, rate and retrieved the data from MySQL but I need to add even my "Service Charges" and "Net Amount" to the ListView but I have "Service Charges" and "Net Amount" in the variable not in the MYSQL how can I insert this two into the ListView.

Here is my code:

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

So how can insert the "Service Charges" and "Net Amount" which I have as variable into the ListView?

Upvotes: 0

Views: 222

Answers (2)

Neerajlal K
Neerajlal K

Reputation: 6828

Its more of a hack. You can add the items to the String arrays before giving it to the Adapter.

Something like this,

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

Upvotes: 1

Kaveesh Kanwal
Kaveesh Kanwal

Reputation: 1763

Make a model class containing Service Charges and Net Amount

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

Now you can create an ArrayList of MyModel class and add your services charges and net amount values:

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

Now you can pass this myModelArray to your list adapter for binding data with the list view.

Retrieve values by:

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

Note: Shift to retrofit, it is 4 times faster than volley.

Upvotes: 1

Related Questions