Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
141 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);
}
question from:https://stackoverflow.com/questions/65855560/not-able-to-send-data-to-recyclerview

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

1 Answer

0 votes
by (71.8m points)

First of all you have adapter (ProfileAdapter.class) to show multiple ProfileResponse objects in your RecyclerView but you have only one ProfileResponse in your call/request. This may not be the best way to show user profile but if you really want, follow this

Add this method to ProfileAdapter.class

public void swapData(List<?> newItems) {
        if (newItems != null) {
            this.profiles = newItems;
            notifyDataSetChanged();
        }
    }

Replace your code from edited:

recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));
adapter  = new ProfileAdapter(getApplicationContext(), new ArrayList<>()); //TODO create filed ProfileAdapter adapter
recyclerView.setAdapter(adapter);

getData();

And swapData in onResponse method

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

It would be better to create a special view for the user profile without RecyclerView and update it. If you want to show more profiles in the list feel free to comment below...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...