从recyclerview适配器获取不可见项目的数据

时间:2018-02-24 08:33:58

标签: android android-recyclerview recycle

我想得到你们的帮助我们可以解决这个问题我试图在mysql表中插入三行但是这里只有两行是可见的,第三次在滚动时可见,所以当我点击提交时它会让我一个错误它在屏幕上插入2个可见行并在3行上抛出空指针,这是不可见的,有些人可以帮助我从视图中获取不可见的数据我知道它的recyclerview并且它重新使用视图i知道机制我只想要一些解决方案,以便不可见的3行可以成功添加到数据库

这是一张图片:

https://imgur.com/a/rkARs

我的recyclerview适配器代码:

public class MyAdapterClosing extends RecyclerView.Adapter<MyAdapterClosing.myViewHolder> {

    private Context context;
    private List<DataofClosing> student;

    public MyAdapterClosing(Context context, List<DataofClosing> student){
        this.context = context;
        this.student = student;
    }

    @Override
    public myViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.layoutclosing,parent,false);
        return new myViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(myViewHolder holder, int position) {
        DataofClosing s = student.get(position);
        holder.brand.setText(s.Brandname);



    }


    public void updateList(List<RecyclerviewWholesale> student)
    {
        student = student;
        notifyDataSetChanged();
    }
    public void addItem(int position, DataofClosing stud)
    {
        student.add(position, stud);
        notifyItemInserted(position);
    }

    public void removeItem(int position) {
        student.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position, student.size());
    }



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


    @Override
    public int getItemViewType(int position) {

        return position;
    }



    public class myViewHolder extends RecyclerView.ViewHolder{
        TextView brand;
        EditText qts,pts,nip,ml90;

        public myViewHolder(View itemView) {
            super(itemView);

            brand = (TextView) itemView.findViewById(R.id.brand);
            qts = (EditText) itemView.findViewById(R.id.qts);
            pts = (EditText) itemView.findViewById(R.id.pts);
            nip = (EditText) itemView.findViewById(R.id.nip);
            ml90 = (EditText) itemView.findViewById(R.id.ml90);

            String qtss = qts.getText().toString();
            String ptss = pts.getText().toString();
            String nipp = nip.getText().toString();
            String ml900 = ml90.getText().toString();

            DataofClosing dataofClosing = new DataofClosing();
            dataofClosing.qts = qtss;
            dataofClosing.pts = ptss;
            dataofClosing.nip = nipp;
            dataofClosing.ml90 = ml900;



        }
    }

}

在主要活动中插入代码是:

class Insertclosing extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        /**
         * Creating product
         */
        protected String doInBackground(String... args) {
            for (int i = 0; i < mAdapter.getItemCount(); i++) {
                rcData = data.get(i);
                View view1 = closingService.getChildAt(i);
                EditText nameEditText = (EditText) view1.findViewById(R.id.qts);
                String name = nameEditText.getText().toString();
                EditText ptss = (EditText) view1.findViewById(R.id.pts);
                String pts = ptss.getText().toString();
                EditText nipp = (EditText) view1.findViewById(R.id.nip);
                String nip = nipp.getText().toString();
                EditText ml900 = (EditText) view1.findViewById(R.id.ml90);
                String ml90 = ml900.getText().toString();
                // Building Parameters
                List<NameValuePair> params = new ArrayList<NameValuePair>();

                params.add(new BasicNameValuePair("brand", rcData.Brandname));
                params.add(new BasicNameValuePair("quantity", name));
                params.add(new BasicNameValuePair("discount",pts));
                params.add(new BasicNameValuePair("ammount",nip ));


                // getting JSON Object
                // Note that create product url accepts POST method
                JSONObject json = jsonParser.makeHttpRequest(url_insert_product_sales,
                        "POST", params);

                // check log cat fro response
                Log.d("Create Response", json.toString());

                // check for success tag
                try {
                    int success = json.getInt(TAG_SUCCESS);

                    if (success == 1) {
                    /*runOnUiThread(new Runnable() {
                        public void run() {

                            Toast.makeText(ChangeAddress.this, "Order Placed Successfully", Toast.LENGTH_SHORT).show();
                        }
                    });*/
                    } else {
                        runOnUiThread(new Runnable() {
                            public void run() {

                                Toast.makeText(closing.this, "Try Again", Toast.LENGTH_SHORT).show();
                            }
                        });               // failed to create product
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         **/
        protected void onPostExecute(String file_url) {

             Toast.makeText(closing.this, "Closing added Successfully", Toast.LENGTH_SHORT).show();

        }
    }

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。 适配器有两种方法

onViewAttachedToWindow(RecyclerView.ViewHolder holder){}

onViewDetachedFromWindow(RecyclerView.ViewHolder holder){}

在适配器中创建 HashMap

public HashMap<Integer, RecyclerView.ViewHolder> holderHashMap = new HashMap<>();

并在onDetach中添加持有者并删除相同的onAttach。像这样。

 @Override
    public void onViewDetachedFromWindow(RecyclerView.ViewHolder holder) {
        holderHashMap.put(holder.getAdapterPosition(),holder);
        super.onViewDetachedFromWindow(holder);
    }

    @Override
    public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
        holderHashMap.remove(holder.getAdapterPosition());
        super.onViewAttachedToWindow(holder);

    }

访问所有viewHolders,包括不可见的viewHolders在你的活动中使用它。

for(int i =0; i<recyclerView.getItemCount();i++){
RecyclerView.ViewHolder holder = surveyQuestionList.findViewHolderForAdapterPosition(i);

            if(holder == null){
                holder = adapter.holderHashMap.get(i);
            }
}

这是迄今为止我提出的最佳解决方案。 让我知道它是否适合你。