Aditya Patel
Aditya Patel

Reputation: 73

How to get text from TextView in RecyclerVIew?

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.

Here is my Adapter-

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

   }
  }
}

Here is my Interface-

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

Here is my Model-

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

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

Upvotes: 1

Views: 2967

Answers (5)

user4571931
user4571931

Reputation:

make some change in code take interface inside adapter class and define object add access it. i do some changes in adapter class used below code ..

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

    }
}

}

then after when you bind adapter then after used below code for getting recycler view click event getting..

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

Upvotes: 0

Vikas singh
Vikas singh

Reputation: 3889

Your RecyclerViewClickListener listener is not initialize in your activty

So you need to initialize your listener in your onCreate method of the QuestionList activity like this

 RecyclerViewClickListener listener;

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

And also you should use getAdapterPosition(); Instead of this.getLayoutPosition() as Saurabh Said

Upvotes: 0

Ionut J. Bejan
Ionut J. Bejan

Reputation: 754

Let's show you the way I do it, but first of all I think you'll have to change this.getLayoutPosition() with 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);
}

Upvotes: 0

Navneet Krishna
Navneet Krishna

Reputation: 5017

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

Upvotes: 0

Saurabh Vadhva
Saurabh Vadhva

Reputation: 511

Instead of this.getLayoutPosition() use getAdapterPosition();

Upvotes: 1

Related Questions