在onclicklistener之后只显示Recycler视图的最后一个元素

时间:2016-11-01 17:55:35

标签: android android-recyclerview

我正在尝试从服务器检索我的数据,然后在RecyclerView的帮助下显示它。我的RecyclerView包含4个按钮,每个按钮将数据发送到不同的表。这就是我的尝试:

public class QuoteAdapter extends RecyclerView.Adapter<QuoteAdapter.ViewHolder> {
    List<QuoteList> qItems;
    String sQuote,sName,sEmail;
    public QuoteAdapter(List<QuoteList> qItems) {
        this.qItems = qItems;
    }

    @Override
    public QuoteAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.fetch_list, parent, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final QuoteAdapter.ViewHolder holder, int position) {
        final QuoteList codeListBeans = qItems.get(position);
        final Background background = new Background();
        holder.tv_quote.setText(codeListBeans.getQuote());
        holder.tv_name.setText(codeListBeans.getName());
        holder.tv_email.setText(codeListBeans.getEmail());
        sQuote=holder.tv_quote.getText().toString();
        sName=holder.tv_name.getText().toString();
        sEmail=holder.tv_email.getText().toString();

        holder.btn_other.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                url = "http://velapanti.esy.es/quotation/other/add.php";
                background.execute(sQuote, sEmail, sName);
            }
        });
        holder.btn_love.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                url = "http://velapanti.esy.es/quotation/love/add.php";
                background.execute(sQuote, sEmail, sName);
            }
        });
        holder.btn_mot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                url = "http://velapanti.esy.es/quotation/motivation/add.php";
                background.execute(sQuote, sEmail, sName);
            }
        });
        holder.btn_frnd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                url = "http://velapanti.esy.es/quotation/friends/add.php";
                background.execute(sQuote, sEmail, sName);
            }
        });

    }

    class ViewHolder extends RecyclerView.ViewHolder {

        public TextView tv_name, tv_quote, tv_email;
        public Button btn_other, btn_mot, btn_love, btn_frnd;
        public ViewHolder(View itemView) {
            super(itemView);
            tv_quote = (TextView) itemView.findViewById(R.id.tv_quote);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            tv_email = (TextView) itemView.findViewById(R.id.tv_email);
            btn_frnd = (Button) itemView.findViewById(R.id.btn_frnd);
            btn_love = (Button) itemView.findViewById(R.id.btn_love);
            btn_mot = (Button) itemView.findViewById(R.id.btn_mot);
            btn_other = (Button) itemView.findViewById(R.id.btn_other);
        }
    }

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

    class Background extends AsyncTask<String, String, String> {
        @Override
        protected void onPreExecute() {

            if (isConnection()) {
                super.onPreExecute();
                progressDialog.show();
            } else {
                showConnectionErrorDialog();
                startActivity(getIntent());
            }
        }

        @Override
        protected String doInBackground(String... strings) {
            sQuote = strings[0];
            sEmail = strings[1];
            sName = strings[2];

            try {
                URL openUrl = new URL(url);
                HttpURLConnection httpURLConnection = (HttpURLConnection) openUrl.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);

                OutputStream OS = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
                String data = URLEncoder.encode("quote", "UTF-8") + "=" + URLEncoder.encode(sQuote, "UTF-8")
                        + "&" +

                        URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(sEmail, "UTF-8")
                        + "&" +

                        URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(sName, "UTF-8");
                bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                OS.close();
                InputStream IS = httpURLConnection.getInputStream();
                IS.close();
                httpURLConnection.disconnect();
                return "Quotation Added";
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        @Override
        protected void onPostExecute(String result) {
            progressDialog.dismiss();
            Snackbar.make(layout, result, Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    }
}

我面临的问题是,当我点击任何按钮时,最后一行的数据移动到表格,无论我点击按钮的行,即如果我点击任何行的按钮,最后一行的数据得到发送但不是从按钮所属的行发送。

1 个答案:

答案 0 :(得分:1)

这里的问题很简单。在onBindViewHolder()方法中,您将获得datasQuotesEmailsName)并单独存储它们。现在,当RecyclerView完成向所有View添加和设置数据时,data容器也会更新,以便您的data包含上一个onBindView()的值调用

也就是说,如果您向下滚动并点击,则会从data发送View。同样,如果您要向上滚动,则会从最可见的View发送数据。

您需要提出一种方法,以便data保持更新。

不是从sQuote获取数据,而是从qItems获取数据。

同样作为一般规则,不要在onBindViewHolder()中创建新对象,尤其是在侦听器中创建新对象,而是在onCreateViewHolder()ViewHolder中创建新对象。因为无论何时滚动回View,都会创建另一个Object

对于需要点击View位置的听众,请将其放入ViewHolder(就像您的情况一样)。因此,您可以通过View轻松获取所点击的getAdapterPosition()的位置。