如何根据标题打开RecyclerView中的项目 - MYSQL数据库

问题描述 投票:0回答:1

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

//主要活动

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);
java php android mysql android-recyclerview
1个回答
0
投票

您可以通过创建回调接口类来设置Click事件,您可以将其传递给她的适配器构造函数然后用作适配器成员,然后在向视图持有者构造函数创建视图持有者时传递该回调,然后通过实现视图将click listener设置为item .OnClickListener并将click设置为item然后当onClick Called调用我们创建的回调时,这有点混乱,但你可以按照this

© www.soinside.com 2019 - 2024. All rights reserved.