gpt4 book ai didi

php - 如何使用 Recyclerview 从 MySQL 获取数据

转载 作者:行者123 更新时间:2023-11-29 22:27:56 26 4
gpt4 key购买 nike

我正在尝试使用回收 View 而不是 ListView 从 mysql 获取数据,我已经看到了如何从字符串数组创建回收 View ,例如:

`String[] models = new String[]{"Gallardo", "Vyron", "Corvette", "Pagani Zonda", "Porsche 911 Carrera", "BMW 720i", "DB77", "Mustang", "Camaro", "CT6"};` 

但我不知道如何从 php 加载:

`{"db_evento":[{"id":"3","nome":"Almo\u00e7o na casa de isa","local":"Navegantes, valthier apto 301","valor":"0.00","categoria":"Familiar ","descricao":"Toda a fam\u00edlia"},{"id":"6","nome":"Above","local":"Chevrolet ","valor":"150.00","categoria":"Shows","descricao":"Above and beyond no Chevrolet hall, as 22h dia 05\/06\/2014"},{"id":"7","nome":"Passeio de catamaram ","local":"Porto","valor":"15.00","categoria":"Passeio ","descricao":"Passeio de catamaram por recife "},{"id":"8","nome":"Corrida","local":"Ca\u00e7ad\u00e3o boa viagem ","valor":"0.00","categoria":"Passeio ","descricao":"Corrida de Cooper no calcadao de boa viagem as 7h"}],"success":1}`

或者来自 json,例如:

``{"orders":[
{"id":"681","item":"Pizza"},
{"id":"690","item":"Pasta"},
{"id":"701","item":"Coke"},
{"id":"710","item":"Burger"},
{"id":"720","item":"Finger-Chips"},
{"id":"730","item":"Noodles"}
],``

最佳答案

您的问题标题与问题的发展有所不同。如果您需要使用 MySql 中的数据填充回收 View ,您将需要向适配器添加一些额外的代码。实际上很简单。我正在输入一些 fragment 来帮助您继续。希望对您有所帮助。

class MyAdapter extends  RecyclerView.Adapter<RecyclerView.ViewHolder> {

private Context context;
private Cursor cursor; // Your data
public MyAdapter( Context context, Cursor cursor) {
this.context = context;
this.cursor = cursor;
}

public Cursor swapCursor(Cursor cursor) {
if (this.cursor == cursor) {
return null;
}
Cursor oldCursor = this.cursor;
this.cursor = cursor;
if (cursor != null) {
this.notifyDataSetChanged();
}
return oldCursor;
}
public void changeCursor(Cursor cursor) {
Cursor old = swapCursor(cursor);
if (old != null) {
old.close();
}
}

关于php - 如何使用 Recyclerview 从 MySQL 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30090738/

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