Data fails to be updated in expandable list

故事扮演 提交于 2020-01-07 03:52:05

问题


I am calling three JSONRequests with Volley and this method is called at every response of the request. This method only is executed when all three responses are ready. But when I try to update the expandable list it fails to display on the screen. Could anyone see if I am doing something wrong?

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

}

and the following is the explist adapter

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

回答1:


Do the variable initializations before the logic starts -

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

}



回答2:


In getChildView

inflate using fragment_daily_list_parent

copy-past error?



来源:https://stackoverflow.com/questions/35660972/data-fails-to-be-updated-in-expandable-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!