使用数据库中的 SwipeRefresh 更新 RecyclerView

update RecyclerView with SwipeRefresh from database

我正尝试在刷新时从数据库中获取更新的数据 在 onCreate 中返回的数据是正确的,当我 Refresh 时似乎返回的数据也是正确的(在 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 不刷新

通过这样做 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();