我使用Volley将列表从MySQL数据库加载到Recycler视图。列表正在顺利进入回收站视图,没有任何问题。但现在我想从我的recyclervie列表中读取问题。我已经为我的活动实现了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;
}
}
这是我的活动 -
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();
}
}
答案 0 :(得分:1)
而不是this.getLayoutPosition()
使用getAdapterPosition();
答案 1 :(得分:0)
尝试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);
}
});
}
答案 2 :(得分:0)
让我们按照我的方式向您展示,但首先我认为您必须使用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);
}
答案 3 :(得分:0)
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 Said
答案 4 :(得分:0)
在代码中对接口内部的接口进行一些更改并定义对象添加访问权限。我在代码下面使用的适配器类做了一些更改。
var d = new Date('2018-03-31');
d.setMonth(d.getMonth() + 1, 1);
dt = new Date(d);
document.getElementById("demo").innerHTML = dt;
}
然后在绑定适配器之后,然后使用下面的代码获取回收器视图单击事件获取..
<p id="demo"></p>