如何使用Recycle视图从MySQL获取数据

时间:2015-05-07 02:06:10

标签: php android mysql sql json

我正在尝试使用Recycle View而不是List View来从mysql获取数据,我已经看到了如何从字符串数组创建循环视图,如:

`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"}
            ],``

1 个答案:

答案 0 :(得分:0)

您的问题标题与问题的发展不同。 如果需要使用MySql中的数据填充循环视图,则需要在适配器中添加一些额外的代码。实际上它非常简单。我正在输入一些片段让你上路。希望它有所帮助。

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