java расходная информация lastadapter notifysetdatachanged не работает

В моем проекте я загружаю в страны, чтобы отобразить их на экране. Однако он асинхронный, поэтому загрузка занимает некоторое время. Поэтому я хочу обновить экран, когда он загружен, но представление вообще не обновляется, когда я вызываю notifydatasetchanged с BaseExpandableListAdapter { . Ниже приведен код:

Деятельность:

public class listCountries extends AppCompatActivity {

    ExpandableListView expandableListView;
    List<String> expandableListTitle;//
    HashMap<String, List<String>> expandableListDetail;
    static HashMap<String, List<String>> expendable;
    CountryAdapter expandableListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_countries);
        expendable  = new HashMap<>();
        expandableListView = (ExpandableListView) findViewById(R.id.expLV);
        new getData().execute();

        expandableListDetail = expendable;
        expandableListTitle = new ArrayList<String>(expandableListDetail.keySet());
        expandableListAdapter = new CountryAdapter(this, expandableListTitle, expandableListDetail);
        expandableListView.setAdapter(expandableListAdapter);
        expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

            @Override
            public void onGroupExpand(int groupPosition) {
                Toast.makeText(getApplicationContext(),
                        expandableListTitle.get(groupPosition) + " List Expanded.",
                        Toast.LENGTH_SHORT).show();
            }
        });

        expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {

            @Override
            public void onGroupCollapse(int groupPosition) {
                Toast.makeText(getApplicationContext(),
                        expandableListTitle.get(groupPosition) + " List Collapsed.",
                        Toast.LENGTH_SHORT).show();

            }
        });

        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                                        int groupPosition, int childPosition, long id) {
                Toast.makeText(
                        getApplicationContext(),
                        expandableListTitle.get(groupPosition)
                                + " -> "
                                + expandableListDetail.get(
                                expandableListTitle.get(groupPosition)).get(
                                childPosition), Toast.LENGTH_SHORT
                ).show();
                return false;
            }
        });
    }






    public class getData extends AsyncTask<Void, Void, String> {

        protected String doInBackground(Void...urls) {
            try {
                URL url = new URL("http://api.geonames.org/countryInfo?username=brian");
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                try {
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                    StringBuilder stringBuilder = new StringBuilder();
                    String line;
                    while ((line = bufferedReader.readLine()) != null) {
                        stringBuilder.append(line).append("\n");
                    }
                    bufferedReader.close();
                    return stringBuilder.toString();
                }
                finally{
                    urlConnection.disconnect();

                }
            }
            catch(Exception e) {
                Log.e("ERROR", e.getMessage(), e);
                return null;
            }
        }

        protected void onPostExecute(String text){
            getCountries(text);
        }

        public void getCountries(String text){

            List<String> africa = new ArrayList<String>();
            List<String> nAmerica = new ArrayList<String>();
            List<String> sAmerica = new ArrayList<String>();
            List<String> antarctica = new ArrayList<String>();
            List<String> europe = new ArrayList<String>();
            List<String> asia = new ArrayList<String>();
            List<String> oceania = new ArrayList<String>();

            Country countries [] = filterData(text);
            for(int i = 0; i < countries.length-2; i++){
                switch (countries[i].getContinent()){
                    case ("EU"):
                        europe.add(countries[i].getCountryName());
                        break;
                    case ("AS"):
                        asia.add(countries[i].getCountryName());
                        break;
                    case ("AF"):
                        africa.add(countries[i].getCountryName());
                        break;
                    case ("NA"):
                        nAmerica.add(countries[i].getCountryName());
                        break;
                    case ("SA"):
                        sAmerica.add(countries[i].getCountryName());
                        break;
                    case ("AN"):
                        antarctica.add(countries[i].getCountryName());
                        break;
                    case ("OC"):
                        oceania.add(countries[i].getCountryName());
                        break;
                }
            }
            expendable = new HashMap<>();
            expendable.put("Europe", europe);
            expendable.put("Asia", asia);
            expendable.put("Antarctica", antarctica);
            expendable.put("North America", nAmerica);
            expendable.put("South America", sAmerica);
            expendable.put("Africa", africa);
            expendable.put("Oceania", oceania);
            expandableListAdapter.notifyDataSetChanged();
        }

}

Класс адаптера:

public class CountryAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<String> expandableListTitle;
    private HashMap<String, List<String>> expandableListDetail;


    public CountryAdapter(Context context, List<String> expandableListTitle,
                          HashMap<String, List<String>> expandableListDetail) {
        this.context = context;
        this.expandableListTitle = expandableListTitle;
        this.expandableListDetail = expandableListDetail;
    }


    @Override
    public Object getChild(int listPosition, int expandedListPosition) {
        return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
                .get(expandedListPosition);
    }

    @Override
    public long getChildId(int listPosition, int expandedListPosition) {
        return expandedListPosition;
    }

    @Override
    public View getChildView(int listPosition, final int expandedListPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        final String expandedListText = (String) getChild(listPosition, expandedListPosition);
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.list_item, null);
        }
        TextView expandedListTextView = (TextView) convertView
                .findViewById(R.id.expandedListItem);
        expandedListTextView.setText(expandedListText);
        return convertView;
    }

    @Override
    public int getChildrenCount(int listPosition) {
        return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
                .size();
    }

    @Override
    public Object getGroup(int listPosition) {
        return this.expandableListTitle.get(listPosition);
    }

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

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

    @Override
    public View getGroupView(int listPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String listTitle = (String) getGroup(listPosition);
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.context.
                    getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.list_group, null);
        }
        TextView listTitleTextView = (TextView) convertView
                .findViewById(R.id.listTitle);
        listTitleTextView.setTypeface(null, Typeface.BOLD);
        listTitleTextView.setText(listTitle);
        return convertView;
    }

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

    @Override
    public boolean isChildSelectable(int listPosition, int expandedListPosition) {
        return true;
    }
}

0 ответов

Другие вопросы по тегам