im a newbie, i have a problem in my recyclerview. I cant fetch my data from my api. ive just took this tutorial since it is the same implementation form my app. http://shaoniiuc.com/android/fetch-json-api-data-recyclerview-using-retrofit-library/, Maybe my implementation is wrong here, can you please help me to solve, thank you :)
This is my api:
public interface Api {
#GET("api/Database/GetSchedule?serialNumber=1807200003")
Call<List<ScheduleDetails>> getSchedData();
}
My DetailObjects:
public class ScheduleDetails {
private String ID;
private String Location;
private String Latitude;
private String Longitude;
private String SequenceNumber;
private String Comment;
private String hasArrived;
private String ActualArrival;
private String hasDepart;
private String ActualDeparture;
private String Status;
private String ID_FleetSchedule;
private String ExpectedTimeOfTransaction;
private String EstimatedTimeOfDeparture;
private String ID_FleetCar;
private String Scheduler;
private String Car;
private String ID_FleetCompany;
private String FleetCompany;
private String ID_FleetDevice;
private String SerialNumber;
//Constructor....
//Getter.....
}
note: in my api, these are the string ive get but
ive only getting least of them... you can check
it on my adapterView.
my adapterview:
public class scheduleAdapter extends
RecyclerView.Adapter<scheduleAdapter.MyViewHolder> {
private List<ScheduleDetails> schedlist;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView location,schedule_status,dateTime,IDFleet,iid;
public MyViewHolder(View view) {
super(view);
location=view.findViewById(R.id.schedule_location);
dateTime=view.findViewById(R.id.schedule_dateTime);
schedule_status=view.findViewById(R.id.schedule_Status);
IDFleet=view.findViewById(R.id.schedule_FleetID);
iid=view.findViewById(R.id.schedule_ID);
}
}
public scheduleAdapter(List<ScheduleDetails> scheduleList) {
this.schedlist = scheduleList;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.schedule_cardview, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int
position) {
ScheduleDetails sched = schedlist.get(position);
holder.location.setText(sched.getLocation());
holder.schedule_status.setText(sched.getStatus());
holder.dateTime.setText(sched.getExpectedTimeOfTransaction());
holder.IDFleet.setText(sched.getID_FleetSchedule());
holder.iid.setText(sched.getID());
}
#Override
public int getItemCount() {
return schedlist.size();
}
}
this is my API:
public class ApiClient {
private static final String BASE_URL = "http://www.example.com";
private static Retrofit retrofit = null;
public static Retrofit getRetrofit() {
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
this is my getList via recyclerview on my main activity:
public class ScheduleFleet extends Fragment {
View inflatedView = null;
private RecyclerView recyclerView;
private scheduleAdapter mAdapter;
private LinearLayoutManager layoutManager;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
inflatedView = inflater.inflate(R.layout.fragment_schedule_fleet, container, false);
return inflatedView;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getActivity().setTitle("Schedule");
final SwipeRefreshLayout refresh = inflatedView.findViewById(R.id.sched_refresh);
getUserList();
refresh.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
doYourUpdate();
}
private void doYourUpdate() {
recyclerView.setAdapter(mAdapter);
getUserList();
refresh.setRefreshing(false);
}
}
);
}
private void getUserList() {
try {
Api service = ApiClient.getRetrofit().create(Api.class);
Call<List<ScheduleDetails>> call = service.getSchedData();
call.enqueue(new Callback<List<ScheduleDetails>>() {
#Override
public void onResponse(Call<List<ScheduleDetails>> call, Response<List<ScheduleDetails>> response) {
List<ScheduleDetails> userList = response.body();
layoutManager = new LinearLayoutManager(getContext());
RecyclerView recyclerView =inflatedView.
findViewById(R.id.scheduleRecycler);
recyclerView.setLayoutManager(layoutManager);
scheduleAdapter recyclerViewAdapter =
new scheduleAdapter(userList);
recyclerView.setAdapter(recyclerViewAdapter);
}
#Override
public void onFailure(Call<List<ScheduleDetails>> call, Throwable t) {
}
});
}catch (Exception e) {}
}
}
note: ive try to use the debug tool.. it says that my api is message:OK also the response= true... but i havnt display the data on my recyclerview...
ill hope, you understand my question.. feel free to comment if i i have wrong in my question.. thanks folks.
this is my sample api get using insomnia:
Related
I am making a simple app in which the user will able to search for books by its name, with google.com book api. For now, I wish to present a list of books with android in their name. I am doing it with Retrofit2 and RecycleView, but nothing is showing.
MainActivity:
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
KnjigaAdapter knjigaAdapter;
List<KnjigaModel> listaKnjiga;
public static final String BASE_URL = "https://www.googleapis.com/books/";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uzmiKomentare();
}
public void uzmiKomentare() {
Gson gson = new GsonBuilder().serializeNulls().create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
KnjigaApi knjigaApi = retrofit.create(KnjigaApi.class);
final Call<KnjigaModel> pozivZaListuKnjiga = knjigaApi.getKnjige("android");
pozivZaListuKnjiga.enqueue(new Callback<KnjigaModel>() {
#Override
public void onResponse(Call<KnjigaModel> call, Response<KnjigaModel> response) {
if (!response.isSuccessful()) {
return;
}
//generateRecycleView(WHAT TO PUT HERE!!!!);
}
#Override
public void onFailure(Call<KnjigaModel> call, Throwable t) {
Log.d("MainActivity:", t.getMessage());
}
});
}
private void generateRecycleView(List<KnjigaModel> knjige) {
listaKnjiga = new ArrayList<>();
recyclerView = findViewById(R.id.recycleview);
knjigaAdapter = new KnjigaAdapter(this, knjige);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(knjigaAdapter);
if (knjigaAdapter.getItemCount() == 0){
Log.i("List is empty: ","YES");
}
else {
Log.i("list is empty: ","No");
}
}
}
api inteface:
public interface KnjigaApi {
#GET("v1/volumes")
Call<KnjigaModel> getKnjige(#Query("q") String knjiga);
}
model class:
public class KnjigaModel {
#SerializedName("title")
#Expose
private String imeKnjige;
#SerializedName("authors")
#Expose
private String imeAutora;
#SerializedName("thumbnail")
#Expose
private String slikaKnjige;
public KnjigaModel(String imeKnjige, String imeAutora,String slikaKnjige) {
this.imeKnjige = imeKnjige;
this.imeAutora = imeAutora;
this.slikaKnjige = slikaKnjige;
}
public String getImeKnjige() {
return imeKnjige;
}
public String getImeAutora() {
return imeAutora;
}
public String getSlikaKnjige() {
return slikaKnjige;
}
}
and my adapter:
public class KnjigaAdapter extends RecyclerView.Adapter<KnjigaAdapter.KomentariViewHolder> {
private List<KnjigaModel> listaKnjiga;
private LayoutInflater inflater;
private Context context;
public KnjigaAdapter(Context context, List<KnjigaModel> listaKnjiga) {
this.listaKnjiga = listaKnjiga;
this.context = context;
}
#Override
public KomentariViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
inflater = LayoutInflater.from(context);
// Inflate the custom layout
View postView = inflater.inflate(R.layout.single_item, parent, false);
// Return a new holder instance
return new KomentariViewHolder(postView);
}
#Override
public void onBindViewHolder(KomentariViewHolder holder, int position) {
KnjigaModel knjige = listaKnjiga.get(position);
holder.naslovKnjige.setText(knjige.getImeKnjige());
holder.imeAutora.setText(knjige.getImeAutora());
Glide.with(context)
.load(knjige.getSlikaKnjige())
.into(holder.slikaKnjige);
}
#Override
public int getItemCount() {
return listaKnjiga.size();
}
public class KomentariViewHolder extends RecyclerView.ViewHolder {
private TextView naslovKnjige;
private TextView imeAutora;
private ImageView slikaKnjige;
public KomentariViewHolder(View itemView) {
super(itemView);
naslovKnjige = itemView.findViewById(R.id.ime_knjige);
imeAutora = itemView.findViewById(R.id.autor_knjige);
slikaKnjige = itemView.findViewById(R.id.sika_korica);
}
}
}
my JSON format:
https://www.googleapis.com/books/v1/volumes?q=android
First of all, I strongly recommend to use RxJava for asynchronous requests, though it's totally optional.
Your Problem:
Your "getKnjige" Method returns only ONE Model, though the endpoint is named volumes (plural), you need to wrap your KnjigaModel in a class like
data class KnjigaResponse(val items: List<KnjigaModel>)
(Kotlin for simplicity, you can also generate a Java class with a single member volumes member that holds a list of KnjigaModel)
In addition, your model is wrong. authors is not a string, but a List of Strings, and "thumbnail" is wrapped in an "imageLinks" Object, and title is wrapped in a volumeInfo Object.
Your retrofit interface then would look like this:
public interface KnjigaApi {
#GET("v1/volumes")
Call<KnjigaResponse> getKnjige(#Query("q") String knjiga);
Request:
final Call<KnjigaResponse> pozivZaListuKnjiga = knjigaApi.getKnjige("android");
pozivZaListuKnjiga.enqueue(new Callback<KnjigaResponse>() {
#Override
public void onResponse(Call<KnjigaResponse> call, Response<KnjigaResponse> response) {
if (!response.isSuccessful()) {
return;
}
generateRecycleView(response.items);
}
#Override
public void onFailure(Call<KnjigaResponse> call, Throwable t) {
Log.d("MainActivity:", t.getMessage());
}
});
I am trying to make a call to this api and am having difficulty as the response.body() is returning null.
http://demo.museum.vebrary.vn/api/stuff/getall
I want to get stuff name of list and show to my recyclerview.
My model:
public class SOAnswersResponse {
#SerializedName("StuffModels")
#Expose
private List<StuffModel> stuffModels = null;
public List<StuffModel> getStuffModels() {
return stuffModels;
}
public void setStuffModels(List<StuffModel> stuffModels) {
this.stuffModels = stuffModels;
}
and
public class StuffModel {
#SerializedName("STUFFID")
#Expose
private Integer sTUFFID;
#SerializedName("STUFFCODE")
#Expose
private String sTUFFCODE;
#SerializedName("STUFFNAME")
#Expose
private String sTUFFNAME;
#SerializedName("STUFFNOTE")
#Expose
private String sTUFFNOTE;
#SerializedName("STUFFORDER")
#Expose
private Integer sTUFFORDER;
#SerializedName("CUSTOMERID")
#Expose
private String cUSTOMERID;
#SerializedName("EXHIBITS")
#Expose
private List<Object> eXHIBITS = null;
Json response
{
"StuffModels":[
{
"STUFFID":2,
"STUFFCODE":"Gi",
"STUFFNAME":"Giấy",
"STUFFNOTE":"",
"STUFFORDER":2,
"CUSTOMERID":"CAMAU",
"EXHIBITS":[
]
},
ApiUtils Class
public class ApiUtils {
private ApiUtils() {
}
public static final String BASE_URL = "http://demo.museum.vebrary.vn/api/";
public static SOService getSOService() {
return RetrofitClient.getClient(BASE_URL).create(SOService.class);
}
}
Service interface
public interface SOService {
#GET("/stuff/getall")
Call<SOAnswersResponse> getAnswers();
}
RetrofitClient Class
public class RetrofitClient {
private static Retrofit retrofit = null;
public static Retrofit getClient(String baseUrl) {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
My RecyclerView adapter
public class CategogyNameRecyclerViewAdapter extends RecyclerView.Adapter<CategogyNameRecyclerViewAdapter.ViewHolder> {
private List<StuffModel> mItems;
private Context mContext;
private PostItemListener mItemListener;
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView titleTv;
PostItemListener mItemListener;
public ViewHolder(View itemView, PostItemListener postItemListener) {
super(itemView);
titleTv = itemView.findViewById(R.id.tvListMenuCategogy);
this.mItemListener = postItemListener;
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
StuffModel item = getItem(getAdapterPosition());
this.mItemListener.onPostClick(item.getSTUFFID());
notifyDataSetChanged();
}
}
public CategogyNameRecyclerViewAdapter(Context context, List<StuffModel> posts, PostItemListener itemListener) {
mItems = posts;
mContext = context;
mItemListener = itemListener;
}
#Override
public CategogyNameRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View postView = inflater.inflate(R.layout.item_list_text, parent, false);
ViewHolder viewHolder = new ViewHolder(postView, this.mItemListener);
return viewHolder;
}
#Override
public void onBindViewHolder(CategogyNameRecyclerViewAdapter.ViewHolder holder, int position) {
StuffModel item = mItems.get(position);
TextView textView = holder.titleTv;
textView.setText(item.getSTUFFNAME());
}
#Override
public int getItemCount() {
return mItems.size();
}
public void updateAnswers(List<StuffModel> items) {
mItems = items;
notifyDataSetChanged();
}
private StuffModel getItem(int adapterPosition) {
return mItems.get(adapterPosition);
}
public interface PostItemListener {
void onPostClick(long id);
}
}
And my main activity
public class Testttt extends AppCompatActivity {
private CategogyNameRecyclerViewAdapter mAdapter;
private RecyclerView mRecyclerView;
private SOService mService;
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView(R.layout.test );
mService = ApiUtils.getSOService();
mRecyclerView = (RecyclerView) findViewById(R.id.rcvCategogyNameMenuTest);
mAdapter = new CategogyNameRecyclerViewAdapter(this, new ArrayList<StuffModel>(0), new CategogyNameRecyclerViewAdapter.PostItemListener() {
#Override
public void onPostClick(long id) {
Toast.makeText(Testttt.this, "Post id is" + id, Toast.LENGTH_SHORT).show();
}
});
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setHasFixedSize(true);
RecyclerView.ItemDecoration itemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST);
mRecyclerView.addItemDecoration(itemDecoration);
loadAnswers();
}
public void loadAnswers() {
mService.getAnswers().enqueue(new Callback<SOAnswersResponse>() {
#Override
public void onResponse(Call<SOAnswersResponse> call, Response<SOAnswersResponse> response) {
Toast.makeText(Testttt.this, "333333333333333333"+response.body(), Toast.LENGTH_SHORT).show();
if(response.isSuccessful()) {
mAdapter.updateAnswers(response.body().getStuffModels());
Log.d("AnswersPresenter", "posts loaded from API");
}else {
int statusCode = response.code();
}
}
#Override
public void onFailure(Call<SOAnswersResponse> call, Throwable t) {
showErrorMessage();
Log.d("AnswersPresenter", "error loading from API");
}
});
}
public void showErrorMessage() {
Toast.makeText(this, "Error loading posts", Toast.LENGTH_SHORT).show();
}
}
The first thing that came in my mind:
Your
public static final String BASE_URL = "http://demo.museum.vebrary.vn/api/";
has a "/" at the the end and your
#GET("/stuff/getall")
Call<SOAnswersResponse> getAnswers();
starts with a "/". So there is a double backslash in the url that might leads to the 404 code. Does this solve the problem?
When i call your URL i receive XML. Maybe the API is not configured correctly?
Change your Service interface
public interface SOService {
#GET("stuff/getall")
Call<SOAnswersResponse> getAnswers();
}
it occurred because you have use start with backslash it already added in your base url
please help me..
i have 10 policy data on my PHP to show on recyclerview android studio using retrofit..but, when i run my emulator, the emulator just show 1 data..my plan just show PolicyNo..when i running emulator and check my log, they've said : my Number Policy Received: 10
sorry for my bad english :(
here is my policyActivity :
public class PolicyActivity extends AppCompatActivity {
private static final String TAG = PolicyActivity.class.getSimpleName();
private String policyno;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_policy);
final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.policy_recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
ApiEndPoint apiEndPoint =
ApiClient.getClient(this).create(ApiEndPoint.class);
Call<PolicyStatus> call = apiEndPoint.getPolicyNo(policyno);
call.enqueue(new Callback<PolicyStatus>() {
#Override
public void onResponse(Call<PolicyStatus> call, Response<PolicyStatus> response) {
List<PolicyNo> policyNo = response.body().getPolicyNo();
Log.d(TAG, "Number Policy Received: " + policyNo.size());
recyclerView.setAdapter(new PolicyAdapter(policyNo, R.layout.list_item_policy, getApplicationContext()));
}
#Override
public void onFailure (Call <PolicyStatus> call, Throwable t){
Log.e(TAG, t.toString());
}
});
}
}
and this is my policyadapter:
public class PolicyAdapter extends RecyclerView.Adapter<PolicyAdapter.PolicyViewHolder> {
private List<PolicyNo> policyNo;
private int rowLayout;
private Context context;
public static class PolicyViewHolder extends RecyclerView.ViewHolder{
LinearLayout policyLayout;
TextView nomorpolis;
public PolicyViewHolder (View v){
super(v);
policyLayout = (LinearLayout) v.findViewById(R.id.policy_layout);
nomorpolis = (TextView)v.findViewById(R.id.nomorpolis);
}
}
public PolicyAdapter (List<PolicyNo>policyNo, int rowLayout, Context context){
this.policyNo = policyNo;
this.context = context;
this.rowLayout = rowLayout;
}
#Override
public PolicyAdapter.PolicyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View view= LayoutInflater.from(parent.getContext()).inflate(rowLayout, parent, false);
return new PolicyViewHolder(view);
}
#Override
public void onBindViewHolder (PolicyViewHolder holder, final int position){
holder.nomorpolis.setText(policyNo.get(position).getPolicyNo());
}
#Override
public int getItemCount() {return policyNo.size();}
}
PolicyStatus :
public class PolicyStatus {
#SerializedName("success")
#Expose
private Boolean success;
#SerializedName("PolicyNo")
#Expose
private List<PolicyNo> policyNo = null;
public Boolean getSuccess() {
return success;
}
public void setSuccess(Boolean success) {
this.success = success;
}
public List<PolicyNo> getPolicyNo() {
return policyNo;
}
public void setPolicyNo(List<PolicyNo> policyNo) {
this.policyNo = policyNo;
}
PolicyNo :
public class PolicyNo {
#SerializedName("ANO")
#Expose
private Integer aNO;
#SerializedName("PolicyNo")
#Expose
private String policyNo;
#SerializedName("ADate")
#Expose
private ADate aDate;
#SerializedName("ADATE")
#Expose
private String aDATE;
public Integer getANO() {
return aNO;
}
public void setANO(Integer aNO) {
this.aNO = aNO;
}
public String getPolicyNo() {
return policyNo;
}
public void setPolicyNo(String policyNo) {
this.policyNo = policyNo;
}
public ADate getADate() {
return aDate;
}
public void setADate(ADate aDate) {
this.aDate = aDate;
}
public String getADATE() {
return aDATE;
}
public void setADATE(String aDATE) {
this.aDATE = aDATE;
}
public PolicyNo (String policyNo){
this.policyNo = policyNo;
}
}
api endpoint :
#GET ("policy.php")
Call<PolicyStatus> getPolicyNo (#Query("PolicyNo") String policyNo);
I think you have given list_item_policy to match_parent. as you are saying you are getting 10 size in log. then this can be only reason. may be you will find items on scroll. So just change this to wrap_content.
Also check your recyclerView getting full height check in the preview.
Code looks fine except one thing which I noticed :-
Do this :-
LinearLayoutManager layoutManager
= new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
myList.setLayoutManager(layoutManager);
instead of :-
recyclerView.setLayoutManager(new LinearLayoutManager(this));
I am try to parse this Json array from this site NewsApi and display it in a listView.When I run the app nothing is displaying.Here is my code.What am doing wrong?.I have tried to debug the problem to no avail.
public class NewsApiClient {
public static String API_BASE_URL = "https://newsapi.org/v1/";
public static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
public static Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create()
);
public static Retrofit retrofit = builder.client(httpClient.build()).build();
NewsApiClient client = retrofit.create(NewsApiClient.class);
}
calling the endpoint?
public interface NewsApiInterface {
//endpoint
#GET("sources")
Call <SourcesResponse> getTechSources(
#Query("language") String language,
#Query("category") String category)
}
I am only using the name,description and category attributes.
public class Source {
#SerializedName("id")
public String id;
#SerializedName("name")
public String name;
#SerializedName("description")
public String description;
#SerializedName("url")
public String url;
#SerializedName("category")
public String category;
#SerializedName("language")
public String language;
#SerializedName("country")
public String country;
}
public class SourcesAdapter extends BaseAdapter {
Context context;
List<Source> sourceList;
public SourcesAdapter( Context context,List<Source> sourceList){
this.context = context;
this.sourceList = sourceList;
}
#Override
public int getCount() {
return sourceList.size();
}
#Override
public Object getItem(int position) {
return sourceList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
convertView = layoutInflater.inflate(R.layout.sources_list,null);
Source currentsource = sourceList.get(position);
TextView sourcesName = (TextView) convertView.findViewById(R.id.sources_name);
TextView sourcesDescription = (TextView) convertView.findViewById(R.id.sources_description);
TextView sourcesCategory = (TextView) convertView.findViewById(R.id.sources_category);
sourcesName.setText(currentsource.name);
sourcesDescription.setText(currentsource.description);
sourcesCategory.setText(currentsource.category);
return convertView;
}
}
public class SourcesFragment extends Fragment {
ListView listView;
public SourcesFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
//
//instance of the adapter to this listview
View view = inflater.inflate(R.layout.fragment_sources, container, false);
listView = (ListView)view.findViewById(R.id.list_view_sources) ;
getSources();
return view;
}
public void getSources(){
Retrofit retrofit = NewsApiClient.builder.build();
NewsApiInterface newsApiInterface = retrofit.create(NewsApiInterface.class);
Call<SourcesResponse> sourcesCall = newsApiInterface.getTechSources("en", "technology");
sourcesCall.enqueue(new Callback<SourcesResponse>() {
#Override
public void onResponse(Call<SourcesResponse> call, Response<SourcesResponse> response) {
List<Source> sources = response.body().sources;
SourcesAdapter sourcesAdapter = new SourcesAdapter(getContext(),sources);
listView.setAdapter(sourcesAdapter);
}
#Override
public void onFailure(Call<SourcesResponse> call, Throwable t) {
}
});
}
}
public class SourcesResponse {
#SerializedName("status")
public String status;
#SerializedName("sources")
public List<Source> sources;
}
I have created 3 fragments,the sources fragment is one of them.On the sources fragment i only want to display sources with technology.
Thank you in advance!
In your retrofit's onResponse callback method, you are accessing sources list like response.body().sources. Instead of this in the SourcesResponse , add a getter and setter for sources like this
public List<SourcesResponse> getSources() {
return sources;
}
public void setSources(List<SourcesResponse> sources) {
this.sources = sources;
}
Now go to your onResponse callback of retrofit and change
response.body().sources
to
response.body().getSources()
I am trying to make a service call with Retrofit and RxJava. I am using Picasso 2.5.2 to upload image from the API call. I am getting the image displayed, but i am getting single image only. When I debug, I get size as 20 but the displayed is single. Any help on that would be great.
The Adapter class:
public class PopularMoviesAdapter extends RecyclerView.Adapter<PopularMoviesAdapter.MoviesViewHolder> {
private final String TAG = PopularMoviesAdapter.class.getSimpleName();
private Context context;
private List<MoviesResponse> movieItems = new ArrayList<>();
public PopularMoviesAdapter(Context context, List<MoviesResponse> movieItems) {
this.context = context;
this.movieItems = movieItems;
}
#Override
public MoviesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.movies_grid_item, parent, false);
return new MoviesViewHolder(view);
}
#Override
public void onBindViewHolder(MoviesViewHolder holder, int position) {
List<Movies> movies = movieItems.get(position).getResults();
String imageUrl = IntentKeys.MOVIES_POSTER_ENDPOINT + movies.get(position).getPosterPath();
Log.d(TAG, "Poster URL from the API call: " + imageUrl);
Picasso.with(context).load(imageUrl).into(holder.imageView);
Glide.with(context).load(imageUrl).into(holder.imageView);
}
#Override
public int getItemCount() {
return movieItems.size();
}
class MoviesViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
public MoviesViewHolder(View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.movies_grid_item_image);
}
}}
The fragment class:
public class PopularMoviesFragment extends Fragment {
private static final int COLUMN_COUNT = 2;
private List<MoviesResponse> responses;
private RecyclerView recyclerView;
private PopularMoviesAdapter popularMoviesAdapter;
private Subscription subscription;
private MoviesService service = RetrofitManager.getMoviesClient().create(MoviesService.class);
public PopularMoviesFragment() {
responses = new ArrayList<>();
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_popular_movies, container, false);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(
getActivity(),
COLUMN_COUNT,
LinearLayoutManager.VERTICAL,
false);
recyclerView = (RecyclerView) view.findViewById(R.id.popular_movies_recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);
recyclerView.addItemDecoration(new GridSpacingItemDecoration(COLUMN_COUNT, dpToPx(10), true));
recyclerView.setItemAnimator(new DefaultItemAnimator());
return view;
}
private int dpToPx(int dp) {
Resources r = getResources();
return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()));
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onStart() {
super.onStart();
getMoviesSubscription();
}
private void getMoviesSubscription() {
subscription = service.getPopularMovies(IntentKeys.POPULAR_MOVIES_API_KEY)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
new Action1<MoviesResponse>() {
#Override
public void call(MoviesResponse movies) {
responses.add(movies);
}
}, new Action1<Throwable>() {
#Override
public void call(Throwable throwable) {
}
}, new Action0() {
#Override
public void call() {
displayPosters();
}
}
);
}
private void displayPosters() {
popularMoviesAdapter = new PopularMoviesAdapter(getContext(), responses);
recyclerView.setAdapter(popularMoviesAdapter);
recyclerView.invalidate();
}
#Override
public void onDestroyView() {
super.onDestroyView();
if (subscription != null && subscription.isUnsubscribed()) {
subscription.unsubscribe();
}
}
}
Finally the layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/movies_grid_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/movies_grid_item_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"/>
</LinearLayout>
POJO:
public class Movies implements Serializable {
private String posterPath;
private Boolean adult;
private String overview;
private String releaseDate;
private List<Integer> genreIds = new ArrayList<Integer>();
private Integer id;
private String originalTitle;
private String originalLanguage;
private String title;
private String backdropPath;
private Float popularity;
private Integer voteCount;
private Boolean video;
private Float voteAverage;
// getters and setters ommitted and constructor
}
public class MoviesResponse implements Serializable {
private Integer page;
private List<Movies> results = new ArrayList<Movies>();
private Integer totalResults;
private Integer totalPages;
// getters and setters ommitted and constructor
}
Thanks!
Try
Context context = holder.itemView.getContext();
in your onBindViewHolder, so that you don't pass context around.
Also try adding popularMoviesAdapter.notifyDataSetChanged() in your fragment. Do you mind uploading your POJO?
in your adapter
public PopularMoviesAdapter(List<Movie> movies) {
this.movies = movies;
}
in your fragment
.subscribe(
new Action1<MoviesResponse>() {
#Override
public void call(MoviesResponse movies) {
responses.add(movies.getResults());
popularMoviesAdapter.notifyDataSetChanged()
}
}
Change
#Override
public void onBindViewHolder(MoviesViewHolder holder, int position) {
List<Movies> movies = movieItems.get(position);
String imageUrl = IntentKeys.MOVIES_POSTER_ENDPOINT + movies.get(position).getPosterPath();
Log.d(TAG, "Poster URL from the API call: " + imageUrl);
Picasso.with(context).load(imageUrl).into(holder.imageView);
Glide.with(context).load(imageUrl).into(holder.imageView);
}
This is your code to set adapter
popularMoviesAdapter = new PopularMoviesAdapter(getContext(), responses); recyclerView.setAdapter(popularMoviesAdapter);
your ArrayList responses = new ArrayList() // this is your arraylist which you are providing to adapter
debug and check size of responses that how many records will return i think it has on one object
i think your array is somewhat like this
responses [{
0,{
}
}];
so passs
popularMoviesAdapter = new PopularMoviesAdapter(getContext(), responses.get(0).getResults
); recyclerView.setAdapter(popularMoviesAdapter);