gpt4 book ai didi

java - 如何根据标题从RecyclerView打开一个项目到另一个项目 - MYSQL 数据库

转载 作者:行者123 更新时间:2023-11-29 16:04:00 25 4
gpt4 key购买 nike

我将 MySQL 数据库中的所有数据列出到 RecyclerView。最近RecyclerView无法点击,只能显示全部列表。因此,我想知道将 RecyclerView 中的所选项目重定向到另一个 Activity 的代码。例如,在RecyclerView中,有一个“标题1、标题2、标题3”的列表。当我点击“Title 1”时,它将重定向到另一个包含“Title 1”中所有内容的 Activity 。下面是我的代码:

//主要 Activity

private static final String URL_PRODUCTS = "http://10.0.2.2/listview/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() {

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("name"),
product.getString("time")
));
}

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

//产品适配器

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

holder.tvName.setText(product.getTitle());
holder.tvTitle.setText(product.getName());
holder.tvTime.setText(product.getTime());
}

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

class ProductViewHolder extends RecyclerView.ViewHolder {

TextView tvName, tvTitle, tvTime;

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

tvName = itemView.findViewById(R.id.tvName);
tvTitle = itemView.findViewById(R.id.tvTitle);
tvTime = itemView.findViewById(R.id.tvTime);
}
}

//产品

public Product(int id, String title, String name, String time) {
this.id = id;
this.title = title;
this.name = name;
this.time = time;

}

public int getId() {
return id;
}

public String getTitle() {
return title;
}

public String getName() {
return name;
}

public String getTime() {
return time;
}

//PHP

//database constants
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'listview');

//connecting to database and getting the connection object
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

//Checking if any error occured while connecting
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die();
}

//creating a query
$stmt = $conn->prepare("SELECT id, title, name, time FROM products;");

//executing the query
$stmt->execute();

//binding results to the query
$stmt->bind_result($id, $title, $name, $time);

$products = array();

//traversing through all the result
while($stmt->fetch()){
$temp = array();
$temp['id'] = $id;
$temp['title'] = $title;
$temp['name'] = $name;
$temp['time'] = $time;
array_push($products, $temp);
}

//displaying the result in json format
echo json_encode($products);

最佳答案

您可以通过为回调创建一个接口(interface)类来设置单击事件,您可以传递她的适配器构造函数,然后将其用作适配器成员,然后在创建 View 持有者时将该回调传递给 View 持有者构造函数,然后将单击监听器设置为项目实现 View.OnClickListener 并将单击设置为项目,然后当 onClick 调用调用我们创建的回调时,这有点困惑,但您可以按照this进行操作

关于java - 如何根据标题从RecyclerView打开一个项目到另一个项目 - MYSQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55913472/

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