Arraylist变为null

时间:2016-08-09 08:58:44

标签: android json arraylist android-recyclerview

我从之前的活动中获取id,如果id为null然后我解析数据并将其存储在arraylist中,但如果它不为null则我不解析数据并尝试在listview中设置arraylist,但它显示arraylist null < / p>

       mRecyclerView = (ListView) findViewById(R.id.filter_orderlist);
        makeJsonArrayRequestCountry();

    }


    private void makeJsonArrayRequestCountry() {

        showpDialog();
        JsonArrayRequest req = new JsonArrayRequest( filter_url,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d("ress", response.toString());

                        filterList =new ArrayList<FilterModelClass>();
                        // Parsing json
                        for (int i = 0; i < response.length(); i++) {
                            try {

                                JSONObject obj = response.getJSONObject(i);

                                String heder=obj.getString("filterName");

                                System.out.println("Hader"+heder);

                                JSONArray details=obj.getJSONArray("getParam");
                                for(int j=0;j<details.length();j++)
                                {

                                    JSONObject det=details.getJSONObject(j);
                                    FilterModelClass movie = new FilterModelClass();
                                    movie.setFilter_Name(det.getString("paramName"));
                                    String cityid=movie.setFilter_ID(det.getString("paramId"));
                                    filterList.add(movie);

                                }

                                mAdapter = new MyCustomBaseAdapter(FilterListActivity.this,filterList);
                                mRecyclerView.setAdapter(mAdapter);

                                btnSelection.setOnClickListener(new View.OnClickListener() {

                                    @Override
                                    public void onClick(View v) {
                                        String data = "";
                                        List<FilterModelClass> stList = ((MyCustomBaseAdapter) mAdapter)
                                                .getStudentist();

                                        for (int i = 0; i < stList.size(); i++) {
                                            FilterModelClass singleStudent = stList.get(i);
                                            if (singleStudent.isselected() == true) {

                                                data = data+singleStudent.getFilter_ID().toString()+",";

                                            }

                                        }
                                        Intent intent=new Intent();
                                        intent.putExtra("filterid",data);
                                        setResult(RESULT_OK, intent);
                                        FilterListActivity.this.finish();

                                        Toast.makeText(FilterListActivity.this,
                                                "Selected Students:" + data, Toast.LENGTH_LONG)
                                                .show();


                                    }
                                });
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }

                        // notifying list adapter about data changes
                        // so that it renders the list view with updated data

                        hidepDialog();

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d("ErrorVolley", "Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_SHORT).show();
                hidepDialog();

            }
        });

        MyApplication.getInstance().addToReqQueue(req, "jreq");
    }


    private void showpDialog() {
        if (!pDialog.isShowing())
            pDialog.show();
    }

    private void hidepDialog() {
        if (pDialog.isShowing())
            pDialog.dismiss();
    }



    @Override
    public void onClick(View v) {

        switch (v.getId()) {


            default:
                break;
        }

    }




    public class MyCustomBaseAdapter extends BaseAdapter  {
        private  List<FilterModelClass> searchArrayList;
        ViewHolder holder;
        private LayoutInflater mInflater;
        SharedPreferences.Editor editor;
        Context context;

        public MyCustomBaseAdapter(Context mainActivity, List<FilterModelClass> results) {
            context = mainActivity;
            searchArrayList = results;
            mInflater = LayoutInflater.from(context);
        }

        public int getCount() {
            return searchArrayList.size();

        }

        public Object getItem(int position) {
            return searchArrayList.get(position);
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(final int position, View convertView, ViewGroup parent)
        {
            SharedPreferences sharedPrefs = context.getSharedPreferences("sharedPrefs", Context.MODE_PRIVATE);


            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.sorting_items, null);
                holder = new ViewHolder();
                holder.txtName = (TextView) convertView.findViewById(R.id.tvName);
                holder.cB = (CheckBox)convertView.findViewById(R.id.chkSelected);

                convertView.setTag(holder);
            }
            else {
                holder = (ViewHolder) convertView.getTag();
            }


            editor = sharedPrefs.edit();

            holder.txtName.setText(searchArrayList.get(position).getFilter_Name());

            holder.cB.setChecked(sharedPrefs.getBoolean("CheckValue" + position, false));
            holder.cB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {


                    editor.putBoolean("CheckValue" + position, isChecked);
                    editor.commit();
                }});
            return convertView;
        }

        class ViewHolder {
            TextView txtName;
            CheckBox cB;
        }
        public List<FilterModelClass> getStudentist() {
            return searchArrayList;
        }

    }
}

2 个答案:

答案 0 :(得分:3)

您尚未为LayoutManager设置RecyclerView: 使用此选项将LayoutManager设置为RecyclerView

 mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
在oncreate中

答案 1 :(得分:1)

问题在于这部分

if(filtrid!=null)
    {
        System.out.println("datacat null"+filterList);
        mAdapter = new CardViewDataAdapter(filterList);
        mRecyclerView.setAdapter(mAdapter);

    }

您已阅读filtrid,但在这种情况下您不会初始化filterList

修改

从意图附加内容中检索filtrid与填充filterList不同。您可以以相同的方式将filterList作为JSON-String传递,您传递了filtrid。然后像在makeJsonArrayRequestCountry()中那样解析JSON-String。更好的方法是,存储列表持久性(SharedPreferences,SQL,...),或者根本不存储它。只要您开始该活动,只需从服务器加载列表即可。