无法将数据发送到 recyclerview
Not able to send data to recyclerview
我有一个关于如何显示我使用 post 请求使用改造从服务器获取的数据的问题。这是我的 json 数据,我想在其中显示回收站视图中的“配置文件”。我正在向服务器发送一个字符串以获取数据。
{
"status": 200,
"profile": {
"firstName": "nelli",
"lastName": "jhilmil",
"gender": "Female",
"addressLine": "Club",
"city": "Delhi",
"pincode": "560100",
"phoneNumber": "2423232344",
"userId": 50,
"createdByAdmin": false
}
}
这是回复json
public class ProfileResponse {
@SerializedName("status")
@Expose
private Status status;
@SerializedName("profile")
@Expose
private Profile profile;
getters and setters...
下面是子模型class。
public class Profile {
@PrimaryKey
@SerializedName("userId")
@Expose
private Integer userId;
@SerializedName("firstName")
@ColumnInfo(name = "firstName")
private String firstName;
@SerializedName("surname")
@Expose
@ColumnInfo(name = "surname")
private String surname;
@SerializedName("gender")
@Expose
@ColumnInfo(name = "gender")
private String gender;
@SerializedName("phoneNumber")
@Expose
@ColumnInfo(name = "phoneNumber")
private String phoneNumber;
@SerializedName("addressLine")
@Expose
@ColumnInfo(name = "addressLine")
private String addressLine1;
@SerializedName("city")
@Expose
@ColumnInfo(name = "city")
private String city;
@SerializedName("pincode")
@Expose
@ColumnInfo(name = "pincode")
private String pincode;
@SerializedName("createbyAdmin")
@Expose
@ColumnInfo(name = "createbyAdmin")
private Boolean createdByAdmin;
getters and setters....
下面是使用 post 方法向服务器发送字符串以获取数据的调用
@POST("user/getProfile")
Call<ProfileResponse> getData(@Body JsonObject jsonObject);
下面是适配器代码,我不知道要将什么作为列表发送,作为数组列表发送,以便将数据发送到适配器
public class ProfileAdapter extends RecyclerView.Adapter<ProfileAdapter.ProfileViewHolder> {
private Context context;
private List<ProfileResponse> profiles;
public ProfileAdapter(Context context, List<ProfileResponse> profiles) {
this.context = context;
this.profiles = profiles;
}
@Override
public ProfileViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.get_profile_items, parent, false);
return new ProfileViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ProfileViewHolder holder, int position) {
这是我从服务器发送和获取数据的改装调用
jsonObj.addProperty("role", String.valueOf(ADMIN));
Call<ProfileResponse> profResponse = AppAuthClient.geVuAPIServices().getData(jsonObj);
profResponse.enqueue(new Callback<ProfileResponse>() {
@Override
public void onResponse(Call<ProfileResponse>call, retrofit2.Response<ProfileResponse>response) {
// mProgressBar.setVisibility(View.GONE);
if(response.isSuccessful() && response.body() != null) {
???????????
Toast.makeText(GetProfile.this, "success", Toast.LENGTH_SHORT);
}
我正在从服务器获取数据,但无法将获取的数据从服务器发送到 recyclerview。非常感谢任何帮助,谢谢。
更新问题
这就是我在 activity
中初始化适配器的方式
getData();
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
}
你在哪里设置适配器到 recyclerview 这样的:
recyclerView.setAdapter(adapter);
我想你忘记设置适配器了,如果你这样做的话,
你说你从服务器获得数据但不能发送到 recyclerview 对吧!!那么你可以这样做:
// Get the response from response.body() and save it in whatever your //response is typed(like your POJO class profileResponse)
//之后你可以将这个profileResponse添加到arrayList
ArrayList<profileResponse> profileResponseList = new ArrayList();
if(response.isSuccessful() && response.body() != null) {
profileResponse = response.body();
profileResponseList.add(profileResponse);
Toast.makeText(GetProfile.this, "success", Toast.LENGTH_SHORT);
}
然后添加可以通知适配器将数据反映到 recyclerview 的结尾
adapter.notifyDataSetChanged();
简单并将此数据处理到适配器中 class
首先,您有适配器 (ProfileAdapter.class) 可以在 RecyclerView 中显示多个 ProfileResponse 对象,但您的 call/request 中只有一个 ProfileResponse。
这可能不是显示用户个人资料的最佳方式,但如果您确实需要,请遵循此
将此方法添加到 ProfileAdapter.class
public void swapData(List<?> newItems) {
if (newItems != null) {
this.profiles = newItems;
notifyDataSetChanged();
}
}
替换您编辑过的代码:
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));
adapter = new ProfileAdapter(getApplicationContext(), new ArrayList<>()); //TODO create filed ProfileAdapter adapter
recyclerView.setAdapter(adapter);
getData();
并在 onResponse 方法中交换数据
@Override
public void onResponse(Call<ProfileResponse>call, retrofit2.Response<ProfileResponse>response) {
// mProgressBar.setVisibility(View.GONE);
if(response.isSuccessful() && response.body() != null) {
//TODO adapter.swapDate(response.body / ProfileResponse)
Toast.makeText(GetProfile.this, "success", Toast.LENGTH_SHORT);
}
}
最好为没有RecyclerView的用户配置文件创建一个特殊视图并更新它。如果您想在列表中显示更多个人资料,请随时在下面发表评论...
我有一个关于如何显示我使用 post 请求使用改造从服务器获取的数据的问题。这是我的 json 数据,我想在其中显示回收站视图中的“配置文件”。我正在向服务器发送一个字符串以获取数据。
{
"status": 200,
"profile": {
"firstName": "nelli",
"lastName": "jhilmil",
"gender": "Female",
"addressLine": "Club",
"city": "Delhi",
"pincode": "560100",
"phoneNumber": "2423232344",
"userId": 50,
"createdByAdmin": false
}
}
这是回复json
public class ProfileResponse {
@SerializedName("status")
@Expose
private Status status;
@SerializedName("profile")
@Expose
private Profile profile;
getters and setters...
下面是子模型class。
public class Profile {
@PrimaryKey
@SerializedName("userId")
@Expose
private Integer userId;
@SerializedName("firstName")
@ColumnInfo(name = "firstName")
private String firstName;
@SerializedName("surname")
@Expose
@ColumnInfo(name = "surname")
private String surname;
@SerializedName("gender")
@Expose
@ColumnInfo(name = "gender")
private String gender;
@SerializedName("phoneNumber")
@Expose
@ColumnInfo(name = "phoneNumber")
private String phoneNumber;
@SerializedName("addressLine")
@Expose
@ColumnInfo(name = "addressLine")
private String addressLine1;
@SerializedName("city")
@Expose
@ColumnInfo(name = "city")
private String city;
@SerializedName("pincode")
@Expose
@ColumnInfo(name = "pincode")
private String pincode;
@SerializedName("createbyAdmin")
@Expose
@ColumnInfo(name = "createbyAdmin")
private Boolean createdByAdmin;
getters and setters....
下面是使用 post 方法向服务器发送字符串以获取数据的调用
@POST("user/getProfile")
Call<ProfileResponse> getData(@Body JsonObject jsonObject);
下面是适配器代码,我不知道要将什么作为列表发送,作为数组列表发送,以便将数据发送到适配器
public class ProfileAdapter extends RecyclerView.Adapter<ProfileAdapter.ProfileViewHolder> {
private Context context;
private List<ProfileResponse> profiles;
public ProfileAdapter(Context context, List<ProfileResponse> profiles) {
this.context = context;
this.profiles = profiles;
}
@Override
public ProfileViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.get_profile_items, parent, false);
return new ProfileViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ProfileViewHolder holder, int position) {
这是我从服务器发送和获取数据的改装调用
jsonObj.addProperty("role", String.valueOf(ADMIN));
Call<ProfileResponse> profResponse = AppAuthClient.geVuAPIServices().getData(jsonObj);
profResponse.enqueue(new Callback<ProfileResponse>() {
@Override
public void onResponse(Call<ProfileResponse>call, retrofit2.Response<ProfileResponse>response) {
// mProgressBar.setVisibility(View.GONE);
if(response.isSuccessful() && response.body() != null) {
???????????
Toast.makeText(GetProfile.this, "success", Toast.LENGTH_SHORT);
}
我正在从服务器获取数据,但无法将获取的数据从服务器发送到 recyclerview。非常感谢任何帮助,谢谢。
更新问题
这就是我在 activity
中初始化适配器的方式 getData();
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
}
你在哪里设置适配器到 recyclerview 这样的:
recyclerView.setAdapter(adapter);
我想你忘记设置适配器了,如果你这样做的话, 你说你从服务器获得数据但不能发送到 recyclerview 对吧!!那么你可以这样做:
// Get the response from response.body() and save it in whatever your //response is typed(like your POJO class profileResponse)
//之后你可以将这个profileResponse添加到arrayList
ArrayList<profileResponse> profileResponseList = new ArrayList();
if(response.isSuccessful() && response.body() != null) {
profileResponse = response.body();
profileResponseList.add(profileResponse);
Toast.makeText(GetProfile.this, "success", Toast.LENGTH_SHORT);
}
然后添加可以通知适配器将数据反映到 recyclerview 的结尾
adapter.notifyDataSetChanged();
简单并将此数据处理到适配器中 class
首先,您有适配器 (ProfileAdapter.class) 可以在 RecyclerView 中显示多个 ProfileResponse 对象,但您的 call/request 中只有一个 ProfileResponse。 这可能不是显示用户个人资料的最佳方式,但如果您确实需要,请遵循此
将此方法添加到 ProfileAdapter.class
public void swapData(List<?> newItems) {
if (newItems != null) {
this.profiles = newItems;
notifyDataSetChanged();
}
}
替换您编辑过的代码:
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));
adapter = new ProfileAdapter(getApplicationContext(), new ArrayList<>()); //TODO create filed ProfileAdapter adapter
recyclerView.setAdapter(adapter);
getData();
并在 onResponse 方法中交换数据
@Override
public void onResponse(Call<ProfileResponse>call, retrofit2.Response<ProfileResponse>response) {
// mProgressBar.setVisibility(View.GONE);
if(response.isSuccessful() && response.body() != null) {
//TODO adapter.swapDate(response.body / ProfileResponse)
Toast.makeText(GetProfile.this, "success", Toast.LENGTH_SHORT);
}
}
最好为没有RecyclerView的用户配置文件创建一个特殊视图并更新它。如果您想在列表中显示更多个人资料,请随时在下面发表评论...