android - 如何从 RecyclerVIew 中的 TextView 获取文本?

标签 android android-recyclerview

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

这是我的适配器-

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

这是我的 Activity -

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

最佳答案

代替 this.getLayoutPosition() 使用 getAdapterPosition();

关于android - 如何从 RecyclerVIew 中的 TextView 获取文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49505625/

相关文章:

Android Recyclerview Remove Animation 应用于所有项目奇怪的行为

android - RecyclerView onBindViewHolder 导致严重的性能问题

android - AsyncTask 和进度条

java - 单击 TextView 后 Android 应用程序停止工作

android - 寻找 OpenCV android 教程

android - 在 android config.xml 中出现错误 "unbound prefix"

Android Picasso Recyclerview 图片下载速度慢

android - RecyclerView ambiguos setVisibility函数,点击一个 View 影响多个 View

android - recyclerview 中的重叠位置/ View

android - Phonegap Android 权限、config.xml 和插件