Reputation: 39
i'm trying to get updated data from database on Refresh in onCreate the returned data is right and when i Refresh it seems that the returned data also right (in Toast ) but gui doesn't refresh with new data what i did wrong !!
public class NewsPage extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
private RecyclerView rv;
private rvAdapter adapter;
public static ArrayList<String> Descriptions = new ArrayList<>();
public static ArrayList<String> FullText = new ArrayList<>();
public static ArrayList<String> ids = new ArrayList<>();
public static List<rvData> data = new ArrayList<>();
public static int[] icons = {R.drawable.day7, R.drawable.day7, R.drawable.day7, R.drawable.day7, R.drawable.day7};
public static ArrayList<String> titles = new ArrayList<>();
public SwipeRefreshLayout strID;
GridTagsAdapter gridTagsAdapter;
public String tags[] = { "#???? ?????","#???? ???","#????? ?????","#???? ????" , "#????? ????????"};
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.news_screen, container, false);
strID = (SwipeRefreshLayout) v.findViewById(R.id.strID);
strID.setOnRefreshListener(this);
strID.post(new Runnable() {
@Override
public void run() {
JSONTask asyncTask = (JSONTask) new JSONTask(new JSONTask.AsyncResponse(){
@Override
public void processFinish(ArrayList<ArrayList<String>> output) {
strID.setRefreshing(true);
Descriptions = output.get(0);
titles = output.get(1);
ids = output.get(2);
FullText = output.get(3);
rv = (RecyclerView) v.findViewById(R.id.NewsScreen);
adapter = new rvAdapter(getActivity(), getData());
adapter.notifyDataSetChanged();
rv.setAdapter(adapter);
rv.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
rv.setNestedScrollingEnabled(false);
adapter.notifyDataSetChanged();
strID.setRefreshing(false);
}
}).execute("http://192.168.1.106:8000/database/news/g/?format=json");
}
});
return v;
}
public List<rvData> getData() {
for (int i = 0; i < Descriptions.size(); i++) {
rvData current = new rvData();
current.titleid = titles.get(i);
current.postbodyid = Descriptions.get(i);
current.imgid = icons[0];
gridTagsAdapter = new GridTagsAdapter(getActivity(),tags);
current.tags_adapter= gridTagsAdapter;
data.add(current);
}
return data;
}
@Override
public void onRefresh() {
JSONTask asyncTask = (JSONTask) new JSONTask(new JSONTask.AsyncResponse(){
@Override
public void processFinish(ArrayList<ArrayList<String>> output) {
strID.setRefreshing(true);
Descriptions.clear();
titles.clear();
ids.clear();
FullText.clear();
Descriptions = output.get(0);
titles = output.get(1);
ids = output.get(2);
FullText = output.get(3);
adapter.notifyDataSetChanged();
Toast.makeText(getActivity(), Descriptions.toString(), Toast.LENGTH_LONG).show();
strID.setRefreshing(false);
}
}).execute("http://192.168.1.106:8000/database/news/g/?format=json");
}
}
the Toast that i made it in onRefresh print right data but Gui doesn't refresh
Upvotes: 0
Views: 109
Reputation: 37404
By doing this Descriptions = output.get(0);
, Descriptions
will start pointing to new list and lost the reference to the list that is being used by the adapter so list with adapter and list in current are no longer same, hence the issue
Instead of this
Descriptions = output.get(0);
titles = output.get(1);
ids = output.get(2);
FullText = output.get(3);
Use
// inside on refresh
data.clear();
//...code
Descriptions.addAll(output.get(0));
titles.addAll(get(1));
ids.addAll(get(2));
FullText.addAll(get(3));
getData();
Upvotes: 1