gpt4 book ai didi

java - 如何让我的 ExpandableListView 等待加载,直到填充我的 HashMap?

转载 作者:太空宇宙 更新时间:2023-11-04 13:32:30 26 4
gpt4 key购买 nike

我在 android 中使用 ExpandableListView,但问题是我的代码在填充 HashMap 之前加载了它,因此它显示了一个空的 ListView。

我加载 ExpandableListView 的 fragment 是这个:

package com.example.arno.eventaris.Fragment;


import android.app.Activity;
import android.app.DialogFragment;
import android.net.wifi.WifiConfiguration;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;




import com.example.arno.eventaris.Activity.MainActivity;
import com.example.arno.eventaris.Error.ErrorJason;

import com.example.arno.eventaris.Expandable.ExpandableListAdapter;
import com.example.arno.eventaris.R;


import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.widget.Toast;



/**
* A simple {@link Fragment} subclass.
*/
public class HomeFragment extends Fragment {

private OnHomeFragmentInteractionListener mListener;
private View view;

private ExpandableListAdapter listAdapter;
private ExpandableListView expListView;
private List<String> listDataHeader;
private HashMap<String, List<String>> listDataChild;
private HashMap<String,HashMap<String,String>> events;

public HomeFragment() {
// Required empty public constructor
}




@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
((MainActivity)getActivity()).populateEvents();

events = ((MainActivity)getActivity()).getEvents();

view=inflater.inflate(R.layout.fragment_home, container, false);


// get the listview
expListView = (ExpandableListView) view.findViewById(R.id.lvExp);

// preparing list data
prepareListData();

listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild);

// setting list adapter
expListView.setAdapter(listAdapter);



return view;
}



/*
* Preparing the list data
*/
private void prepareListData() {



listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();

// Adding child data
listDataHeader.add("A");
listDataHeader.add("B");
listDataHeader.add("C");
listDataHeader.add("D");
listDataHeader.add("E");
listDataHeader.add("F");
listDataHeader.add("G");
listDataHeader.add("H");
listDataHeader.add("I");
listDataHeader.add("J");
listDataHeader.add("K");
listDataHeader.add("L");
listDataHeader.add("M");
listDataHeader.add("N");
listDataHeader.add("O");
listDataHeader.add("P");
listDataHeader.add("Q");
listDataHeader.add("R");
listDataHeader.add("S");
listDataHeader.add("T");
listDataHeader.add("U");
listDataHeader.add("V");
listDataHeader.add("W");
listDataHeader.add("X");
listDataHeader.add("Y");
listDataHeader.add("Z");
listDataHeader.add("*");

// Adding child data
List<String> A = new ArrayList<String>();
List<String> B = new ArrayList<String>();
List<String> C = new ArrayList<String>();
List<String> D = new ArrayList<String>();
List<String> E = new ArrayList<String>();
List<String> F = new ArrayList<String>();
List<String> G = new ArrayList<String>();
List<String> H = new ArrayList<String>();
List<String> I = new ArrayList<String>();
List<String> J = new ArrayList<String>();
List<String> K = new ArrayList<String>();
List<String> L = new ArrayList<String>();
List<String> M = new ArrayList<String>();
List<String> N = new ArrayList<String>();
List<String> O = new ArrayList<String>();
List<String> P = new ArrayList<String>();
List<String> Q = new ArrayList<String>();
List<String> R = new ArrayList<String>();
List<String> S = new ArrayList<String>();
List<String> T = new ArrayList<String>();
List<String> U = new ArrayList<String>();
List<String> V = new ArrayList<String>();
List<String> W = new ArrayList<String>();
List<String> X = new ArrayList<String>();
List<String> Y = new ArrayList<String>();
List<String> Z = new ArrayList<String>();
List<String> anders = new ArrayList<String>();

for(String key : events.keySet())
{
String eersteLetter = Character.toString(key.charAt(0));

switch(eersteLetter)
{
case "a" : A.add(key);
break;
case "b" : B.add(key);
break;
case "c" : C.add(key);
break;
case "d" : D.add(key);
break;
case "e" : E.add(key);
break;
case "f" : F.add(key);
break;
case "g" : G.add(key);
break;
case "h" : H.add(key);
break;
case "i" : I.add(key);
break;
case "j" : J.add(key);
break;
case "k" : K.add(key);
break;
case "l" : L.add(key);
break;
case "m" : M.add(key);
break;
case "n" : N.add(key);
break;
case "o" : O.add(key);
break;
case "p" : P.add(key);
break;
case "q" : Q.add(key);
break;
case "r" : R.add(key);
break;
case "s" : S.add(key);
break;
case "t" : T.add(key);
break;
case "u" : U.add(key);
break;
case "v" : V.add(key);
break;
case "w" : W.add(key);
break;
case "x" : X.add(key);
break;
case "y" : Y.add(key);
break;
case "z" : Z.add(key);
break;
default : anders.add(key);
break;

}
}

listDataChild.put(listDataHeader.get(0), A); // Header, Child data
listDataChild.put(listDataHeader.get(1), B);
listDataChild.put(listDataHeader.get(2), C);
listDataChild.put(listDataHeader.get(3), D);
listDataChild.put(listDataHeader.get(4), E);
listDataChild.put(listDataHeader.get(5), F);
listDataChild.put(listDataHeader.get(6), G);
listDataChild.put(listDataHeader.get(7), H);
listDataChild.put(listDataHeader.get(8), I);
listDataChild.put(listDataHeader.get(9), J);
listDataChild.put(listDataHeader.get(10), K);
listDataChild.put(listDataHeader.get(11), L);
listDataChild.put(listDataHeader.get(12), M);
listDataChild.put(listDataHeader.get(13), N);
listDataChild.put(listDataHeader.get(14), O);
listDataChild.put(listDataHeader.get(15), P);
listDataChild.put(listDataHeader.get(16), Q);
listDataChild.put(listDataHeader.get(17), R);
listDataChild.put(listDataHeader.get(18), S);
listDataChild.put(listDataHeader.get(19), T);
listDataChild.put(listDataHeader.get(20), U);
listDataChild.put(listDataHeader.get(21), V);
listDataChild.put(listDataHeader.get(22), W);
listDataChild.put(listDataHeader.get(23), X);
listDataChild.put(listDataHeader.get(24), Y);
listDataChild.put(listDataHeader.get(25), Z);
listDataChild.put(listDataHeader.get(26), anders);

}


@Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
try{
mListener = (OnHomeFragmentInteractionListener) activity;

}
catch (ClassCastException e)
{
throw new ClassCastException(activity.toString() + "must implement OnFragmentInteractionListener");
}
}

@Override
public void onDetach()
{
super.onDetach();
mListener = null;
}



public interface OnHomeFragmentInteractionListener
{



}


}

问题是我用于 ExpandableListView 的数据来自于我放入 MaintActivity.Java 中的 HashMap 中的 JSON 文件。

现在它会遍历我的整个 HomeFragment.java 代码直到最后,并且只有当发生这种情况时它才会开始执行这行代码:

((MainActivity)getActivity()).populateEvents();

我已将其放在 onCreateView 方法的开头。我跟踪了调试器,所以我知道他遵循的顺序不是我想要的。有没有办法确保他在继续创建 ExpendableListView 之前执行这行代码?

最佳答案

我建议您使用AsyncTask来填充数据。您将在 doInBackground() 方法中加载数据,然后在 onPostExecute() 中创建适配器。

1) 创建 AsyncTask 类:

private class PopulatingTask extends AsyncTask<Void, Void, Boolean> {

private ProgressDialog dialog;
private MainActivity activity;

public PopulatingTask (MainActivity activity) {
this.activity = activity;
dialog = new ProgressDialog(activity);
}

@Override
protected void onPreExecute() {
this.dialog.setMessage("Loading data...");
this.dialog.show();
}

@Override
protected Boolean doInBackground(Void... args) {
try{

activity.populateEvents();

events = activity.getEvents();

// preparing list data
prepareListData();

return true;

} catch (Exception e){
Log.e("tag", "error", e);
return false;
}
}

@Override
protected void onPostExecute(Boolean success) {

listAdapter = new ExpandableListAdapter(activity, listDataHeader, listDataChild);

// setting list adapter
expListView.setAdapter(listAdapter);

// dismiss the dialog
if (dialog.isShowing()) {
dialog.dismiss();
}

if (success) {
Toast.makeText(activity, "OK", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(activity, "Error", Toast.LENGTH_LONG).show();
}
}
}

2) 将您的 onCreateView() 方法更改为:

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

view=inflater.inflate(R.layout.fragment_home, container, false);

// get the listview
expListView = (ExpandableListView) view.findViewById(R.id.lvExp);

// start the asyncTask
new PopulatingTask((MainActivity)getActivity()).execute();

return view;
}

关于java - 如何让我的 ExpandableListView 等待加载,直到填充我的 HashMap?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32029517/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com