Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.2k views
in Technique[技术] by (71.8m points)

android - Not able to send data to recyclerview

I have a problem on how to display data i m getting from server using post request using retrofit. Here is my json data in which I want to display the "profile" in the recycler view. i m sending a string to server to get the data.

{
    "status": 200,
    "profile": {
        "firstName": "nelli",
        "lastName": "jhilmil",
        "gender": "Female",
        "addressLine": "Club",
        "city": "Delhi",
        "pincode": "560100",
        "phoneNumber": "2423232344",
        "userId": 50,
        "createdByAdmin": false
    }
}

This is the response json

public class ProfileResponse {

    @SerializedName("status")
    @Expose
    private Status status;

    @SerializedName("profile")
    @Expose
    private Profile profile;
    getters and setters...

Below is child model 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....

Below is the call wherein using post method sending a string to server to get the data

@POST("user/getProfile")
Call<ProfileResponse> getData(@Body JsonObject jsonObject);

Below is Adapter code where i have no idea what to send as a list to send as arraylist to send data to adapter

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) {

This is my retrofit call to send and get data from server

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

I am getting the data from server but not able to send the fetched data from server to recyclerview. Would highly appreciate any help, thank you.

Updated question

this is how i initialized adapter in my activity

  getData();
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false);
    recyclerView.setLayoutManager(linearLayoutManager);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Where did you set the adapter to the recyclerview like this:

recyclerView.setAdapter(adapter);

I think you forgot to set the adapter,if you do so then, you said that you got the data from the server but not able to send to the recyclerview right!! then you can do this :

  // Get the response from response.body() and save it in whatever your //response is typed(like your POJO class profileResponse)

//after that you can add this profileResponse into 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);
               }

then add the end you can notify adapter to reflect that data onto recyclerview

    adapter.notifyDataSetChanged();

simple and handle this data into adapter class


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...