通过GSON将数据放入RecyclerView

时间:2016-12-22 11:33:17

标签: android json android-recyclerview gson

我想将从服务器获取的数据放到RecyclerView中,但RecyclerView中没有显示任何内容。

这是我的JSON结构:

rmagick

首先,我生成了POJO类(带https://stackoverflow.com/a/41282162/1975112),产生了以下类:

Userlist.java

{
"userlist": [{
    "user": {
        "firstname": "Marcelina",
        "city": "North Alvera",
        "id": {
            "low": 35392,
            "high": 0
        },
        "lastname": "Ernser"
    }
}, {
    "user": {
        "firstname": "Laury",
        "city": "South Lottie",
        "id": {
            "low": 51009,
            "high": 0
        },
        "lastname": "McClure"
    }
}]}

User.java

public class Userlist {

@SerializedName("user")
@Expose
private User user;

public Userlist(User user) {
    this.user = user;
}

public User getUser() {
    return user;
}

public void setUser(User user) {
    this.user = user;
}}

Id.java

public class User {

@SerializedName("firstname")
@Expose
private String firstname;
@SerializedName("city")
@Expose
private String city;
@SerializedName("id")
@Expose
private Id id;
@SerializedName("lastname")
@Expose
private String lastname;

public User(String firstname, String city, Id id, String lastname) {
    this.firstname = firstname;
    this.city = city;
    this.id = id;
    this.lastname = lastname;
}

public String getFirstname() {
    return firstname;
}

public void setFirstname(String firstname) {
    this.firstname = firstname;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}

public Id getId() {
    return id;
}

public void setId(Id id) {
    this.id = id;
}

public String getLastname() {
    return lastname;
}

public void setLastname(String lastname) {
    this.lastname = lastname;
}}

最后,这是我的MainActivity:

public class Id {

@SerializedName("low")
@Expose
private int low;
@SerializedName("high")
@Expose
private int high;

public Id(int low, int high) {
    this.low = low;
    this.high = high;
}

public int getLow() {
    return low;
}

public void setLow(int low) {
    this.low = low;
}

public int getHigh() {
    return high;
}

public void setHigh(int high) {
    this.high = high;
}}

现在的问题是如何以正确的方式处理获取的数据。我已经阅读了很多关于AsyncTask的信息,但哪种解决方案最适合获取json数据,用GSON解析并将其放入RecyclerView?!

先谢谢你们。

干杯安迪

2 个答案:

答案 0 :(得分:0)

您正在尝试在将阵列用户列表附加到recyclerView适配器中之前通知适配器更改。

通过创建Recyclerview实例来更正代码,然后将userList附加回RecyclerAdapter实例,然后再通知对Recycler适配器的更改。

如下所示

        userList = Arrays.asList(gson.fromJson(responseStr, Userlist.class));
        /*As shown here*/
        adapter = new RecyclerViewAdapter(your_activity.this, userList);

        System.out.print(responseStr);
        response.body().close();
        adapter.notifyDataSetChanged();

答案 1 :(得分:0)

尝试这样的事情

   public class MainActivity extends AppCompatActivity {


  private ArrayList<Userlist> userList;
  private RecyclerView recyclerView;
  private RecyclerViewAdapter adapter;
  private RecyclerView.LayoutManager layoutManager;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_layout);
    recyclerView = (RecyclerView) findViewById(R.id.rv_user);

     recyclerView.setHasFixedSize(true);
     LinearLayoutManager layoutManager = new 
     LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, 
     false);
    recyclerView.setLayoutManager(layoutManager);

  ButterKnife.bind(this);
  getAllUsers();
 }


 private void getAllUsers() {

    OkHttpClient client = new OkHttpClient();

     Request request = new Request.Builder()
        .url(BASE_URL + "user")
        .build();

     client.newCall(request).enqueue(new Callback() {
    @Override
    public void onFailure(Call call, IOException e) {
        e.printStackTrace();
    }
    @Override
    public void onResponse(Call call, Response response) throws IOException {
        String responseStr = response.body().string();
        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson = gsonBuilder.create();
        //List<Userlist> userList = new ArrayList<>();
         Userlist userlist = gson.fromJson(String.valueOf(responseStr),Userlist.class));
        userList.add(userlist);
        adapter = new RecyclerViewAdapter(userList);
        recyclerView.setAdapter(adapter);
        System.out.print(responseStr);
        response.body().close();
        adapter.notifyDataSetChanged();
       } 
     });
      }}