【问题标题】:How to get text from TextView in RecyclerVIew?如何从 RecyclerVIew 中的 TextView 获取文本?
【发布时间】:2018-03-27 15:25:45
【问题描述】:

我正在使用 Volley 将列表从 MySQL 数据库加载到 Recycler 视图。列表非常顺利地进入回收站视图,没有任何问题。但现在我想从我的回收站列表中阅读问题。我已经为我的活动实现了接口。但是当我点击列表时,它什么也没有显示。请帮助我完成我的任务。应用程序没有显示错误,它只是在列表点击时什么都不做。

这是我的适配器-

public class ProductsAdapter extends RecyclerView.Adapter<ProductsAdapter.ProductViewHolder> {

private Context mCtx;
private List<Question> qaList;
private static RecyclerViewClickListener itemListener;

public ProductsAdapter(Context mCtx, List<Question> qaList,RecyclerViewClickListener itemListener) {
    this.mCtx = mCtx;
    this.qaList = qaList;
    this.itemListener = itemListener;
}

@Override
public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(mCtx);
    View view = inflater.inflate(R.layout.product_list, null);
    return new ProductViewHolder(view);
}

@Override
public void onBindViewHolder(ProductViewHolder holder, int position) {
    Question que = qaList.get(position);

    holder.textViewQue.setText(que.getQue());
    holder.textViewA.setText(que.getopA());
    holder.textViewB.setText(que.getopB());
    holder.textViewC.setText(que.getopC());
    holder.textViewD.setText(que.getopD());
    holder.textViewAns.setText(que.getAns());
}

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

public static class ProductViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    TextView textViewQue, textViewA, textViewB, textViewC, textViewD, textViewAns;

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

        textViewQue = itemView.findViewById(R.id.tv_Que);
        textViewA = itemView.findViewById(R.id.tvOpt_A);
        textViewB = itemView.findViewById(R.id.tvOpt_B);
        textViewC = itemView.findViewById(R.id.tvOpt_C);
        textViewD = itemView.findViewById(R.id.tvOpt_D);
        textViewAns = itemView.findViewById(R.id.tv_Ans);

        itemView.setOnClickListener(this);
    }
   @Override
   public void onClick(View v)
   {
       itemListener.recyclerViewListClicked(v, this.getLayoutPosition());

   }
  }
}

这是我的界面-

public interface RecyclerViewClickListener {
   public void recyclerViewListClicked(View v, int position);
}

这是我的模型-

public class Question {

  public String Que;
  private String opA;
  private String opB;
  private String opC;
  private String opD;
  private String Ans;

public Question(String Que, String opA,String opB,String opC,String opD,String Ans ) {

    this.Que = Que;
    this.opA = opA;
    this.opB = opB;
    this.opC = opC;
    this.opD = opD;
    this.Ans = Ans;
}

public String getQue() {
    return Que;
}

public String getopA() {
    return opA;
}

public String getopB() {
    return opB;
}

public String getopC() {
    return opC;
}

public String getopD() {
    return opD;
}

public String getAns() {
 return Ans; 
}
}

这是我的活动-

public class QuestionList extends AppCompatActivity implements RecyclerViewClickListener {

private static final String URL_PRODUCTS = "http://xxxxxxxxxxxxxxxxx";    

//a list to store all the products
List< Question> qaList;    
RecyclerViewClickListener listener;   
RecyclerView recyclerView;
private ProductsAdapter adapter1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_question_list);


    //getting the recyclerview from xml
    recyclerView = (RecyclerView) findViewById(R.id.recylcerView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    //initializing the productlist
    qaList = new ArrayList<>();
          loadProducts();

}

private void loadProducts() {

    /*
    * Creating a String Request
    * The request type is GET defined by first parameter
    * The URL is defined in the second parameter
    * Then we have a Response Listener and a Error Listener
    * In response listener we will get the JSON response as a String
    * */
    StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_PRODUCTS,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        //converting the string to json array object
                        JSONArray array = new JSONArray(response);

                        //traversing through all the object
                        for (int i = 0; i < array.length(); i++) {

                            //getting product object from json array
                            JSONObject product = array.getJSONObject(i);

                            //adding the product to product list
                            qaList.add(new Question(

                                    product.getString("Que"),
                                    product.getString("opA"),
                                    product.getString("opB"),
                                    product.getString("opC"),
                                    product.getString("opD"),
                                    product.getString("Ans")

                            ));
                        }

                        //creating adapter object and setting it to recyclerview
                           adapter1 = new ProductsAdapter(QuestionList.this, qaList,listener);
                          recyclerView.setAdapter(adapter1);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });

    //adding our stringrequest to queue
    Volley.newRequestQueue(this).add(stringRequest);
}


@Override
public void recyclerViewListClicked(View v, int position) {

 //I want to read question that has been clicked by user. Nothing haapening here.   
 Toast.makeText(this,qaList.get(position).Que,Toast.LENGTH_LONG).show();
}
}

【问题讨论】:

  • 吐司显示了吗?
  • 看起来 RecyclerViewClickListener listener 中的 QuestionList 类从未初始化。但它至少应该导致 NullPointerException。你确定没有执行/崩溃?
  • 既不吐司也不崩溃。

标签: android android-recyclerview


【解决方案1】:

getAdapterPosition();代替this.getLayoutPosition()

【讨论】:

    【解决方案2】:

    试试onBindViewHolder

    @Override
    public void onBindViewHolder(ProductViewHolder holder, int position) {
    Question que = qaList.get(position);
    
    holder.textViewQue.setText(que.getQue());
    holder.textViewA.setText(que.getopA());
    holder.textViewB.setText(que.getopB());
    holder.textViewC.setText(que.getopC());
    holder.textViewD.setText(que.getopD());
    holder.textViewAns.setText(que.getAns());
    
    holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                  itemListener.recyclerViewListClicked(v, position);        
                }
            });
    }
    

    【讨论】:

      【解决方案3】:

      让我们向您展示我的做法,但首先我认为您必须将 this.getLayoutPosition() 更改为 getAdapterPosition()

      @Override
      public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
          holder.bind();
      }
      
      public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
      
          ViewHolder(View itemView) {
              super(itemView);
          }
      
          @Override
          public void onClick(View view) {
              if (mClickListener != null)
                  mClickListener.onItemClick(view, getAdapterPosition());
          }
      
          void bind(){
              int position = getAdapterPosition();
              // this is how you use adapter position to get data from your Collection / Array
          }
      }
      
      // allows clicks events to be caught
      public void setClickListener(ItemClickListener itemClickListener) {
          this.mClickListener = itemClickListener;
      }
      
      // parent activity will implement this method to respond to click events
      public interface ItemClickListener {
          void onItemClick(View view, int position);
      }
      

      【讨论】:

        【解决方案4】:

        您的RecyclerViewClickListener listener 未在您的activty 中初始化

        所以你需要像这样在QuestionList 活动的 onCreate 方法中初始化你的监听器

         RecyclerViewClickListener listener;
        
        listener = new RecyclerViewClickListener() {
                    @Override
                    public void recyclerViewListClicked(View v, int position) {
                        Toast.makeText(getApplicationContext(), String.valueOf(position),Toast.LENGTH_LONG).show();
                    }
                };
        

        你也应该使用 getAdapterPosition(); 而不是 this.getLayoutPosition() 就像 Saurabh 所说的那样

        【讨论】:

          【解决方案5】:

          对代码进行一些更改,获取适配器类中的接口并定义对象添加访问它。我对代码下面使用的适配器类进行了一些更改..

          public class ProductsAdapter extends RecyclerView.Adapter<ProductsAdapter.ProductViewHolder> {
          
          private Context mCtx;
          private List<Question> qaList;
          RecyclerViewClickListener recyclerViewClickListener;
          
          public void setRecyclerViewClickListener(RecyclerViewClickListener recyclerViewClickListener) {
              this.recyclerViewClickListener = recyclerViewClickListener;
          }
          
          public ProductsAdapter(Context mCtx, List<Question> qaList) {
              this.mCtx = mCtx;
              this.qaList = qaList;
          }
          
          public interface RecyclerViewClickListener {
               void recyclerViewListClicked(View v, int position);
          }
          
          @Override
          public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
              LayoutInflater inflater = LayoutInflater.from(mCtx);
              View view = inflater.inflate(R.layout.product_list, null);
              return new ProductViewHolder(view);
          }
          
          @Override
          public void onBindViewHolder(ProductViewHolder holder, int position) {
              Question que = qaList.get(position);
          
              holder.textViewQue.setText(que.getQue());
              holder.textViewA.setText(que.getopA());
              holder.textViewB.setText(que.getopB());
              holder.textViewC.setText(que.getopC());
              holder.textViewD.setText(que.getopD());
              holder.textViewAns.setText(que.getAns());
              holder.itemView.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
                      recyclerViewClickListener.recyclerViewListClicked(view,position);
                  }
              });
          }
          
          @Override
          public int getItemCount() {
              return qaList.size();
          }
          
          public static class ProductViewHolder extends RecyclerView.ViewHolder{
              TextView textViewQue, textViewA, textViewB, textViewC, textViewD, textViewAns;
          
              public ProductViewHolder(View itemView) {
                  super(itemView);
          
                  textViewQue = itemView.findViewById(R.id.tv_Que);
                  textViewA = itemView.findViewById(R.id.tvOpt_A);
                  textViewB = itemView.findViewById(R.id.tvOpt_B);
                  textViewC = itemView.findViewById(R.id.tvOpt_C);
                  textViewD = itemView.findViewById(R.id.tvOpt_D);
                  textViewAns = itemView.findViewById(R.id.tv_Ans);
          
              }
          }
          

          }

          然后当你绑定适配器之后,然后在使用下面的代码获取回收器视图点击事件获取之后..

           mRvData.setAdapter(productsAdapter);
              productsAdapter.setRecyclerViewClickListener(new ProductsAdapter.RecyclerViewClickListener() {
                  @Override
                  public void recyclerViewListClicked(View v, int position) {
                      Toast.makeText(getApplicationContext(),"Position to Click:"+position,Toast.LENGTH_SHORT).show();
                  }
              });
          

          【讨论】:

          • 什么也没发生。我处于同样的情况 - 没有吐司,没有崩溃
          猜你喜欢
          • 1970-01-01
          • 2015-03-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-03-21
          • 2021-10-27
          • 1970-01-01
          相关资源
          最近更新 更多