数据无法在可扩展列表中更新

时间:2016-02-26 20:19:44

标签: java android expandablelistadapter

我使用JSONRequests调用三个Volley,并在请求的每个响应中调用此方法。仅当所有三个响应都准备就绪时才执行此方法。但是当我尝试更新可扩展列表时,它无法在屏幕上显示。谁能看出我做错了什么?

private void updateWhenReady(){
    System.out.println(validCurrent+ " " +  validDaily + " " + validHourly );

    if(validCurrent && validDaily && validHourly)
    {


        System.out.println("in");


        for (WeatherCondition wc: dailyResponseList)
        {
            ArrayList<WeatherCondition> tempList = new ArrayList<>();
            for(WeatherCondition w: hourlyResponseList)
            {
                if(w.getDate().equalsIgnoreCase(wc.getDate()))
                {
                    tempList.add(w);
                }
            }


                weatherList.put(wc,tempList);

        }
        for (WeatherCondition weatherCondition: weatherList.keySet())
        {
            System.out.println(" +   " + weatherCondition.getDate());
        }


        weatherList = dbHelper.getWeatherConditionsHashMap();
        hourlyList = new ArrayList<WeatherCondition>(weatherList.keySet());
        adapter = new WeatherSearchListAdapter(getActivity().getApplicationContext(), weatherList, hourlyList);
        expList.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        System.out.println(validCurrent + " " + validDaily + " " + validHourly);

        validCurrent = false;
        validDaily = false;
        validHourly = false;
    }

}

以下是explist适配器

public class WeatherSearchListAdapter extends BaseExpandableListAdapter {
    private Context ctx;
    private HashMap<WeatherCondition, List<WeatherCondition>> weatherList;
    private List<WeatherCondition> list;


    public WeatherSearchListAdapter(Context ctx, HashMap<WeatherCondition, List<WeatherCondition>> parentList, List<WeatherCondition> list){
        this.weatherList = parentList;
        this.list = list;
        this.ctx = ctx;

    };



    @Override
    public int getGroupCount() {
        return list.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {

        return weatherList.get(list.get(groupPosition)).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return list.get(groupPosition);
    }

    @Override
    public Object getChild(int parent, int child) {
        return weatherList.get(list.get(parent)).get(child);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int parent, int child) {
        return child;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        WeatherCondition groupWeatherCondition = (WeatherCondition) getGroup(groupPosition);
        if(convertView == null)
        {
            LayoutInflater inflator = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflator.inflate(R.layout.fragment_daily_list_parent, parent, false);
        }



        ImageView weatherIconIdIV = (ImageView) convertView.findViewById(R.id.weatherIconIV);
        ImageView windIconIdIV = (ImageView) convertView.findViewById(R.id.windDirectionIV);
        TextView humidityTV = (TextView) convertView.findViewById(R.id.humitityListText);
        TextView rainTV = (TextView) convertView.findViewById(R.id.rainListText);
        TextView windDirectionTV = (TextView) convertView.findViewById(R.id.windDirectionText);
        TextView windSpeedTV = (TextView) convertView.findViewById(R.id.windSpeedText);
        TextView maxTempTV = (TextView) convertView.findViewById(R.id.maxTempListText);
        TextView minTempTV = (TextView) convertView.findViewById(R.id.minTempListText);
        TextView dateTV = (TextView) convertView.findViewById(R.id.dateListText);

        String name = groupWeatherCondition.getWeatherIconId();
        int weatherIconId = ctx.getResources().getIdentifier(name, "drawable", ctx.getPackageName());
        weatherIconIdIV.setImageResource(weatherIconId);


        String name1 = groupWeatherCondition.getWind().getWindIconId();
        int windIconId = ctx.getResources().getIdentifier(name1, "drawable", ctx.getPackageName());
        windIconIdIV.setImageResource(windIconId);

         humidityTV.setText(groupWeatherCondition.getHumidity());
         rainTV.setText(groupWeatherCondition.getRain());
         windDirectionTV.setText(groupWeatherCondition.getWind().getWindDirection());
         windSpeedTV.setText(groupWeatherCondition.getWind().getSpeed());
         maxTempTV.setText(groupWeatherCondition.getMaxTemp());
         minTempTV.setText(groupWeatherCondition.getMinTemp());
         dateTV.setText(groupWeatherCondition.getDate());

         convertView.setBackgroundColor(Color.parseColor("#5E5E5E"));

         return convertView;

    }

    @Override
    public View getChildView(int parent, int child, boolean isLastChild, View convertView, ViewGroup parentView) {
        WeatherCondition childWeatherCondition = (WeatherCondition) getChild(parent, child);
        if(convertView == null)
        {
            LayoutInflater inflator = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflator.inflate(R.layout.fragment_daily_list_parent, parentView, false);
        }

        ImageView weatherIconIdIV = (ImageView) convertView.findViewById(R.id.weatherIconIV);
        ImageView windIconIdIV = (ImageView) convertView.findViewById(R.id.windDirectionIV);
        TextView humidityTV = (TextView) convertView.findViewById(R.id.humitityListText);
        TextView rainTV = (TextView) convertView.findViewById(R.id.rainListText);
        TextView windDirectionTV = (TextView) convertView.findViewById(R.id.windDirectionText);
        TextView windSpeedTV = (TextView) convertView.findViewById(R.id.windSpeedText);
        TextView maxTempTV = (TextView) convertView.findViewById(R.id.maxTempListText);
        TextView minTempTV = (TextView) convertView.findViewById(R.id.minTempListText);
        TextView dateTV = (TextView) convertView.findViewById(R.id.dateListText);

        String name = childWeatherCondition.getWeatherIconId();
        int weatherIconId = ctx.getResources().getIdentifier(name, "drawable", ctx.getPackageName());
        weatherIconIdIV.setImageResource(weatherIconId);


        String name1 = childWeatherCondition.getWind().getWindIconId();
        int windIconId = ctx.getResources().getIdentifier(name1, "drawable", ctx.getPackageName());
        windIconIdIV.setImageResource(windIconId);

        humidityTV.setText(childWeatherCondition.getHumidity());
        rainTV.setText(childWeatherCondition.getRain());
        windDirectionTV.setText(childWeatherCondition.getWind().getWindDirection());
        windSpeedTV.setText(childWeatherCondition.getWind().getSpeed());
        maxTempTV.setText(childWeatherCondition.getMaxTemp());
        minTempTV.setText(childWeatherCondition.getMinTemp());
        dateTV.setText(childWeatherCondition.getDate());

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

2 个答案:

答案 0 :(得分:2)

在逻辑开始之前进行变量初始化 -

private void updateWhenReady(){

    System.out.println(validCurrent+ " " +  validDaily + " " + validHourly );

    weatherList = dbHelper.getWeatherConditionsHashMap();
    hourlyList = new ArrayList<WeatherCondition>(weatherList.keySet());
    adapter = new WeatherSearchListAdapter(getActivity().getApplicationContext(), weatherList, hourlyList);

    System.out.println(validCurrent + " " + validDaily + " " + validHourly);

    if(validCurrent && validDaily && validHourly)
    {   
        System.out.println("in");


        for (WeatherCondition wc: dailyResponseList)
        {
            ArrayList<WeatherCondition> tempList = new ArrayList<>();
            for(WeatherCondition w: hourlyResponseList)
            {
                if(w.getDate().equalsIgnoreCase(wc.getDate()))
                {
                    tempList.add(w);
                }
            }

            weatherList.put(wc,tempList);

        }

        for (WeatherCondition weatherCondition: weatherList.keySet())
        {
            System.out.println(" +   " + weatherCondition.getDate());
        }

        //Do you need to reset these? What if only two of the three were true?
            validCurrent = false;
            validDaily = false;
            validHourly = false;
        }
    }
    expList.setAdapter(adapter);
    /* If you are setting adapter every time *updateWhenReady* is invoked, no need to notifyDataSetChanged, because its a new adapter and layout will be drawn fresh */
    //adapter.notifyDataSetChanged();

}

答案 1 :(得分:0)

getChildView

使用fragment_daily_list_parent

进行充气

复制过去错误?