【问题标题】:ArrayList object is returing one row on ListView in java androidArrayList 对象正在返回 java android 中 ListView 上的一行
【发布时间】:2018-05-02 03:04:39
【问题描述】:

我正在尝试在 ArrayList 对象上添加来自 MySQL 数据库的对象,但结果只给了我一行 我在我的ListView 上使用自定义适配器,我想我可以使用循环多个对象到ArrayList 对象但我失败了,请帮助我,

我的代码:

String driver_fullname = json.getString("driver_fullname");
String driver_phonenumber = json.getString("driver_phonenumber");
String plate_no = json.getString("plate_no");
String parking_name = json.getString("parking_name");

List<PaymentTiming_Items> getAllDiverDetails = new ArrayList<PaymentTiming_Items>();
PaymentTiming_Items timingItems = new PaymentTiming_Items();
timingItems.setPlateNo(plate_no);
timingItems.setParkingName(parking_name);
timingItems.setDriverFullName(driver_fullname);
getAllDiverDetails.add(timingItems); // store all drivers' info to
}

if (getAllDiverDetails.size() !=0) {
userList =  new ArrayList<>   (getAllDiverDetails);
listAdapter = new PaymentTiming_ListAdapter(getApplicationContext(), userList);
myList.setAdapter(listAdapter);
}

【问题讨论】:

  • 你能添加自定义布局xml吗..检查它的高度是否“匹配当前”。如果它 match_parent 将其更改为 wrap_content
  • @Tomin B 自定义布局在自定义适配器中,我认为问题不是布局
  • 添加如何加载多个数据。?代码中没有循环。
  • @Mohamed,我没有收到错误,问题是我的 json 生成了很多行,但我通过 ArrayList 在 ListView 上只得到了一行
  • 我正在更新我的评论 - 添加用于加载多个数据的代码。

标签: java android mysql listview arraylist


【解决方案1】:

看起来您每次解析对象时都在创建一个 ArrayList。如果我理解正确,您的代码应该是这样的:

// ArrayList will be created only once for a json response.
    List<PaymentTiming_Items> getAllDiverDetails = new ArrayList<PaymentTiming_Items>();

  //Now parse add all elements in json response and add to list.
        for(all items in your jsonResponse List ) {    
            //Parse fields from json object
            String driver_fullname = json.getString("driver_fullname");
            String driver_phonenumber = json.getString("driver_phonenumber");
            String plate_no = json.getString("plate_no");
            String parking_name = json.getString("parking_name");

            //create object
            PaymentTiming_Items timingItems = new PaymentTiming_Items();
            timingItems.setPlateNo(plate_no);
            timingItems.setParkingName(parking_name);
            timingItems.setDriverFullName(driver_fullname);
            getAllDiverDetails.add(timingItems); // store all drivers' info to
        }

        //Now list will have all the items, Add this list to adapter.   
            if (getAllDiverDetails.size() !=0) {
            userList =  new ArrayList<>(getAllDiverDetails);
            listAdapter = new PaymentTiming_ListAdapter(getApplicationContext(), userList);
            myList.setAdapter(listAdapter);
            }

【讨论】:

  • 谢谢,你说得对,我每次都在解析一个对象
【解决方案2】:

假设您的服务器在JSONArray 中给出结果,说 response 作为字符串尝试以下操作

List<PaymentTiming_Items> getAllDiverDetails = new ArrayList<PaymentTiming_Items>();


        JSONArray jsonArray = new JSONArray(response);
        int size = jsonArray.length();
        if (size > 0)
        {
            for (int i = 0; i < size; i++)
            {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String driver_fullname = json.getString("driver_fullname");
                String driver_phonenumber = json.getString("driver_phonenumber");
                String plate_no = json.getString("plate_no");
                String parking_name = json.getString("parking_name");


                PaymentTiming_Items timingItems = new PaymentTiming_Items();
                timingItems.setPlateNo(plate_no);
                timingItems.setParkingName(parking_name);
                timingItems.setDriverFullName(driver_fullname);
                getAllDiverDetails.add(timingItems); // store all drivers' info to
            }
        }

if (getAllDiverDetails.size() !=0) {
userList =  new ArrayList<>   (getAllDiverDetails);
listAdapter = new PaymentTiming_ListAdapter(getApplicationContext(), userList);
myList.setAdapter(listAdapter);
}

【讨论】:

    【解决方案3】:

    您必须使用JSONArrayJSON 获取项目列表。然后用它们填充您的 ArrayList 并传递给您的适配器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-15
      • 2019-02-20
      • 1970-01-01
      • 1970-01-01
      • 2014-10-02
      • 2015-07-12
      • 2016-04-16
      • 1970-01-01
      相关资源
      最近更新 更多