如何在RecyclerVIew中从TextView获取文本? [英] How to get text from TextView in RecyclerVIew?

查看:86
本文介绍了如何在RecyclerVIew中从TextView获取文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am loading list from MySQL Database to Recycler view using Volley. List is coming to recycler view very smoothly without any problem. But now I want to read question from my recyclervie list. I have implemented Interface to my activity. But when I click on list, it shows nothing. Please help me to do my task. App is showing no error, it just do nothing on list click.

这是我的适配器-

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

推荐答案

使用 getAdapterPosition();

这篇关于如何在RecyclerVIew中从TextView获取文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆