Android凌空列表视图未显示

时间:2016-04-30 13:14:43

标签: android json listview android-volley

问题:Android凌空列表视图未显示。

描述:尝试以listview格式获取Json数据,在它显示的logcat中,即使我能够正确解析它。 但是当我尝试在Fragment中运行它时它不会显示任何内容 这个错误。

W / AbsListView:意图操作不存在的数据,childcount = 0,mFirstPosition = 0,mItemCount = 0,适配器计数= 0,mAdapter = com.brillmindz.roomshare.CustomListAdapter @ 3fbf203f,action = 0,mActivePointerId = 0,mScrollY = 0,这= android.widget.ListView {1752f18f V.ED.VC. ........ 0,0-540,691#7f0d004c app:id / homelistView}

这是代码OF

HomepageFragment

public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){

    View rootView = inflater.inflate(R.layout.content_main, container, false);

   // usersList = new ArrayList<String>();


    listView = (ListView) rootView.findViewById(R.id.homelistView);

    adapter = new CustomListAdapter(this,hotelLists);

    listView.setAdapter(adapter);

    pDialog = new ProgressDialog(getContext());

    // Showing progress dialog before making http request

    pDialog.setMessage("Loading...");

    pDialog.show();


    final StringRequest jsonObjReq = new StringRequest(Request.Method.POST, url_shareroom, new Response.Listener<String>() {

        @Override
 public void onResponse(String response) {

            Log.d(TAG, response);
            pDialog.dismiss();
            HotelList hotelList = new HotelList();

          //  adapter.notifyDataSetChanged();
            try {
                JSONObject jsonObject = new JSONObject(response);
                String hotelsDetailsArray = jsonObject.getString("hotelsDetails");
                Log.d("Hotel name : ", hotelsDetailsArray);
                JSONArray jsonArray = new JSONArray(hotelsDetailsArray);
                for(int i = 0; i<jsonArray.length();i++) {
                    JSONObject mJsonObject = jsonArray.getJSONObject(i);
                    String hotelName  = mJsonObject.getString("hotel_name");
                    Log.d("Hotel name : ", hotelName);
                    String city  = mJsonObject.getString("city");
                    Log.d("City : ", city);
                    String state  = mJsonObject.getString("state");
                    Log.d("State : ", state);
                    String country  = mJsonObject.getString("country");
                    Log.d("Country : ", country);
                    String address  = mJsonObject.getString("address");
                    Log.d("Address : ", address);
                    String list_title  = mJsonObject.getString("list_title");
                    Log.d("list_title : ", list_title);
                    String check_in_date  = mJsonObject.getString("check_in_date");
                    Log.d("check_in_date : ", check_in_date);
                    String check_out_date  = mJsonObject.getString("check_out_date");
                    Log.d("check_out_date : ", check_out_date);
                    String rate  = mJsonObject.getString("rate");
                    Log.d("rate : ", rate);
                    String created  = mJsonObject.getString("created");
                    Log.d("created : ", created);
                    String created_time  = mJsonObject.getString("created_time");
                    Log.d("created_time : ", created_time);
                    String matching_percentage  = mJsonObject.getString("matching_percentage");
                    Log.d("matching_percentage : ", matching_percentage);
                    String star_rating  = mJsonObject.getString("star_rating");
                    Log.d("star_rating : ", star_rating);
                    String reviews  = mJsonObject.getString("reviews");
                    Log.d("reviews : ", reviews);

               hotelLists.add(hotelList);

                }
            } catch (JSONException e1) {
                e1.printStackTrace();
            }

            } catch (JSONException e1) {
                e1.printStackTrace();
            }

    **      adapter = new CustomListAdapter(this,hotelLists);
            listView.setAdapter(adapter);
            adapter.notifyDatasetChanged();** 

        }

 }

    },
            new Response.ErrorListener() {

                @Override

                public void onErrorResponse(VolleyError error) {

               /*     Log.e("Error Response", error.toString());*/

                    Log.e(TAG, "Volley Error: " + error.getMessage());

                    // 

Toast.makeText(MainActivity.this,error.toString(),Toast.LENGTH_LONG).show();

                }

            }) {

        protected Map<String, String> getParams() throws com.android.volley.AuthFailureError {

            Map<String, String> params = new HashMap<String, String>();

            params.put("user_id", "80");

            return params;

        }

    };

    AppController.getInstance().addToRequestQueue(jsonObjReq);

    return rootView;


}



@Override

public void onDestroy() {

    super.onDestroy();

    hidePDialog();

}

private void hidePDialog() {

    if (pDialog != null) {

        pDialog.dismiss();

        pDialog = null;

    }

}


}

自定义列表适配器

public class CustomListAdapter extends BaseAdapter {

private HomePageFragment homePageFragment;

Context mycontext;

private LayoutInflater inflater;

private List<HotelList> hotelItems;

ImageLoader imageLoader = AppController.getInstance().getImageLoader();

public CustomListAdapter(HomePageFragment hpfragment, List<HotelList> hotelItems) {

    this.homePageFragment = hpfragment;

    this.hotelItems = hotelItems;

}


@Override

public int getCount() {

    return hotelItems.size();

}


@Override

public Object getItem(int location) {

    return hotelItems.get(location);

}


@Override

public long getItemId(int position) {

    return position;

}


@Override

public View getView(int position, View convertView, ViewGroup parent) {

    if (inflater == null)

        inflater = (LayoutInflater) mycontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.list_row, null);

    }

   /* if (imageLoader == null)

        imageLoader = AppController.getInstance().getImageLoader();

    NetworkImageView thumbNail = (NetworkImageView) convertView
            .findViewById(R.id.thumbnail);*/

    TextView hotel_name = (TextView) convertView.findViewById(R.id.hotel_name);

    TextView city = (TextView) convertView.findViewById(R.id.city);

    TextView state = (TextView) convertView.findViewById(R.id.state);

    TextView amenities =(TextView) convertView.findViewById(R.id.amenities);

    TextView country = (TextView) convertView.findViewById(R.id.country);

    // getting data for the row
    HotelList m = hotelItems.get(position);



    hotel_name.setText(m.getHotel_name());

Log.e("Hotelname", m.getHotel_name());

    city.setText(m.getCity());

    Log.e("city", m.getCity());

    amenities.setText(m.getAmenities());

    Log.e("amenities", m.getAmenities());

    state.setText(m.getState());

    Log.e("state", m.getState());

    country.setText(m.getCountry());

    Log.e("country", m.getCountry());

    return convertView;
}

}

酒店列表

public class HotelList {

private String
        hotel_name,
        rate,
        city,
        state,
        country,
        address,
        list_title,
        check_in_date,
        check_out_date,
        amenities,
        created,
        created_time,
        match_percentage,
        star_rating,
        reviews;


private String user_Id;


public HotelList() {
}

public HotelList(String hotel_name, String city, String state, String country,
                 String address, String list_title, String check_in_date, String amenities,
                 String check_out_date, String created, String created_time, String match_percentage, String star_rating, String reviews
) {
    this.hotel_name= hotel_name;
    this.city = city;
    this.amenities= amenities;
    this.state = state;
    this.country= country;
    this.address= address;
    this.list_title = list_title;
    this.check_in_date= check_in_date;
    this.check_out_date= check_out_date;
    this.created =created;
    this.rate= rate;
    this.created_time= created_time;
    this.match_percentage= match_percentage;
    this.star_rating = star_rating;
    this.reviews= reviews;
}




//STRING

private String created() {
    return created;
}

private String created_time() {
    return created_time;
}

private String star_rating() {
    return star_rating;
}

private String match_percentage() {
    return match_percentage;
}

private String reviews() {
    return reviews;
}
public String getCity() {
    return city;
}

public String getState() {
    return state;
}

public String getCountry() {
    return country;
}

public String getAddress() {
    return address;
}

public String getHotel_name() {
    return hotel_name;
}

public String getList_title() {
    return list_title;
}

private String check_out_date() {
    return check_out_date;
}

private String check_in_date() {
    return check_in_date;
}

private String list_title() {
    return list_title;
}

private String address() {
    return address;
}

private String hotel_name() {
    return hotel_name;
}

public String getCheck_in_date() {
    return check_in_date;
}

public String getCheck_out_date() {
    return check_out_date;
}

private String country() {
    return country;
}

private String state() {
    return state;
}

private String city() {
    return city;
}

public String getAmenities() {
    return amenities;
}

public String getRate() {
    return rate;
}

//SETTER
public void setState(String state) {
    this.state = state;
}

public void setHotel_name(String hotel_name) {
    this.hotel_name = hotel_name;
}

public void setCity(String city) {
    this.city = city;
}

public void setAddress(String address) {
    this.address = address;
}

public void setList_title(String list_title) {
    this.list_title = list_title;
}

public void setCheck_in_date(String check_in_date) {
    this.check_in_date = check_in_date;
}

public void setCountry(String country) {
    this.country = country;
}

public void setCheck_out_date(String check_out_date) {
    this.check_out_date = check_out_date;
}

public void setAmenities(String amenities) {
    this.amenities = amenities;
}

public void setUser_Id(String user_Id) {
    this.user_Id = user_Id;
}

public void setGenre(ArrayList<String> genre) {
}


public String getCreated_time() {
    return created_time;
}

public String getMatch_percentage() {
    return match_percentage;
}

public String getStar_rating() {
    return star_rating;
}

public String getReviews() {
    return reviews;
}
}

MY JSON回复

{     “成功”:“1”,     “hotelsDetails”:[{         “id”:“1”,         “hotel_name”:“纽约中央公园丽思卡尔顿酒店”,         “城市”:“纽约”,         “设施”:“”,         “州”:“阿拉斯加”,         “国家”:“美国”,         “地址”:“50 Central Park S New York 10019”,         “list_title”:“纽约丽思卡尔顿酒店”,         “check_in_date”:“2016-03-17”,         “check_out_date”:“2016-03-21”,         “率”:“100”,         “创造”:“2016-02-11”,         “created_time”:“00:00:00”,         “matching_percentage”:“73”,         “star_rating”:“3”,         “评论”:“15”     },{         “id”:“2”,

我错了。?

3 个答案:

答案 0 :(得分:0)

你没有解决凌空反应的任何事情 您应该解析响应并将其添加到本部分的适配器

@Override

        public void onResponse(String response) {

            Log.d(TAG, response);
            //parse data and add to adapter
            pDialog.dismiss();
}

另外,你的列表在凌空响应之前的初始位置是什么?

并且您的响应是jsonobject,使用JsonRequest而不是StringRequest

答案 1 :(得分:0)

您正在适配器中设置一个空列表,当您从后端获取数据并使用数据更新列表时,您不会使用列表中的新数据更新适配器。在使用数据更新列表后,只需致电adapter.notifyDatasetChanged()即可。这应该有效。

答案 2 :(得分:0)

完成解析JSON后,即:try/catch块后,执行以下操作。

adapter = new CustomListAdapter(this,newHotelLists);
listView.setAdapter(adapter);
adapter.notifyDatasetChanged();

newHotelsList是您下载的新酒店列表。