1

How do I display multiple rows of data in ListView?

Now I am only able to retrieve and display one data(Item) at a time, I want to retrieve multiple rows of data(Items) and display all them in ListView.

In this case, I am using the name of the user to retrieve the coupons he/she have in the database. So for i can only display one coupon and I want to know how to display multiple coupon in ListView.

    protected String doInBackground(String... args) {
        // Check for success tag
        int success;
        try {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("name", name));


            JSONObject json = jsonParser.makeHttpRequest(
                    url_all_coupons, "GET", params);


            Log.d("Single Voucher Details", json.toString());

            // json success tag
            success = json.getInt(TAG_SUCCESS);
            if (success == 1) {

                JSONArray productObj = json
                        .getJSONArray(TAG_COUPONS); // JSON Array


                JSONObject coupons = productObj.getJSONObject(0);

2 Answers 2

1

To populate the ListView I suggest you create a List of objects (vouchers) and create an Adapter that knows how to display any number of coupons in the list. First of all you may want to define a simple class for your coupons (reimplement it according to your data structure):

public class Coupon {
    private String mCouponText;

    public Coupon(String text) {
        mCouponText = text;
    }

    public String getCouponText() {
        return mCouponText;
    }
}

After that you should create a List of these objects from your JSONArray. There are different approaches, one of them:

List<Coupon> couponsList = new ArrayList<>();
JSONArray coupons = json
            .getJSONArray(TAG_COUPONS); // JSON Array
for (JSONObject coupon : (Iterable<JSONObject>) coupons) {
    String text = coupon.get("text");
    couponsList.add(new Coupon(text));
}

And the last thing to do is to create an Adapter and set it as an adapter of your ListView in your Activity:

ArrayAdapter<Coupon> adapter = new ArrayAdapter<>(this, 
        android.R.layout.simple_list_item_1, couponsList);
yourListView.setAdapter(adapter);

After that the ListView will use the Adapter to populate its rows. You may want to implement your own adapter since it gets you far more options, you'll easily find out how to do it.

Update

Consider using RecyclerView instead.

Sign up to request clarification or add additional context in comments.

Comments

0

Make a class call it anything you like (lets call it Item), and each instance of this call will represent relevant row in the Database ( or any source from where you are getting the information)

Item.java may have few instance variable like coupons and userName,

public class Item {

    private String coupons;

    private String userName;

    public String getCoupons() {
        return coupons;
    }

    public void setCoupons(String coupons) {
        this.coupons = coupons;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
}

Make a Arraylist of type Item and feed this information to a custom ListView to show the details you want it to.

ArrayList<Item> list = new ArrayList<>();
        for (int i = 0; i < productObj.length(); i++) {
            Item item = new Item();
            item.setCoupons(""+productObj.getJSONObject(i).getString("CouponKey"));
            list.add(item);
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.