在适配器上获取json数据设置时,自定义Listview数据将重写

时间:2015-11-03 14:49:49

标签: android

我正在使用volley库进行web api调用并使用for循环检索json数据并在自定义listview上设置该数据但listview只显示该数据的两个值,如果我的循环运行4次只有2或4个编号值是列表视图上显示。 下面是我从json数据中获取值的代码。

details = new ArrayList();

    depart_adapter = new Result_dep_adapter(Result.this, android.R.layout.simple_list_item_2, details);

    return_adapter = new Result_ret_adapter(Result.this, android.R.layout.simple_list_item_2, details);
    departlist.setAdapter(depart_adapter);
    returnlist.setAdapter(return_adapter);

尝试{

                     for (int k = 0; k < response.length(); k++) {
                     custom_result=new Custom_Result();
                         Log.i("response",","+ response.length());
                         JSONObject obj = response.getJSONObject(k);
                         JSONObject flight = obj.getJSONObject("Flights");
                         JSONArray array = flight.getJSONArray("Segments");
                         for (int i = 0; i < array.length(); i++) {

                             JSONObject depart = (JSONObject) array.get(i);




                                 String departdate_time = depart.getString("DepartureTime");
                                 String arrivedate_time = depart.getString("ArrivalTime");
                             Boolean returnflight=depart.getBoolean("IsReturnFlight");
                                duration=depart.getString("FlightDuration");
                             Log.i("segments",","+returnflight);
                                 JSONObject Airline=depart.getJSONObject("Airline");
                                 String Airline_code=Airline.getString("Name");
                                 JSONObject Airlinename=depart.getJSONObject("MarketingCarrier");
                                 String name=Airlinename.getString("Name");
                                    Log.i("Airline code", Airline_code);
                                 String[] departtime = departdate_time.split("T");
                                  String[] arrivetime = arrivedate_time.split("T");
                                 if (!returnflight) {
                                     String ddate = departtime[0];
                                     String dtime = departtime[1].substring(0, 5);
                                     Log.i("time", dtime);
                                     String duration = depart.getString("FlightDuration");
                                     String Adate = arrivetime[0];
                                     String Atime = arrivetime[1].substring(0, 5);


                                     custom_result.setDepart_date(ddate);
                                     custom_result.setDepart_time(dtime);
                                     custom_result.setArrive_date(Adate);
                                     custom_result.setArrive_time(Atime);
                                     custom_result.setDep_duration(duration);
                                     custom_result.setDep_Airline_name(name);
                                     custom_result.setDep_Airline_code(Airline_code);



                                 }else{
                                     String Rdate = departtime[0];
                                     String Rtime = departtime[1].substring(0, 5);//return depart time
                                     Log.i("time", Rtime);
                                     String return_duration = depart.getString("FlightDuration");
                                     String ARdate = arrivetime[0];
                                     String ARtime = arrivetime[1].substring(0, 5);

                                     custom_result.setRet_arr_date(ARdate);
                                     custom_result.setRet_arr_time(ARtime);
                                     custom_result.setRet_dep_date(Rdate);
                                     custom_result.setRet_dep_time(Rtime);
                                     custom_result.setRet_duration(return_duration);
                                     custom_result.setRet_Airline_name(name);
                                     custom_result.setRet_Airline_code(Airline_code);

                                 }



                             }
                             String stops = flight.getString("Stops");
                             JSONObject Fare = flight.getJSONObject("Fare");
                             String published_fare = Fare.getString("PublishedFare");
                             custom_result.setStops(stops);
                             custom_result.setPrice(published_fare);

                             details.add(custom_result);





                         }

我是新来的android请帮帮我.. 谢谢

这是我的adpater代码

私有类ViewHolder     {         ImageView Airline_logo;         TextView Airline_code,dep_time,dep_arr_time,dep_stops,dep_duration,                  dep_price;

}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder=null;
 //  custom_result=Custom_Result.getCustom_result();
    custom_result=getItem(position);
    LayoutInflater inflater=(LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if(convertView==null)
    {
        convertView=inflater.inflate(R.layout.result_listview,null);

        holder=new ViewHolder();
       holder. Airline_logo=(ImageView) convertView.findViewById(R.id.dep_airlines_image);
        holder.Airline_code=(TextView) convertView.findViewById(R.id.dep_airline_code);
        holder.dep_time=(TextView) convertView.findViewById(R.id.dep_depart_time);
        holder.dep_arr_time=(TextView) convertView.findViewById(R.id.dep_arrive_time);
        holder.dep_stops=(TextView) convertView.findViewById(R.id.dep_stops_info);
        holder.dep_duration=(TextView) convertView.findViewById(R.id.dep_duration);
        holder.dep_price=(TextView) convertView.findViewById(R.id.dep_airline_price);
        convertView.setTag(holder);
    }else
    {
        holder=(ViewHolder)convertView.getTag();
    }
    String imagename=custom_result.getDep_Airline_code().toLowerCase();

  String path="drawable/"+imagename;

  // String logo=path.toLowerCase();
  // String PACKAGE_name=getContext().getPackageName();
int imageresource=context.getResources().getIdentifier(path, null, context.getPackageName());
if(imageresource==0)
    {
imageresource=R.drawable.flight_icon;
        }
    Drawable image=context.getResources().getDrawable(imageresource);

  // holder. Airline_logo.setImageBitmap(BitmapFactory.decodeResource(getContext().getResources(),image));
    holder.Airline_logo.setImageDrawable(image);
    holder.Airline_code.setText(custom_result.getDep_Airline_name());
    holder.dep_time.setText(custom_result.getDepart_time());
    holder.dep_arr_time.setText(custom_result.getArrive_time());
    holder.dep_stops.setText(custom_result.getStops());
    holder.dep_duration.setText(custom_result.getDep_duration());
    holder.dep_price.setText(custom_result.getPrice());

    return convertView;
}

1 个答案:

答案 0 :(得分:0)

您正在使用android.R.layout.simple_list_item_2作为行的布局。

That layout只有2个文字视图,因此无法显示超过2个值。

如果您需要显示从BaseAdapter类创建自定义适配器所需的更多内容。

然后在getView()方法中,您可以使用所需的任何内容绘制行。

希望这有帮助。