从片段上的onBindViewHolder中的recyclerview获取价值

时间:2019-04-25 18:19:59

标签: android android-recyclerview

我在片段上具有recyclerview,我想从recyclerview上的editText和spinner获取值,我尝试使用相同的代码进行活动,并且无错误地运行,但是在片段上始终会出错nullpointerexception。

片段中listAdapter的这段代码

public class ListAdapter extends RecyclerView.Adapter<OneFragment.ListAdapter.ViewHolder>
    {

        private ArrayList<DataQuestionGrading> dataList;
        private List<DataQuestionGrading> filterlist = null;

        public ListAdapter(ArrayList<DataQuestionGrading> data)
        {
            this.dataList = data;
            this.filterlist = new ArrayList(dataList);
        }

        public class ViewHolder extends RecyclerView.ViewHolder
        {
            TextView textQues;
            TextView idQues;
            TextView persenQue;
            EditText txtPart;

            public ViewHolder(View itemView)
            {
                super(itemView);
                this.textQues = (TextView) itemView.findViewById(R.id.gradingPart);
                this.idQues = (TextView) itemView.findViewById(R.id.idGrading);
                this.persenQue = (TextView) itemView.findViewById(R.id.persenGrading);
                this.txtPart = (EditText) itemView.findViewById(R.id.noteGrading);
            }
        }

        @NonNull
        @Override
        public ListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_grading, parent, false);

            OneFragment.ListAdapter.ViewHolder viewHolder = new OneFragment.ListAdapter.ViewHolder(view);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(final ListAdapter.ViewHolder holder,final int position) {

            holder.idQues.setText(dataList.get(position).getIdQue());
            holder.persenQue.setText(dataList.get(position).getPercentase());
            holder.textQues.setText(dataList.get(position).getPart_name());
            ViewGroup rootView = (ViewGroup) getView();
            for (int i = 0; i < dataList.size(); i++){
                View view = rootView.getChildAt(i);
                EditText notes = (EditText) view.findViewById(R.id.noteGrading);
                TextView ids = (TextView) view.findViewById(R.id.idGrading);
                TextView persens = (TextView) view.findViewById(R.id.persenGrading);
                kondisi = (Spinner) view.findViewById(R.id.kondisiGrading);

                //String pilihkondisi = kondisi.getSelectedItem().toString();
                kondisi.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> adapterView, View view, int position, long itemID) {

                        if(position != 0) {
                            if(kondisi.getItemAtPosition(position).toString() != null) {
                                pilihkondisi = adapterView.getItemAtPosition(position).toString();
                            }
                        }
                        //Toast.makeText(getActivity(), "Selected"+item, Toast.LENGTH_SHORT).show();


                    }
                    @Override
                    public void onNothingSelected(AdapterView<?> adapterView) {

                    }
                });
                String cek;

                if(pilihkondisi.equals("OKE")){
                    cek = "1";
                }else if (pilihkondisi.equals("BERMASALAH")){
                    cek = "2";
                }else{
                    cek = "3";
                }

                String note = notes.getText().toString();
                String not;
                if(note.matches("")){
                    not = "-";
                }else{
                    not = note;
                }
                String id = ids.getText().toString();
                String persen = persens.getText().toString();

                JSONObject part = new JSONObject();
                try {
                    part.put("id", id);
                    part.put("condition",not);
                    part.put("percentage",persen);
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                jsonChek1.put(part);
            }

        }

        @Override
        public int getItemCount() {
            return filterlist.size();
        }

    }

该代码在活动上起作用,但是当我在getText或setOnItemSelected的片段错误中使用时,我尝试使用ViewGroup进行更改,但仍然没有错误。我为此需要帮助

0 个答案:

没有答案