Arraylist变为空(Arraylist become null)

231 IT屋

I am getting id from previous activity,if id is null then i parse data and store it in arraylist,but if it is not null then i dont parse data and trying to set arraylist in listview,but it shows arraylist null

       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;
        }

    }
}
解决方案

You have not set a LayoutManager to your RecyclerView: use this to set LayoutManager to your RecyclerView:

 mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

in oncreate

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

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

}


私人void makeJsonArrayRequestCountry(){

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

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

JSONObject obj = response.getJSONObject(i);

String heder = obj.getString ( filterName);

System.out.println( H​​ader + 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){
字符串数据= ;
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,
选定的学生: +数据,Toast.LENGTH_LONG)
.show();


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

}

//向列表适配器通知有关数据更改的信息
//以便它呈现具有更新数据
的列表视图
hidepDialog();

}
},新的Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError错误){
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() ){


默认值:
休息;
}

}




公共类MyCustomBaseAdapter扩展了BaseAdapter {
private List< FilterModelClass> searchArrayList;
ViewHolder持有人;
私人LayoutInflater mInflater;
SharedPreferences.Editor编辑器;
上下文上下文;

public MyCustomBaseAdapter(Context mainActivity,List< FilterModelClass>结果){
context = mainActivity;
searchArrayList =结果;
mInflater = LayoutInflater.from(上下文);
}

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

}

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

public long getItemId(int 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);
持有人= 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();
}


编辑器= 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;
}

}
}

解决方案

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



  mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); 


在oncreate


本文地址:IT屋 » Arraylist变为空