How can I make this Adapter working with retrofit 2.0?
I mean in MainActivity
MainActivity.java
public class MainActivity extends AppCompatActivity {
private ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
Retrofit retrofit = new Retrofit.Builder().baseUrl("http://10.0.2.2/alruthea").build();
final TelService service = retrofit.create(TelService.class);
service.getphones(new Callback<List<tel_list>>() {
#Override
public void onResponse(Response<List<tel_list>> response, Retrofit retrofit) {
ArrayAdapter adapter = new ArrayAdapter(MainActivity.this, Telephones);
listView.setAdapter(adapter);
}
#Override
public void onFailure(Throwable t) {
}
}
);
}
}
TelService.java
public interface TelService {
#GET("/getphones.php")
retrofit.Call<List<tel_list>> getphones();
}
tel_list.java
public class tel_list{
private String name;
private String number;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
TelAdapter.java
public class TelAdapter extends ArrayAdapter<tel_list>{
Context context;
public TelAdapter(Context context, int resource, ArrayList<tel_list> users){
super(context, resource, users);
this.context = context;
}
#Override
public View getView(int position, View cell, ViewGroup parent){
Holder holder;
if (cell == null) {
holder = new Holder();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
cell = inflater.inflate(R.layout.list_item, null);
holder.textview = (TextView) cell.findViewById(R.id.names);
holder.textview1 = (TextView) cell.findViewById(R.id.numbers);
cell.setTag(holder);
}else {
holder = (Holder)cell.getTag();
}
tel_list user = getItem(position);
holder.textview.setText(user.getName());
holder.textview1.setText(user.getNumber());
return cell;
}
private class Holder{
TextView textview;
TextView textview1;
}
}
and about TelAdapter.class do I to write private class Holder or static class Holder?
Did you try it:
#Override
public void onResponse(Response<List<tel_list>> response, Retrofit retrofit) {
if (!response.isSuccess()) {
//TODO
return;
}
List<tel_list> date = response.body();
ArrayAdapter adapter = new ArrayAdapter(MainActivity.this, date);
listView.setAdapter(adapter);
}
The solution Patrick provided is correct. Just that you need 1 more resource parameter for the ArrayAdapter. This should work.
#Override
public void onResponse(Response<List<tel_list>> response, Retrofit retrofit) {
if (!response.isSuccess()) {
//TODO
return;
}
List<tel_list> date = response.body();
ArrayAdapter adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, date);
listView.setAdapter(adapter);
}
Related
I want to fetch data from json file from the internet using url, After doing all the codes RecyclerView says no adapter attached. I am not able to find out what is wrong with my code.
public class MainActivity extends AppCompatActivity {
private String urlData = "https://vast-shore-74260.herokuapp.com/banks?city=MUMBAI";
private RecyclerView recyclerView;
private RecyclerView.Adapter Radapter;
private List<ListItem> listItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//Using this listitem in recyclerView works just fine but when i try to to parse json file, RecyclerView says no adapter attached.
/*listItems = new ArrayList<>();
for (int i=0;i<=10;i++)
{
ListItem listItem = new ListItem(
"SBI"+(i+1),
"Lorem ipsum",
"SBIN00009945",
"khanapara",
"9945",
"ghy",
"kamrup",
"assam"
);
listItems.add(listItem);
}
Radapter = new MyAdapter(listItems,this);
recyclerView.setAdapter(Radapter);*/
Spinner dropdown = findViewById(R.id.spinner);
String[] items = new String[]{"Select City","Mumbai","Bangalore","Chennai"};
ArrayAdapter<String>adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,items)
{
#Override
public boolean isEnabled(int position)
{
if (position == 0)
{
return false;
}
else
{
return true;
}
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{
View view = super.getDropDownView(position,convertView,parent);
TextView txtView = (TextView)view;
if (position == 0)
{
txtView.setTextColor(Color.GRAY);
}
else
{
txtView.setTextColor(Color.BLACK);
}
return view;
}
};
dropdown.setAdapter(adapter);
listItems = new ArrayList<>();
loadRecyclerViewData();
}
private void loadRecyclerViewData()
{
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading Data...");
progressDialog.show();
StringRequest stringRequest = new StringRequest(
Request.Method.GET,
urlData,
new Response.Listener<String>() {
#Override
public void onResponse(String s) {
progressDialog.dismiss();
try {
JSONObject jsonObject = new JSONObject(s);
JSONArray array = jsonObject.getJSONArray(urlData);
for (int i=0; i<array.length();i++)
{
JSONObject o = array.getJSONObject(i);
ListItem item = new ListItem(
o.getString("bank_name"),
o.getString("address"),
o.getString("ifsc"),
o.getString("branch"),
o.getInt("bank_id"),
o.getString("city"),
o.getString("district"),
o.getString("state")
);
listItems.add(item);
}
/*Radapter = new MyAdapter(listItems,getApplicationContext());
recyclerView.setAdapter(Radapter);*/
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),error.getMessage(),Toast.LENGTH_LONG).show();
Log.e("errorcode",error.getMessage());
}
}
);
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
There might be some problem in this code:
Radapter = new MyAdapter(listItems,getApplicationContext());
recyclerView.setAdapter(Radapter);
This is the Adapter class
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<ListItem> listItems;
private Context context;
public MyAdapter(List<ListItem> listItemAdap, Context context) {
this.listItems = listItemAdap;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder( ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_items,parent,false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
ListItem listItem = listItems.get(position);
holder.txtviewBankName.setText(listItem.getBank_name());
holder.txtviewAddress.setText(listItem.getAddress());
holder.txtviewIfsc.setText(listItem.getIfsc());
holder.txtviewBranch.setText(listItem.getBranch());
holder.txtviewBankId.setText(listItem.getBank_id());
holder.txtviewCity.setText(listItem.getCity());
holder.txtviewDistrict.setText(listItem.getDistrict());
holder.txtviewState.setText(listItem.getState());
}
#Override
public int getItemCount() {
return listItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder
{
public TextView txtviewBankName;
public TextView txtviewAddress;
public TextView txtviewIfsc;
public TextView txtviewBranch;
public TextView txtviewBankId;
public TextView txtviewCity;
public TextView txtviewDistrict;
public TextView txtviewState;
public ViewHolder(View itemView) {
super(itemView);
txtviewBankName = (TextView)itemView.findViewById(R.id.bankName);
txtviewAddress = (TextView)itemView.findViewById(R.id.address);
txtviewIfsc = (TextView)itemView.findViewById(R.id.ifsc);
txtviewBranch = (TextView)itemView.findViewById(R.id.branch);
txtviewBankId = (TextView)itemView.findViewById(R.id.bankId);
txtviewCity = (TextView)itemView.findViewById(R.id.city);
txtviewDistrict = (TextView)itemView.findViewById(R.id.district);
txtviewState = (TextView)itemView.findViewById(R.id.state);
}
}
}
Your JSON string has some wrong characters, it should be like this
[{"ifsc":"ABHY0065001","bank_id":60,"branch":"RTGS-HO","address":"ABHYUDAYA BANK BLDG., B.NO.71, NEHRU NAGAR, KURLA (E), MUMBAI-400024","city":"MUMBAI","district":"GREATER MUMBAI","state":"MAHARASHTRA","bank_name":"ABHYUDAYA COOPERATIVE BANK LIMITED"}]
then for parsing this json you can use the com.google.Gson library like this
Type listType = new TypeToken<ArrayList<ListItem>>() {}.getType();
String s = "[{\"ifsc\":\"ABHY0065001\",\"bank_id\":60,\"branch\":\"RTGS-HO\",\"address\":\"ABHYUDAYA BANK BLDG., B.NO.71, NEHRU NAGAR, KURLA (E), MUMBAI-400024\",\"city\":\"MUMBAI\",\"district\":\"GREATER MUMBAI\",\"state\":\"MAHARASHTRA\",\"bank_name\":\"ABHYUDAYA COOPERATIVE BANK LIMITED\"}]";
List<ListItem> items = new Gson().fromJson(s, listType);
then you can set this list as input into your Adapter then assign your adapter to your recycler.
Hi I'm building chat app UI with android but it runs without showing any data or views,
I interact with a bot server that should return text after I send a text. I'm using retrofit for http connections.
the model class showing are the response from json
please help me and ask me any details you want
My adapter
public class MessageListAdapter extends
RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int VIEW_TYPE_MESSAGE_SENT = 1;
private static final int VIEW_TYPE_MESSAGE_RECEIVED = 2;
private List<Messages> data;
private Context mContext;
// constructor
public MessageListAdapter(List<Messages> data, Context mContext) {
this.data = data;
this.mContext = mContext;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView;
if (viewType == VIEW_TYPE_MESSAGE_SENT) {
return new SentMessageHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.user_message, parent, false));
} else {
return new RecvdMessageHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.bot_message, parent, false));
}
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
int type = getItemViewType(position);
Messages messages = data.get(position);
if (type == VIEW_TYPE_MESSAGE_SENT) {
SentMessageHolder sentMessageHolder = (SentMessageHolder) holder;
sentMessageHolder.messageBody.setText(messages.getText());
} else {
RecvdMessageHolder recvdMessageHolder = (RecvdMessageHolder) holder;
recvdMessageHolder.messageBody.setText(messages.getText());
}
}
public int getItemViewType(int position) {
Messages messages = data.get(position);
if (messages.getRecipientId() == ("default")) {
return VIEW_TYPE_MESSAGE_SENT;
} else
return VIEW_TYPE_MESSAGE_RECEIVED;
}
#Override
public int getItemCount() {
if (data != null) {
return data.size();
} else return 0;
}
class SentMessageHolder extends RecyclerView.ViewHolder {
public TextView messageBody;
public SentMessageHolder(View itemView) {
super(itemView);
messageBody = (TextView) itemView.findViewById(R.id.text_message_body);
}
}
class RecvdMessageHolder extends RecyclerView.ViewHolder {
public TextView messageBody;
public RecvdMessageHolder(View itemView) {
super(itemView);
messageBody = (TextView) itemView.findViewById(R.id.text_message_body);
}
}
My activity
public class MainActivity extends AppCompatActivity {
private EditText editText;
private RecyclerView mMessageRecycler;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter MessageListAdapter;
List<Messages> data;
//init client
Retrofit retrofit = RetrofitClient.getRetrofitClient();
//contact with the interface
APIService apiService = retrofit.create(APIService.class);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.my_msg);
data = new ArrayList<>();
mMessageRecycler = (RecyclerView) findViewById(R.id.reyclerview_message_list);
MessageListAdapter = new MessageListAdapter(data , this);
// messageListAdapter.notifyDataSetChanged();
mMessageRecycler.setAdapter(MessageListAdapter);
mMessageRecycler.setLayoutManager(new LinearLayoutManager(this));
mMessageRecycler.setHasFixedSize(true);
}
public void clickedbtn(View view) {
fetchMessages();
}
private void fetchMessages() {
Call<List<Messages>> res = apiService.getBotResponse(editText.getText().toString());
res.enqueue(new Callback<List<Messages>>() {
#Override
public void onResponse(Call<List<Messages>> call, Response<List<Messages>> response) {
List<Messages> messages = response.body();
data.addAll(messages);//Changed here
MessageListAdapter.notifyDataSetChanged();//Changed here
mMessageRecycler.smoothScrollToPosition(messages.size()-1);
}
#Override
public void onFailure(Call<List<Messages>> call, Throwable t) {
}
});
}
My model class
public class Messages {
#SerializedName("recipient_id")
#Expose
private String recipientId;
#SerializedName("text")
#Expose
private String text;
public Messages(String text) {
this.text = text;
}
public String getRecipientId() {
return recipientId;
}
public void setRecipientId(String recipientId) {
this.recipientId = recipientId;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
Try adding both the instantiation of the Adapter and the RecyclerView.setAdapter() to the retrofit onResponse():
Like this:
Call<List<Messages>> res = apiService.getBotResponse(editText.getText().toString());
res.enqueue(new Callback<List<Messages>>() {
#Override
public void onResponse(Call<List<Messages>> call, Response<List<Messages>> response) {
List<Messages> messages = response.body();
//Init the Adpater here!!
MessageListAdapter = new MessageListAdapter(data , this);
//SetAdapter here!!
mMessageRecycler.setAdapter(MessageListAdapter);
data.addAll(messages);//Changed here
MessageListAdapter.notifyDataSetChanged();//Changed here
mMessageRecycler.smoothScrollToPosition(messages.size()-1);
}
#Override
public void onFailure(Call<List<Messages>> call, Throwable t) {
}
});
EDIT:
In addition to the above, your clickedbtn() should look like this:
public void clickedbtn(View view) {
Messages msg = new Messages(editText.getText().toString());
msg.setRecipientId("default");
data.add(msg);
MessageListAdapter.notifyDataSetChanged();
fetchMessages();
}
how to get ID position in call retrofit
GET https://api.themoviedb.org/3/movie/{movie_id}/credits?api_key=<>
i need to get position id send to server in loadCast function
and in MovieService that's retrofit call i need send postion id befor credits
i don't know how to do that if any one can help me thanks so much for that <3
//this my call retrofit server
public interface MovieService {
#GET("popular?" + Common.API_KEY + "&language=en-US")
Call<MoviesList> getPopular(#Query("api_key") String api_key);
#GET( ListMovieAdapter.SELECTED_MOVIE +"/credits?" + Common.API_KEY +
"&language=en-US")
Call<MovieCast> getCast(
#Query("api_key") String api_key);
----------------------------------------------------------------
package com.example.android.movie;
/**
* Created by yuyu on 12-Nov-18.
*/
public class MovieDetails extends YouTubeBaseActivity {
Result selectedMovie;
private ArrayList<Cast> cast;
private RecyclerView.LayoutManager mLayoutManager;
private RecyclerView mRecyclerView;
private CastMovieAdapter castMovieAdapter;
private TextView name;
private ImageView imageMovie;
private TextView date;
private TextView rating;
private ArrayList<Result> results;
MovieService mService;
private static final String YT_API_KEY = "###";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.movie_details);
selectedMovie =
getIntent().getParcelableExtra(ListMovieAdapter.SELECTED_MOVIE);
mRecyclerView = (RecyclerView) findViewById(R.id.cast_recycler);
mLayoutManager = new LinearLayoutManager(this,
LinearLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(mLayoutManager);
cast = new ArrayList<>();
castMovieAdapter = new CastMovieAdapter(cast, MovieDetails.this);
mRecyclerView.setAdapter(castMovieAdapter);
results = new ArrayList<>();
mService = Common.getMovieService();
loadTriler();
loadMovies();
loadCast();
}
this function to load movie
private void loadMovies() {
mService.getPopular(Common.API_KEY).enqueue(new Callback<MoviesList>()
{
#Override
public void onResponse(Call<MoviesList> call, Response<MoviesList>
response) {
results.clear();
results.addAll(response.body().getResults());
name = (TextView) findViewById(R.id.name_movie);
rating = (TextView) findViewById(R.id.rating);
date = (TextView) findViewById(R.id.date_det);
imageMovie = (ImageView) findViewById(R.id.imageView);
date.setText(selectedMovie.getReleaseDate());
name.setText(selectedMovie.getTitle());
rating.setText(String.valueOf(selectedMovie.getVoteAverage()));
final String image = Common.IMAGE_LOAD +
selectedMovie.getPosterPath();
Picasso.with(MovieDetails.this)
.load(image)
.into(imageMovie);
}
#Override
public void onFailure(Call<MoviesList> call, Throwable t) {
Log.d("===LoadMovies", "onResponse: " + t);
}
});
}
//i have proplem here in send ID postion
private void loadCast() {
mService.getCast(
ListMovieAdapter.SELECTED_MOVIE+Common.API_KEY).enqueue(new
Callback<MovieCast>() {
#Override
public void onResponse(Call<MovieCast> call, final
Response<MovieCast> response) {
cast.clear();
cast.addAll(response.body().getCast());
mRecyclerView.getAdapter().notifyDataSetChanged();
}
#Override
public void onFailure(Call<MovieCast> call, Throwable t) {
}
});
}
}
package com.example.android.movie.Adapter;
/**
* Created by yuyu on 11-Nov-18.
*/
public class ListMovieAdapter extends
RecyclerView.Adapter<ListMovieAdapter.MyViewHolder> {
private ArrayList<Result> mMovies;
private Context context;
public static final String SELECTED_MOVIE = "selected_movie";
private int lastPosition = -1;
public ListMovieAdapter(ArrayList<Result> mMovies, Context context) {
this.mMovies = mMovies;
this.context = context;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.list_views, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
//Animation Scroll
Animation animation = AnimationUtils.loadAnimation(context,
(position > lastPosition) ? R.anim.up_from_bottom
: R.anim.down_from_top);
holder.itemView.startAnimation(animation);
lastPosition = position;
holder.nameMovie.setText(mMovies.get(position).getTitle());
final String image = Common.IMAGE_LOAD +
mMovies.get(position).getPosterPath();
Picasso.with(context)
.load(image)
.into(holder.imageMovie);
holder.rating.setText(String.valueOf(
mMovies.get(position).getVoteAverage()));
holder.dateMovie.setText(mMovies.get(position).getReleaseDate());
holder.setItemClickListener(new ItemClickListener() {
#Override
public void onClick(View view, int position) {
Intent intent = new Intent(context, MovieDetails.class);
Result result = mMovies.get(position);
intent.putExtra(SELECTED_MOVIE, result);
context.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return mMovies.size();
}
class MyViewHolder extends RecyclerView.ViewHolder implements
View.OnClickListener {
ImageView imageMovie;
TextView nameMovie;
TextView rating;
TextView dateMovie;
ItemClickListener itemClickListener;
public MyViewHolder(View itemView) {
super(itemView);
this.imageMovie = (ImageView)
itemView.findViewById(R.id.image_movie);
this.nameMovie = (TextView) itemView.findViewById(R.id.name_movie);
this.rating = (TextView) itemView.findViewById(R.id.rating);
this.dateMovie = (TextView) itemView.findViewById(R.id.date);
itemView.setOnClickListener(this);
}
public void setItemClickListener(ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
#Override
public void onClick(View v) {
itemClickListener.onClick(v, getAdapterPosition());
}
}
}
From the Retrofit documentation:
URL MANIPULATION A request URL can be updated dynamically using
replacement blocks and parameters on the method. A replacement block
is an alphanumeric string surrounded by { and }. A corresponding
parameter must be annotated with #Path using the same string.
#GET("group/{id}/users")
Call<List<User>> groupList(#Path("id") int groupId);
https://square.github.io/retrofit/
When I just display string data it seems to show up fine, but when I try to display an int or double the RecyclerView won't populate. I am pulling from the Google places API JSON. I think maybe it is the way I'm using my onBindViewHolder()? Thanks
// MainActivity
private void getJson() {
String URL = https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=40.7085,-74.003124&opennow&radius=5000&type=restaurant&key=
JsonObjectRequest root = new JsonObjectRequest(Request.Method.GET, URL, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray results = response.getJSONArray("results");
Log.d("RESULTS", String.valueOf(results));
for(int i = 0; i<results.length(); i++) {
JSONObject resultsObj = results.getJSONObject(i);
mImageUrls.add(resultsObj.getString("icon"));
mTitle.add(resultsObj.getString("name"));
//mRating.add(resultsObj.getDouble("rating"));
mPriceLevel.add(resultsObj.getInt("price_level"));
}
getRecyclerView();
} catch (JSONException e) {
e.printStackTrace();
}
}
},new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
mRequestQueue.add(root);
}
private void getRecyclerView() {
// LinearLayoutManager
recyclerView = findViewById(R.id.recyclerView);
adapter = new RecyclerViewAdapter(mImageUrls, mTitle, mPriceLevel, this);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
}
And the RecyclerViewAdapter Class is.
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{
private static final String TAG = "RecyclerViewAdapter";
private ArrayList<String> rNames = new ArrayList<>();
private ArrayList<String> rImageUrls = new ArrayList<>();
private ArrayList<Double> rRatings = new ArrayList<>();
private ArrayList<Integer> rPriceLevel = new ArrayList<>();
private Context mContext;
public RecyclerViewAdapter(ArrayList<String> image, ArrayList<String> name, ArrayList<Integer> priceLevel, Context context) {//, ArrayList<Double> rating
this.rImageUrls = image;
this.rNames = name;
//this.rRatings = rating;
this.rPriceLevel = priceLevel;
mContext = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_item, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
//return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, final int position) {
Glide.with(mContext).asBitmap().load(rImageUrls.get(position)).into(holder.image);
holder.name.setText(rNames.get(position));
//holder.rating.setText(rRatings.get(position));
holder.priceLevel.setText(rPriceLevel.get(position));
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick: clicked on an image: " + rNames.get(position));
Toast.makeText(mContext, rNames.get(position), Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return rNames.size(); //we can use rImageUrls/rRatings as well because they are all the same size
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView image;
TextView name;
TextView rating;
TextView priceLevel;
TextView address;
RelativeLayout parentLayout;
public ViewHolder(View itemView) {
super(itemView);
image = (ImageView) itemView.findViewById(R.id.image);
name = (TextView) itemView.findViewById(R.id.title);
//rating = itemView.findViewById(R.id.rating);
priceLevel = (TextView) itemView.findViewById(R.id.priceLevel);
parentLayout = itemView.findViewById(R.id.parentLayout);
}
}
}
The reason for this is you are not converting Double to String
change this line holder.rating.setText(rRatings.get(position)); to holder.rating.setText(String.valueOf(rRatings.get(position)));
I would like to suggest you to create an object having all necessary parameters in it and then pass a single list of that object to the RecyclerViewAdapter.
public class MapElement {
public String name;
public String imageUrl;
public Double rating;
public Integer price;
}
Then create an ArrayList of this object when you get the response from your getJson function call. Now in your adapter just use a single list of MapElement object to show the data from there.
The modified adapter should look like this.
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{
private static final String TAG = "RecyclerViewAdapter";
private ArrayList<MapElement> elements = new ArrayList<>();
private Context mContext;
public RecyclerViewAdapter(ArrayList<MapElement> elements, Context context) {
this.elements = elements;
mContext = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_item, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, final int position) {
Glide.with(mContext).asBitmap().load(rImageUrls.get(position)).into(holder.image);
holder.name.setText(elements.get(position).name);
holder.rating.setText(elements.get(position).rating);
holder.priceLevel.setText(elements.get(position).price);
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick: clicked on an image: " + rNames.get(position));
Toast.makeText(mContext, elements.get(position).name, Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return elements.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView image;
TextView name;
TextView rating;
TextView priceLevel;
TextView address;
RelativeLayout parentLayout;
public ViewHolder(View itemView) {
super(itemView);
image = (ImageView) itemView.findViewById(R.id.image);
name = (TextView) itemView.findViewById(R.id.title);
rating = itemView.findViewById(R.id.rating);
priceLevel = (TextView) itemView.findViewById(R.id.priceLevel);
parentLayout = itemView.findViewById(R.id.parentLayout);
}
}
}
First you should check that the rating you are getting is a double value.
It might be the case that an exception is thrown after this line
mRating.add(resultsObj.getDouble("rating"))
Also as #hooray_todd siggested textview only takes string value so you have to change your data into string.
I need to parse JSON data into HorizontalScrollView with Retrofit2. I parsed it into ListView succesfully, but I haven't imagination how to do it into HorizontalScrollView. Google can't give me right answer.
My code with ListView below.
P.S. I want to do HorizontalScrollView instead of ListView.
Like this.
MyForecastAdapter.java
public class MyForecastAdapter extends ArrayAdapter<Forecast> {
List<Forecast> forecastList;
Context context;
private LayoutInflater inflater;
public MyForecastAdapter(Context context, List<Forecast> objects) {
super(context, 0, objects);
this.context = context;
this.inflater = LayoutInflater.from(context);
forecastList = objects;
}
#Override
public Forecast getItem(int position) {
return forecastList.get(position);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView == null) {
View view = inflater.inflate(R.layout.activity_row, parent, false);
viewHolder = ViewHolder.create((RelativeLayout) view);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
Forecast item = getItem(position);
viewHolder.textViewTemp.setText(item.getMain().getTemp());
viewHolder.textViewHumidity.setText(item.getMain().getHumidity());
return viewHolder.relativeLayout;
}
private static class ViewHolder {
public final RelativeLayout relativeLayout;
public final TextView textViewTemp;
public final TextView textViewHumidity;
private ViewHolder(RelativeLayout relativeLayout, TextView textViewTemp, TextView textViewHumidity) {
this.relativeLayout = relativeLayout;
this.textViewTemp = textViewTemp;
this.textViewHumidity = textViewHumidity;
}
public static ViewHolder create(RelativeLayout relativeLayout) {
TextView textViewTemp = (TextView) relativeLayout.findViewById(R.id.textViewTemp);
TextView textViewHumidity = (TextView) relativeLayout.findViewById(R.id.textViewHumidity);
return new ViewHolder(relativeLayout, textViewTemp, textViewHumidity);
}
}
}
ForecastList.java
public class ForecastList {
#SerializedName("list")
#Expose
private ArrayList<Forecast> forecasts = new ArrayList<>();
public ArrayList<Forecast> getForecasts() {
return forecasts;
}
public void setForecasts(ArrayList<Forecast> forecasts) {
this.forecasts = forecasts;
}
}
Forecast.java - getters/setters.
ApiService.java
public interface ApiService {
#GET("/data/2.5/forecast?q=Kirov,ru&appid=/../")
Call<ForecastList> getMyJSON();
}
RetrofitClient.java
public class RetrofitClient {
private static final String ROOT_URL = "http://api.openweathermap.org";
private static Retrofit getRetrofitInstance() {
return new Retrofit.Builder().baseUrl(ROOT_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
public static ApiService getApiService() {
return getRetrofitInstance().create(ApiService.class);
}
}
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
forecastList = new ArrayList<>();
parentView = findViewById(R.id.parentLayout);
listView = (ListView) findViewById(R.id.listView);
ApiService apiService = RetrofitClient.getApiService();
Call<ForecastList> call = apiService.getMyJSON();
call.enqueue(new Callback<ForecastList>() {
#Override
public void onResponse(Call<ForecastList> call, Response<ForecastList> response) {
if (response.isSuccessful()) {
forecastList = response.body().getForecasts();
adapter = new MyForecastAdapter(MainActivity.this, forecastList);
listView.setAdapter(adapter);
} else {
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
}
}
#Override
public void onFailure(Call<ForecastList> call, Throwable t) {
}
});
}
You can use horizonatal Scroll View using Recycle View with LinearLayoutManager with constructor new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
Use this code .....
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
forecastList = new ArrayList<>();
parentView = findViewById(R.id.parentLayout);
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
myList = (RecyclerView) findViewById(R.id.listView);
myList.setLayoutManager(layoutManager);
ApiService apiService = RetrofitClient.getApiService();
Call<ForecastList> call = apiService.getMyJSON();
call.enqueue(new Callback<ForecastList>() {
#Override
public void onResponse(Call<ForecastList> call, Response<ForecastList> response) {
if (response.isSuccessful()) {
forecastList = response.body().getForecasts();
adapter = new MyForecastAdapter(MainActivity.this, forecastList);
myList.setAdapter(adapter);
} else {
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
}
}
#Override
public void onFailure(Call<ForecastList> call, Throwable t) {
}
});
}
and change ListView into RecycleView in XML and in code also.....