Volley,SwipeToRefresh和notifyDataSetChanged()不起作用

时间:2018-01-12 19:07:54

标签: android

首先,在你向我投票之前,我已经阅读过每一篇文章。 并没有帮助解决这个问题。

我使用Volley从我的服务器获取数据并将数据放入GridView中。

现在当我使用swipeToRefresh时,它应该加载新的/更改的数据。为此我尝试了一切,但没有任何效果。

首先是我的代码:

MainActivity.java

public class MainActivity extends AppCompatActivity {

    List<getter_setter> list = new ArrayList<>();
    grid_adapter grid_adapter;
    GridView gridView;
    SwipeRefreshLayout swipeRefreshLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        swipeRefreshLayout = findViewById(R.id.swipeRefresh);


        getData();
        gridView = findViewById(R.id.gridView);
        grid_adapter = new grid_adapter(getApplicationContext(),list);
        gridView.setAdapter(grid_adapter);

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                        getData();
                        grid_adapter.update(list);

                swipeRefreshLayout.setRefreshing(false);
            }
        });





    }

    public void getData(){
        final String url = "http://*.**.***.***/";
        final RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    JSONArray jsonArray = response.getJSONArray("userdata");
                    for(int i = 0; i < jsonArray.length(); i++){
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        String NAME = jsonObject.getString("NAME");
                        String BILD = jsonObject.getString("VORSCHAUBILD");

                        getter_setter getter_setter = new getter_setter(NAME,"",BILD);
                        list.add(getter_setter);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        requestQueue.add(jsonObjectRequest);
    }
}

和BaseAdapter

public class grid_adapter extends BaseAdapter{

    Context context;
    List<getter_setter> list;

    public grid_adapter(Context context, List<getter_setter> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    public void update(List<getter_setter> listUpdate){
       this.list.addAll(listUpdate);
       notifyDataSetChanged();
    }


    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        View gridView = view;
        if(view == null){
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            gridView = inflater.inflate(R.layout.grid_design,null);
        }

        final getter_setter getter = list.get(i);

        TextView name = gridView.findViewById(R.id.NAME);
        final ImageView bild = gridView.findViewById(R.id.bild);

        name.setText(getter.getNAME());
        Picasso.with(context).load(getter.getVORSCHAUBILD()).into(bild);

        return gridView;
    }
}

1 个答案:

答案 0 :(得分:0)

检查更新的侦听器

<强>解释 - :    因为您将列表的引用传递给RecyclerView因此在活动中更新列表将自动更新适配器中的列表

  

无需调用更新方法

swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                            list.clear(); // Clean The List Before Adding Updated Data
                            grid_adapter.notifyDataSetChanged();
                            getData();
                            grid_adapter.notifyDataSetChanged();

                    swipeRefreshLayout.setRefreshing(false);
                }
            });