【问题标题】:How to open an item from RecyclerView to another according to its title - MYSQL Database如何根据标题从 RecyclerView 打开一个项目到另一个项目 - MYSQL 数据库
【发布时间】:2019-04-30 03:11:16
【问题描述】:

我将 MySQL 数据库中的所有数据都列出到了 RecyclerView。最近 RecyclerView 无法点击,只能显示所有列表。因此,我想知道将 RecyclerView 中的选定项目重定向到另一个活动的代码。例如,在 RecyclerView 中,有一个“标题 1,标题 2,标题 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】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-01
      • 1970-01-01
      • 2016-04-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多