Listview仅在滚动或触摸后显示所有项目

时间:2018-09-05 09:39:07

标签: android listview

我的列表视图仅在滚动或触摸后显示所有项目。

这是显示项目的方法:

private void callVolley(){
    itemList.clear();

    pDialog = new ProgressDialog(getActivity());
    pDialog.setMessage("Cargando temas de la materia seleccionada...");
    showDialog();


    JsonArrayRequest jArr = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            Log.d(TAG, response.toString());
            hideDialog();

            // Parsing json
            for (int i = 0; i < response.length(); i++) {
                try {
                    JSONObject obj = response.getJSONObject(i);

                    Data item = new Data();

                    item.setMenu(obj.getString(TAG_NOMBRE));
                    item.setId(obj.getString(TAG_ID));


                    itemList.add(item);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }


            adapter.notifyDataSetChanged();
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            hideDialog();
        }
    });


    AppController.getInstance().addToRequestQueue(jArr);
}

这是Adapter类:

public class Adapter extends BaseAdapter {

    private Context activity;
    private ArrayList<Data> data;
    private static LayoutInflater inflater = null;
    private View vi;
    private ViewHolder viewHolder;

    public Adapter(Context context, ArrayList<Data> items) {
        this.activity = context;
        this.data = items;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

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

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

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        vi = view;
        final int pos = position;
        Data items = data.get(pos);

        if(view == null) {
            vi = inflater.inflate(R.layout.list_row, null);
            viewHolder = new ViewHolder();
            viewHolder.checkBox = (CheckBox) vi.findViewById(R.id.cb);
            viewHolder.menu = (TextView) vi.findViewById(R.id.nama_menu);
            vi.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) view.getTag();
            viewHolder.menu.setText(items.getMenu());
        }

        if(items.isCheckbox()){
            viewHolder.checkBox.setChecked(true);
        } else {
            viewHolder.checkBox.setChecked(false);
        }

        return vi;
    }

    public ArrayList<Data> getAllData(){
        return data;
    }

    public void setCheckBox(int position){
        Data items = data.get(position);
        items.setCheckbox(!items.isCheckbox());
        notifyDataSetChanged();
    }

    public class ViewHolder{
        TextView menu;
        CheckBox checkBox;
    }
}

2 个答案:

答案 0 :(得分:0)

替换adapter.notifyDataSetChanged();与  adapter(context,itemList); 它会工作,有时是由于未通知后端响应适配器的奇数数据。

答案 1 :(得分:0)

 //set  adapter
    private void SetListAdapter(final List<String> List) {
        Adapter listAdapter = new Adapter(YourActivity.instance, List);
        binding.listview.setAdapter(listAdapter);

    }