There is a fragment code where for recyclerView
I put my adapter rvDownloadAdapter
If I fill the array before the adapter is initialized, then the elements are displayed in recyclerView
works. As for example, I initially add one element to the array.
Then I call AsyncTask where I get data from the PHPFunction
and in the PHPFunction
class PHPFunction
fill in the array and return it. But when onPostExecute
update the adapter in onPostExecute
it is not updated, I initially thought that the array was empty, but then I log the size of the array and it shows me that the size is four, ie array is not empty.
Here is the fragment code:
public class DownloadCentrFragment extends Fragment { Context context; Resources resources; TabLayout tabLayout; ViewPager viewPager; List<View> pages = new ArrayList<View>(); List<String> titles = new ArrayList<String>(); ArrayList<itemGetServerList> itemGetServerInfos = new ArrayList<itemGetServerList>(); RVDownloadAdapter rvDownloadAdapter = null; public DownloadCentrFragment() {} public DownloadCentrFragment setInstance() { DownloadCentrFragment fragment = new DownloadCentrFragment(); return fragment; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_download_centr, container, false); context = this.getActivity().getApplicationContext(); resources = this.getActivity().getResources(); tabLayout = (TabLayout) view.findViewById(R.id.tabLayout); viewPager = (ViewPager) view.findViewById(R.id.viewPager); titles.add(resources.getString(R.string.text_UsersList)); titles.add(resources.getString(R.string.text_YouList)); View v1 = inflater.inflate(R.layout.fragment_home_download_centr, null); EditText SearchText = (EditText) v1.findViewById(R.id.SearchText); ImageView Search = (ImageView) v1.findViewById(R.id.Search); ImageView Filtr = (ImageView) v1.findViewById(R.id.Filtr); RecyclerView recyclerView = (RecyclerView) v1.findViewById(R.id.recycler_view); LinearLayoutManager llm = new LinearLayoutManager(view.getContext()); recyclerView.setLayoutManager(llm); itemGetServerInfos.add(new itemGetServerList("Таблица умножения на два", 228, 5, "7-9", "Таблица на два от тред до семи", "","","arif","", null)); rvDownloadAdapter = new RVDownloadAdapter(itemGetServerInfos, view.getContext()); recyclerView.setAdapter(new AlphaInAnimationAdapter(rvDownloadAdapter)); ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(view.getContext(),pages,titles); pages.add(v1); viewPager.setAdapter(viewPagerAdapter); tabLayout.setupWithViewPager(viewPager); new LoadInfoTask().execute(); return view; } class LoadInfoTask extends AsyncTask<Void, Void, Void> { @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected Void doInBackground(Void... params) { itemGetServerInfos = new PHPFunction(resources, context).getInList(0,30); return null; } @Override protected void onPostExecute(Void result) { super.onPostExecute(result); rvDownloadAdapter.notifyDataSetChanged(); Log.d("myLog", itemGetServerInfos.size() + " sizes"); } } }
rvDownloadAdapter
public class RVDownloadAdapter extends RecyclerView.Adapter<RVDownloadAdapter.ViewHolder> { ArrayList<itemGetServerList> itemGetServerInfo = new ArrayList<itemGetServerList>(); Context context; public RVDownloadAdapter (ArrayList<itemGetServerList> itemGetServerInfoLoad, Context context) { this.itemGetServerInfo = itemGetServerInfoLoad; this.context = context; } public static class ViewHolder extends RecyclerView.ViewHolder { View view; CardView cardView; ImageView icon; TextView NameList; TextView Des; TextView Rating; TextView Age; ViewHolder(View itemView) { super(itemView); cardView = (CardView) itemView.findViewById(R.id.card_view); icon = (ImageView) itemView.findViewById(R.id.icon); NameList = (TextView) itemView.findViewById(R.id.nameList); Des = (TextView) itemView.findViewById(R.id.des); Rating = (TextView) itemView.findViewById(R.id.rating); Age = (TextView) itemView.findViewById(R.id.age); view = itemView; } } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_download_card, parent, false); ViewHolder vh = new ViewHolder(v); return vh; } @Override public void onBindViewHolder(final ViewHolder holder, final int position) { final itemGetServerList infoData = itemGetServerInfo.get(position); holder.NameList.setText(itemGetServerInfo.get(position).NameList); holder.Des.setText(itemGetServerInfo.get(position).Des); holder.Rating.setText(itemGetServerInfo.get(position).Rating + ""); holder.Age.setText(itemGetServerInfo.get(position).Age); switch (itemGetServerInfo.get(position).Tag) { case "orph": holder.icon.setImageDrawable(context.getResources().getDrawable(R.drawable.icon_orphographic)); break; case "arif": holder.icon.setImageDrawable(context.getResources().getDrawable(R.drawable.icon_arithmetic)); break; } } @Override public void onAttachedToRecyclerView(RecyclerView recyclerView) { super.onAttachedToRecyclerView(recyclerView); } @Override public int getItemCount() { return itemGetServerInfo.size(); } }
rvDownloadAdapter
class? You have designated the classAlphaInAnimationAdapter
as an adapter, it is for the mind that you need to donotifyDataSetChanged()
so that the list is updated. As data for theAlphaInAnimationAdapter
adapter assigned byRecyclerView
you are using another adapter or I do not understand your logic at all. - pavlofff