Hello.
I have a fragment. I in AsynsTask get data from the server and I want to display in ListView . The Sheet itself received from the server has 10 items, but it first fills the ListView an empty one and only when you call AsynsTask again, AsynsTask fills with the received data.
How do I fill the ListView to display a fragment?
UPD: fragment code:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { v = inflater.inflate(R.layout.my_fragment_get_post, container, false); listView = (ListView) v.findViewById(R.id.listFragmentPost); adapter = new PostListAdapter(getActivity(),R.layout.list_item_news,postsLists, 0); listView.setAdapter(adapter); new MyTask(getActivity()).execute(); listView.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView absListView, int scrollState) { if (scrollState == 0) { if (listView.getLastVisiblePosition() >= postsLists.size()-1) { new MyTask(getActivity()).execute(); } } } @Override public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) { } }); return v; } AsyncTask code:
class MyTask extends AsyncTask<Void, Void, ArrayList<String>>{ Activity activity; List<PostsList> list = new ArrayList<>(); ArrayList<String> arrayList = new ArrayList<>(); public MyTask(Activity activity){ this.activity = activity; } @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected ArrayList<String> doInBackground(Void... params) { BufferedReader reader = null; String resultJson = ""; HttpURLConnection urlConnection = null; JSONObject dataJsonObj = null; String link; JSONArray friends = null; /* ;*/ try { link = "http://anika-cs.by/app/liker/get_posts.php?id_vk="+ VKAccessToken.currentToken().userId+"&&offset=0"; URL url1 = new URL(link); urlConnection = (HttpURLConnection) url1.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.connect(); InputStream inputStream = urlConnection.getInputStream(); StringBuffer buffer1 = new StringBuffer(); reader = new BufferedReader(new InputStreamReader(inputStream)); String line; while ((line = reader.readLine()) != null) { buffer1.append(line); } resultJson = buffer1.toString(); dataJsonObj = new JSONObject(resultJson); friends = dataJsonObj.getJSONArray("xxx"); for (int i =0; i < friends.length(); i++) { JSONObject friend = friends.getJSONObject(i); String id_post = friend.getString("id_post"); String id_vk = friend.getString("id_vk"); String s =(id_vk+"_"+id_post); arrayList.add(s); } for(int i=0;i<arrayList.size();i++){ VKRequest zapros = VKApi.wall().getById(VKParameters.from(VKApiConst.POSTS, arrayList.get(i), VKApiConst.EXTENDED, 1)); final int finalI = i; int finalI1 = i; zapros.executeWithListener(new VKRequest.VKRequestListener() { @Override public void onComplete(VKResponse response) { PostListAdapter postListAdapter = null; try { JSONObject responseObject = response.json.getJSONObject("response"); Log.d("MyFragment", "responseObject = " + responseObject); JSONArray posts, groups, profiles; if (responseObject.has("groups")) groups = responseObject.getJSONArray("groups"); else groups = null; if (responseObject.has("profiles")) profiles = responseObject.getJSONArray("profiles"); else profiles = null; if (responseObject.has("items")) posts = responseObject.getJSONArray("items"); else posts = null; for (int i = 0; i < posts.length(); i++) { JSONObject post = posts.getJSONObject(i); if (groups == null || profiles == null) postsLists.add(attachClass.postObject(post)); else postsLists.add(attachClass.postObject(post,groups,profiles,v.getContext())); } adapter.notifyDataSetChanged(); } catch (Exception e) { } //adapter.notifyDataSetChanged(); } @Override public void attemptFailed(VKRequest request, int attemptNumber, int totalAttempts) { } @Override public void onError(VKError error) { } }); } } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } try { } catch (Exception e){ e.printStackTrace(); } return arrayList; } @Override protected void onPostExecute(ArrayList<String> lists) { } } } } The fact is that when you rotate the screen data is displayed. Thank you in advance.
ListViewto display the fragment will not work. You can load the data in the asynstask into the activation, and after the data has loaded, transfer it to the fragment and display it. Ps. Keeping a link to activations in asynstax is a very bad practice. - post_zeew