我正在尝试从Refresh上的数据库获取更新数据 在onCreate中返回的数据是正确的,当我刷新它似乎返回的数据也正确(在Toast中)但gui不刷新新数据 我做错了什么!!
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");
}
}
我在onRefresh中创建的Toast打印正确的数据,但Gui不刷新
答案 0 :(得分:1)
通过执行此操作Descriptions = output.get(0);
,Descriptions
将开始指向新列表并丢失对适配器正在使用的列表的引用,因此具有适配器的列表和当前列表不再相同,因此问题
而不是这个
Descriptions = output.get(0);
titles = output.get(1);
ids = output.get(2);
FullText = output.get(3);
使用
// inside on refresh
data.clear();
//...code
Descriptions.addAll(output.get(0));
titles.addAll(get(1));
ids.addAll(get(2));
FullText.addAll(get(3));
getData();