gpt4 book ai didi

php - android 从 mysql 中检索数据并将其呈现在 recycleview 中并添加事件

转载 作者:行者123 更新时间:2023-11-30 21:46:06 25 4
gpt4 key购买 nike

我已经从 android 中的 mysql 检索数据并将其呈现在回收 View 中。现在我想添加 onclick 监听器以检索信息。代码可在此处获得 https://www.simplifiedcoding.net/retrieve-data-mysql-database-android/

现在我想将 onclick 监听器添加到检索到的信息中。我使用 volley。

public class Product {
private int id;
private String title;
private String shortdesc;
private double rating;
private double price;
private String image;

public Product(int id, String title, String shortdesc, double rating, double price, String image) {
this.id = id;
this.title = title;
this.shortdesc = shortdesc;
this.rating = rating;
this.price = price;
this.image = image;
}

public int getId() {
return id;
}

public String getTitle() {
return title;
}

public String getShortdesc() {
return shortdesc;
}

public double getRating() {
return rating;
}

public double getPrice() {
return price;
}

public String getImage() {
return image;
}
}

产品适配器

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

private Context mCtx;
private List<Product> productList;

public ProductsAdapter(Context mCtx, List<Product> productList) {
this.mCtx = mCtx;
this.productList = productList;
}

@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) {
Product product = productList.get(position);

//loading the image
Glide.with(mCtx)
.load(product.getImage())
.into(holder.imageView);

holder.textViewTitle.setText(product.getTitle());
holder.textViewShortDesc.setText(product.getShortdesc());
holder.textViewRating.setText(String.valueOf(product.getRating()));
holder.textViewPrice.setText(String.valueOf(product.getPrice()));
}

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

class ProductViewHolder extends RecyclerView.ViewHolder {

TextView textViewTitle, textViewShortDesc, textViewRating, textViewPrice;
ImageView imageView;

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

textViewTitle = itemView.findViewById(R.id.textViewTitle);
textViewShortDesc = itemView.findViewById(R.id.textViewShortDesc);
textViewRating = itemView.findViewById(R.id.textViewRating);
textViewPrice = itemView.findViewById(R.id.textViewPrice);
imageView = itemView.findViewById(R.id.imageView);
}
}
}

主要 Activity

public class MainActivity extends AppCompatActivity {

//this is the JSON Data URL
//make sure you are using the correct ip else it will not work
private static final String URL_PRODUCTS = "http://192.168.101.1/MyApi/Api.php";

//a list to store all the products
List<Product> productList;

//the recyclerview
RecyclerView recyclerView;


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

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

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

//this method will fetch and parse json
//to display it in recyclerview
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
productList.add(new Product(
product.getInt("id"),
product.getString("title"),
product.getString("shortdesc"),
product.getDouble("rating"),
product.getDouble("price"),
product.getString("image")
));
}

//creating adapter object and setting it to recyclerview
ProductsAdapter adapter = new ProductsAdapter(MainActivity.this, productList);
recyclerView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

}
});

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

JSON

[{"id":1,"title":"abc","shortdesc":"dis","rating":1,"price":1,"image":"aa"},
{"id":2,"title":"def","shortdesc":"abc","rating":3,"price":1000,"image":"abc"}]

最佳答案

只需用这个替换你的 ProductViewHolder 类......它工作正常

class ProductViewHolder extends RecyclerView.ViewHolder {


TextView textViewTitle, textViewShortDesc, textViewRating, textViewPrice;
ImageView imageView;

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

textViewTitle = itemView.findViewById(R.id.textViewTitle);
textViewShortDesc = itemView.findViewById(R.id.textViewShortDesc);
textViewRating = itemView.findViewById(R.id.textViewRating);
textViewPrice = itemView.findViewById(R.id.textViewPrice);
imageView = itemView.findViewById(R.id.imageView);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mCtx, YourActivity.class);
mCtx.startActivity(intent);
}
});
}
}

关于php - android 从 mysql 中检索数据并将其呈现在 recycleview 中并添加事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49422434/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com