【问题标题】:How do I display multiple rows of data from mysql in listview?如何在listview中显示来自mysql的多行数据?
【发布时间】:2015-12-16 06:55:38
【问题描述】:

如何在ListView中显示多行数据?

现在我一次只能检索和显示一个数据(项目),我想检索多行数据(项目)并将它们全部显示在ListView 中。

在这种情况下,我使用用户名来检索他/她在数据库中的优惠券。所以我只能显示一张优惠券,我想知道如何在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);

【问题讨论】:

    标签: android listview android-listview


    【解决方案1】:

    要填充ListView,我建议您创建一个对象(优惠券)的List,并创建一个知道如何在列表中显示任意数量的优惠券的Adapter。首先,您可能想为您的优惠券定义一个简单的类(根据您的数据结构重新实现它):

    public class Coupon {
        private String mCouponText;
    
        public Coupon(String text) {
            mCouponText = text;
        }
    
        public String getCouponText() {
            return mCouponText;
        }
    }
    

    之后,您应该从您的JSONArray 创建这些对象的列表。有不同的方法,其中之一:

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

    最后要做的是创建一个Adapter 并将其设置为ListViewActivity 中的适配器:

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

    之后ListView 将使用Adapter 填充其行。您可能想要实现自己的适配器,因为它为您提供了更多选择,您会很容易地了解如何做到这一点。

    更新

    考虑改用RecyclerView

    【讨论】:

      【解决方案2】:

      让一个类调用它任何你喜欢的名称(让我们称之为 Item),并且此调用的每个实例都将代表 Database 中的相关行(或您获取信息的任何来源)

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

      创建Item 类型的Arraylist 并将此信息提供给自定义ListView 以显示您想要的详细信息。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-07
        • 1970-01-01
        • 2011-04-28
        • 1970-01-01
        • 2021-01-20
        相关资源
        最近更新 更多