I have an activity that contains 2 fragments, each fragment has 2 list views
, each has an adapter. Therefore, I get fairly long data json
from the server, I use AsyncTask
and post execute
, I analyze the data. The problem is that it freezes the loading animation for 4-5 seconds.
I tried using a stream Handler
, but the problem still exists, I have to do something terrible here.
public class DataService extends AsyncTask<String, String, Void> {
@Override
protected Void doInBackground(String... params) {
url = new URL(DATA_URL);
String jsonString = stringBuilder.toString();
jsonObject = new JSONObject(jsonString);
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
allDataFragment.setUpcomingDataList(UpcomingAllDataList);
awayFragment.setUpcomingDataList(tUpcomingAwayList);
homeFragment.setUpcomingDataList(tUpcomingHomeList);
allDataFragment.setPastDataList(pastAllDataList);
awayFragment.setPastDataList(pastAwayList);
homeFragment.setPastDataList(pastHomeList);
}
I added log messages to the adapter, and I see that it hangs during parsing, so it accepts all messages executed in the handler thread
Handler handler= new Handler();
handler.post(new Runnable() {
@Override
public void run() {
allDataFragment.setUpcomingDataList(UpcomingAllDataList);
awayFragment.setUpcomingDataList(tUpcomingAwayList);
homeFragment.setUpcomingDataList(tUpcomingHomeList);
allDataFragment.setPastDataList(pastAllDataList);
awayFragment.setPastDataList(pastAwayList);
homeFragment.setPastDataList(pastHomeList);
}
});
I also tried adding a handler to Adapter
, but it could not load the component UI
.
This is the code snippet of my snippet.
if (pastListView != null) {
PastListAdapter allGamesAdapter = new PastListAdapter(getContext(), pastAllDataList);
pastListView.setAdapter(allGamesAdapter);
}
Adapter .
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView vScore = (TextView) v.findViewById(R.id.v_score);
TextView date = (TextView) v.findViewById(R.id.date);
ImageView image = (ImageView) v.findViewById(R.id.iv_image);
vScore.setText(dataList.get(position).getScore);
date.setText(dataList.get(position).date);
Log.d(TAG, "getView: Row");
return v;
}
, , , .