I have added data in firebase. All i have to do is get that data in recyclerview. i have done this many type, but this time it is not showing and i don't know the reason because it is not showing in log. Can any one help?
here's my Activity where the RV is located
rvsalonlist is recyclerview
public void firebasedata() {
FirebaseRecyclerOptions<salonList> options =
new FirebaseRecyclerOptions.Builder<salonList>()
.setQuery(FirebaseDatabase.getInstance().getReference().child("salon"), salonList.class)
.build();
adapter = new SalonListAdapter(options);
rvSalonList.setAdapter(adapter);
adapter.startListening();
}
This is the adapter
public class SalonListAdapter extends FirebaseRecyclerAdapter<salonList,SalonListAdapter.myviewholder> {
public SalonListAdapter(#NonNull FirebaseRecyclerOptions<salonList> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull myviewholder holder, int position, #NonNull salonList model) {
holder.tvSalonName.setText(String.valueOf(model.getSalonName()));
holder.tvSalonAddress.setText(String.valueOf(model.getSalonAddresss()));
Glide.with(holder.ivSalonImage.getContext()).load(model.getImageUrl()).into(holder.ivSalonImage);
}
#NonNull
#Override
public myviewholder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_salon,parent,false);
return new myviewholder(view);
}
class myviewholder extends RecyclerView.ViewHolder{
ImageView ivSalonImage;
TextView tvSalonName, tvSalonMobileNumber, tvSalonAddress;
public myviewholder(#NonNull View itemView) {
super(itemView);
ivSalonImage = itemView.findViewById(R.id.ivSalonImage);
tvSalonName = itemView.findViewById(R.id.tvSalonName);
tvSalonMobileNumber = itemView.findViewById(R.id.tvSalonMobileNumber);
tvSalonAddress = itemView.findViewById(R.id.tvSalonAddress);
}
}
}
Heres the model class .
[![public class salonList {
private String imageUrl, salonName, ownerName, salonEmail, salonMobileNumber, salonAddresss, salonOpenTime, salonCloseTime;
public salonList() {
}
public salonList(String imageUrl, String salonName, String ownerName, String salonEmail, String salonMobileNumber, String salonAddresss, String salonOpenTime, String salonCloseTime) {
this.imageUrl = imageUrl;
this.salonName = salonName;
this.ownerName = ownerName;
this.salonEmail = salonEmail;
this.salonMobileNumber = salonMobileNumber;
this.salonAddresss = salonAddresss;
this.salonOpenTime = salonOpenTime;
this.salonCloseTime = salonCloseTime;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public String getSalonName() {
return salonName;
}
public void setSalonName(String salonName) {
this.salonName = salonName;
}
public String getOwnerName() {
return ownerName;
}
public void setOwnerName(String ownerName) {
this.ownerName = ownerName;
}
public String getSalonEmail() {
return salonEmail;
}
public void setSalonEmail(String salonEmail) {
this.salonEmail = salonEmail;
}
public String getSalonMobileNumber() {
return salonMobileNumber;
}
public void setSalonMobileNumber(String salonMobileNumber) {
this.salonMobileNumber = salonMobileNumber;
}
public String getSalonAddresss() {
return salonAddresss;
}
public void setSalonAddresss(String salonAddresss) {
this.salonAddresss = salonAddresss;
}
public String getSalonOpenTime() {
return salonOpenTime;
}
public void setSalonOpenTime(String salonOpenTime) {
this.salonOpenTime = salonOpenTime;
}
public String getSalonCloseTime() {
return salonCloseTime;
}
public void setSalonCloseTime(String salonCloseTime) {
this.salonCloseTime = salonCloseTime;
}
}
Database Screenshot
your model class variable names and firebase attributes name are not same. use same name in both places.
for example in firebase use salonName instead of name. similarly for other attributes as well
The names have to be identical
For eg, you have ownerName in the class but ownername in the database. Those should all be identical to the ones you have in your class.
Related
I am using Android Paging with Room Database.I am going to fetch data using retrofit.But I am getting error No adapter attached; skipping layout.I searched a lot but dont find solution for this. Base url is working, for security reason i just hide base url.
private StoreAdapter storeAdapter;
private Store_ViewModel store_viewModel;
private RecyclerView recyclerView;
private static final String URL_DATA="https://xxxx/";
//insertion
private Store_Repository store_repository;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
store_repository=new Store_Repository(getApplication());
//adapter
storeAdapter=new StoreAdapter(getApplicationContext(), this);
//recycler
recyclerView=findViewById(R.id.recycler_store);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
store_viewModel=new ViewModelProvider(this).get(Store_ViewModel.class);
store_viewModel.pagedListLiveData.observe(this, new Observer<PagedList<StoreModel>>() {
#Override
public void onChanged(PagedList<StoreModel> storeModels) {
storeAdapter.submitList(storeModels);
recyclerView.setAdapter(storeAdapter);
}
});
getAllProducts();
}
private void getAllProducts() {
Retrofit retrofit=new Retrofit.Builder()
.baseUrl(URL_DATA)
.addConverterFactory(GsonConverterFactory.create())
.build();
//calling api
Api api=retrofit.create(Api.class);
Call<List<StoreModel>>call=api.getAllProducts();
call.enqueue(new Callback<List<StoreModel>>() {
#Override
public void onResponse(Call<List<StoreModel>> call, Response<List<StoreModel>> response) {
if (response.isSuccessful())
{
store_repository.insert(response.body());
}
}
#Override
public void onFailure(Call<List<StoreModel>> call, Throwable t) {
Toast.makeText(MainActivity.this, "Something get Wrong", Toast.LENGTH_SHORT).show();
}
});
}
This is my ViewModel Class
public class Store_ViewModel extends AndroidViewModel {
public LiveData<PagedList<StoreModel>>pagedListLiveData;
private StoreDao storeDao;
public Store_ViewModel(#NonNull Application application) {
super(application);
storeDao= StoreDatabase.getINSTANCE(application).storeDao();
pagedListLiveData=new LivePagedListBuilder<>(
storeDao.getAllItems(),5
).build();
}
}
And this is my adapter class
public class StoreAdapter extends PagedListAdapter<StoreModel,StoreAdapter.StoreViewHolder> {
private Context context;
private static Listener listener;
public StoreAdapter(Context context,Listener listener)
{
super(storeModelItemCallback);
this.context=context;
this.listener=listener;
}
#NonNull
#Override
public StoreViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return null;
}
#Override
public void onBindViewHolder(#NonNull StoreViewHolder holder, int position) {
StoreModel storeModel=getItem(position);
holder.Product_name.setText(storeModel.getProduct_name());
holder.Product_weight.setText(storeModel.getProduct_weight());
holder.Price.setText(storeModel.getPrice());
holder.Mrp.setText(storeModel.getMrp());
}
static class StoreViewHolder extends RecyclerView.ViewHolder
{
TextView Product_name;
TextView Product_weight;
TextView Price;
TextView Mrp;
public StoreViewHolder(#NonNull View itemView) {
super(itemView);
Product_name=itemView.findViewById(R.id.product_name);
Product_weight=itemView.findViewById(R.id.product_weight);
Price=itemView.findViewById(R.id.price);
Mrp=itemView.findViewById(R.id.mrp);
//listener
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onItemCLickListener(getAdapterPosition());
}
});
}
}
static DiffUtil.ItemCallback<StoreModel> storeModelItemCallback=new DiffUtil.ItemCallback<StoreModel>() {
#Override
public boolean areItemsTheSame(#NonNull StoreModel oldItem, #NonNull StoreModel newItem) {
return oldItem.getDatabase_id()==newItem.getDatabase_id();
}
#SuppressLint("DiffUtilEquals")
#Override
public boolean areContentsTheSame(#NonNull StoreModel oldItem, #NonNull StoreModel newItem) {
return oldItem.equals(newItem);
}
};
Json Response from Server
{"status":1,"msg":"",
"paginate":{"limit":1000,"PageNo":1},
"data":
[
{
"product_id":23234,
"product_brand_id":130,
"product_name":"Xyz"
,"product_code":"1554729666482",
"mrp":5,
"price":4,
"product_weight":1,
"product_weight_unit":"PCS"
}
,{"product_id":23244,
"product_brand_id":130,
"product_name":"Abc - 100 Gms",
"product_code":"9A","mrp":38,"price":31.94,
"product_weight":100,"product_weight_unit":"GM"}
ApiInterface
public interface Api {
#GET("/get-products")
Call<List<StoreModel>>getAllProducts();
}
Below is my StoreModel in which i am using room database fro creating tables
#Entity(tableName = "store",indices = #Index(value="product_id",unique = true))
public class StoreModel {
#PrimaryKey(autoGenerate = true)
private int database_id;
#SerializedName("product_id")
private int product_id;
#SerializedName("product_brand_id")
private int product_brand_id;
#SerializedName("product_name")
private String product_name;
#SerializedName("product_code")
private int product_code;
#SerializedName("mrp")
private int mrp;
#SerializedName("price")
private int price;
#SerializedName("product_weight")
private int product_weight;
#SerializedName("product_weight_unit")
private String product_weight_unit;
public StoreModel() {
}
public StoreModel(int product_id, int product_brand_id, String product_name, int product_code, int mrp, int price, int product_weight, String product_weight_unit) {
this.product_id = product_id;
this.product_brand_id = product_brand_id;
this.product_name = product_name;
this.product_code = product_code;
this.mrp = mrp;
this.price = price;
this.product_weight = product_weight;
this.product_weight_unit = product_weight_unit;
}
public int getProduct_id() {
return product_id;
}
public void setProduct_id(int product_id) {
this.product_id = product_id;
}
public int getProduct_brand_id() {
return product_brand_id;
}
public void setProduct_brand_id(int product_brand_id) {
this.product_brand_id = product_brand_id;
}
public String getProduct_name() {
return product_name;
}
public void setProduct_name(String product_name) {
this.product_name = product_name;
}
public int getProduct_code() {
return product_code;
}
public void setProduct_code(int product_code) {
this.product_code = product_code;
}
public int getMrp() {
return mrp;
}
public void setMrp(int mrp) {
this.mrp = mrp;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getProduct_weight() {
return product_weight;
}
public void setProduct_weight(int product_weight) {
this.product_weight = product_weight;
}
public String getProduct_weight_unit() {
return product_weight_unit;
}
public void setProduct_weight_unit(String product_weight_unit) {
this.product_weight_unit = product_weight_unit;
}
public int getDatabase_id() {
return database_id;
}
public void setDatabase_id(int database_id) {
this.database_id = database_id;
}
}
Below is Dao class
#Dao
public interface StoreDao {
#Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(List<StoreModel> storeModels);
#Query("DELETE FROM store")
void deleteAll();
#Query("SELECT * FROM store ORDER BY database_id ASC")
DataSource.Factory<Integer,StoreModel>getAllItems();
}
Set recyclerView.setAdapter(storeAdapter); outside of observer because each time data is observed it will set adapter. So, adapter is attached to recyclerView only once. Like
recyclerView.setAdapter(storeAdapter);
store_viewModel.pagedListLiveData.observe(this, new Observer<PagedList<StoreModel>>() {
#Override
public void onChanged(PagedList<StoreModel> storeModels) {
storeAdapter.submitList(storeModels);
//recyclerView.setAdapter(storeAdapter);
}
});
From the API you are not getting exactly the StoreModel as response. You are getting another object which is StoreModel is a child object. You have to create resposne object like below:
public class ResponseObject{
//#SerializedName("status")
//private int status;
#SerializedName("data")
private List<StoreModel> storeModelList;
//getters and setters goes here
}
And then your interface should be like below as you are expecting
ResponseObject here
public interface Api {
#GET("/get-products")
Call<ResponseObject> getAllProducts();
}
friends...
In my project, I am creating a list of images from the server and showing in Recyclerview.
now I want to remove a particular item from the list when I click on the verify button.
I tried to hide holder.itemview but its see again when I scroll.
I just want to remove or hide that item which is verified once.
here is my code :
1) Main activity
public class MainActivity extends AppCompatActivity {
private static final String url = "www.mysite.com/api/api-id-list.php?action=imgs";
private ProgressDialog mDetailProgress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main_activity);
LoadRecyclerView();
}
private void LoadRecyclerView() {
mDetailProgress = new ProgressDialog(this);
mDetailProgress.setTitle("Loading images");
mDetailProgress.setMessage("Please Wait...");
mDetailProgress.setCanceledOnTouchOutside(false);
mDetailProgress.show();
final RecyclerView imgList = (RecyclerView) findViewById(R.id.imgList);
imgList.setLayoutManager(new LinearLayoutManager(this));
StringRequest request = new StringRequest(url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
Image[] images = gson.fromJson(response, Image[].class);
imgList.setAdapter(new ImageAdapter(MainActivity.this, images));
mDetailProgress.dismiss();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getBaseContext(), "Something went wrong..!", Toast.LENGTH_LONG);
}
});
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(request);
}}
2) ImageAdapter
public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder> {
private ProgressDialog mDetailProgress;
private Context context;
private Image[] data;
Button btn_ban, btn_verify;
private View view;
public ImageAdapter (Context context, Image[] data) {
this.context = context;
this.data = data;
}
#NonNull
#Override
public ImageViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int i) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
view = inflater.inflate(R.layout.single_item, parent, false);
return new ImageViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull final ImageViewHolder holder, final int position) {
final Image imagelist = data[position];
holder.userCode.setText(imagelist.getCode());
Glide.with(holder.userImage.getContext()).load(imagelist.getIdPhoto()).into(holder.userImage);
btn_verify.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.itemView.setVisibility(View.GONE);
}
});
}
#Override
public int getItemCount() { return data.length; }
public class ImageViewHolder extends RecyclerView.ViewHolder {
TextView userCode;
ImageView userImage;
public ImageViewHolder(#NonNull View itemView) {
super(itemView);
userImage = (ImageView) itemView.findViewById(R.id.card_iv_img);
userCode = (TextView) itemView.findViewById(R.id.card_tv_code);
btn_ban = (Button) itemView.findViewById(R.id.btn_ban);
btn_verify = (Button) itemView.findViewById(R.id.btn_verify);
}
}}
3) Image.java
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Image {
#SerializedName("per_ID")
#Expose
private String perID;
#SerializedName("code")
#Expose
private String code;
#SerializedName("first_nm")
#Expose
private String firstNm;
#SerializedName("last_nm")
#Expose
private String lastNm;
#SerializedName("photo_ID")
#Expose
private String photoID;
#SerializedName("id_photo")
#Expose
private String idPhoto;
public String getPerID() {
return perID;
}
public void setPerID(String perID) {
this.perID = perID;
}
public Image withPerID(String perID) {
this.perID = perID;
return this;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public Image withCode(String code) {
this.code = code;
return this;
}
public String getFirstNm() {
return firstNm;
}
public void setFirstNm(String firstNm) {
this.firstNm = firstNm;
}
public Image withFirstNm(String firstNm) {
this.firstNm = firstNm;
return this;
}
public String getLastNm() {
return lastNm;
}
public void setLastNm(String lastNm) {
this.lastNm = lastNm;
}
public Image withLastNm(String lastNm) {
this.lastNm = lastNm;
return this;
}
public String getPhotoID() {
return photoID;
}
public void setPhotoID(String photoID) {
this.photoID = photoID;
}
public Image withPhotoID(String photoID) {
this.photoID = photoID;
return this;
}
public String getIdPhoto() {
return idPhoto;
}
public void setIdPhoto(String idPhoto) {
this.idPhoto = idPhoto;
}
public Image withIdPhoto(String idPhoto) {
this.idPhoto = idPhoto;
return this;
}}
First make data not array, but List, so it's easy to remove item;
Secondly set listener for verify button inside ViewHolder and inside OnClick remove item and notify adapter.
btn_verify.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = getAdapterPosition();
data.remove(position);
notifyItemRemoved(position);
}
});
Note: Setting OnClickListener inside ViewHolder makes sure that only correct item is removed by using getAdapterPosition() to get correct position.
Position provided by onBindViewHolder might be invalid after new items are inserted.
im using thoughtbot expandable recyclerview (https://github.com/thoughtbot/expandable-recycler-view) and i can't get to expand groups on clicks. also i know that the child items are not even binded (not sure if they were supposed to bind or only on expanding of group.)
my view holders:
public class PlaylistViewHolder extends GroupViewHolder {
private TextView mPlaylistName, mPlaylistCount;
private ImageView arrow;
ExpandableListView.OnGroupClickListener listener;
public PlaylistViewHolder(View itemView) {
super(itemView);
mPlaylistName = itemView.findViewById(R.id.playlistNameView);
mPlaylistCount = itemView.findViewById(R.id.videosCount);
arrow = itemView.findViewById(R.id.expandBtn);
}
public void setCurrentPlaylist(YoutubePlaylist playlist){
Log.e("###",playlist.toString());
mPlaylistName.setText(playlist.getListTitle());
mPlaylistCount.setText("5");
}
}
/////
public class VideoViewHolder extends ChildViewHolder {
TextView mVideoName,mVideoLinkView;
ImageView mVideoThumb;
public VideoViewHolder(View itemView) {
super(itemView);
mVideoName = itemView.findViewById(R.id.videoNameView);
mVideoThumb = itemView.findViewById(R.id.videoThumb);
mVideoLinkView = itemView.findViewById(R.id.videoLinkView);
}
public void onBind(YoutubeVideo video){
Log.e("###","Video binded!!!!!!!");
mVideoName.setText(video.getTitle());
mVideoLinkView.setText(video.getThumbnailLink());
}
}
my classes:
the group class:
public class YoutubePlaylist extends ExpandableGroup<YoutubeVideo> {
#SerializedName("ListTitle")
private String title;
#SerializedName("ListItems")
private ArrayList<YoutubeVideo> videos;
public YoutubePlaylist(String title, List<YoutubeVideo> items) {
super(title, items);
this.title = title;
}
public String getListTitle() {
return title;
}
public void setListTitle(String listTitle) {
this.title = listTitle;
}
public ArrayList<YoutubeVideo> getVideos() {
return videos;
}
public void setVideos(ArrayList<YoutubeVideo> videos) {
this.videos = videos;
}
#Override
public String toString() {
return "YoutubePlaylist{" +
"title='" + title + '\'' +
", videos=" + videos +
'}';
}
}
my child class:
class YoutubeVideo implements Parcelable {
#SerializedName("Title")
private String title;
#SerializedName("link")
private String linkToVideo;
#SerializedName("thumb")
private String thumbnailLink;
public YoutubeVideo(String title, String linkToVideo, String thumbnailLink) {
this.title = title;
this.linkToVideo = linkToVideo;
this.thumbnailLink = thumbnailLink;
}
protected YoutubeVideo(Parcel in) {
title = in.readString();
linkToVideo = in.readString();
thumbnailLink = in.readString();
}
public static final Creator<YoutubeVideo> CREATOR = new Creator<YoutubeVideo>() {
#Override
public YoutubeVideo createFromParcel(Parcel in) {
return new YoutubeVideo(in);
}
#Override
public YoutubeVideo[] newArray(int size) {
return new YoutubeVideo[size];
}
};
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getLinkToVideo() {
String newLink;
if (linkToVideo.contains(" ")) {
newLink = linkToVideo.replaceAll(" ", "");
return newLink;
}
return linkToVideo;
}
public void setLinkToVideo(String linkToVideo) {
this.linkToVideo = linkToVideo;
}
public String getThumbnailLink() {
String fixedThumb=thumbnailLink;
if (thumbnailLink.contains(" ")) {
fixedThumb = thumbnailLink.replaceAll(" ", "");
return fixedThumb;
}
return thumbnailLink;
}
public void setThumbnailLink(String thumbnailLink) {
this.thumbnailLink = thumbnailLink;
}
#Override
public String toString() {
return "YoutubeVideo{" +
"title='" + getTitle() + '\'' +
", linkToVideo='" + getLinkToVideo() + '\'' +
", thumbnailLink='" + getThumbnailLink() + '\'' +
'}';
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(title);
dest.writeString(linkToVideo);
dest.writeString(thumbnailLink);
}
}
the adapter:
public class PlaylistAdapter extends
ExpandableRecyclerViewAdapter<PlaylistViewHolder, VideoViewHolder> {
public PlaylistAdapter(List<? extends ExpandableGroup> groups) {
super(groups);
}
#Override
public PlaylistViewHolder onCreateGroupViewHolder(ViewGroup parent, int
viewType) {
View view =
LayoutInflater.from(parent.getContext()).inflate(R.layout.playlist_item,
parent, false);
return new PlaylistViewHolder(view);
}
#Override
public VideoViewHolder onCreateChildViewHolder(ViewGroup parent, int
viewType) {
View view =
LayoutInflater.from(parent.getContext()).inflate(R.layout.video_item,
parent,
false);
return new VideoViewHolder(view);
}
#Override
public void onBindChildViewHolder(VideoViewHolder holder, int flatPosition,
ExpandableGroup group, int childIndex) {
YoutubeVideo video = (YoutubeVideo) group.getItems().get(childIndex);
holder.onBind(video);
}
#Override
public void onBindGroupViewHolder(PlaylistViewHolder holder, int
flatPosition, ExpandableGroup group) {
holder.setCurrentPlaylist((YoutubePlaylist) group);
}
}
main activity:
public class MainActivity extends AppCompatActivity {
RecyclerView videoListView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GetJsonFromUrl getJsonService = RetrofitInstance.getRetrofitInstance().create(GetJsonFromUrl.class);
Call<JsonObject> call = getJsonService.getJSON();
call.enqueue(new Callback<JsonObject>() {
#Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
if(response.isSuccessful()){
JsonArray array = response.body().getAsJsonArray("Playlists");
Gson gson = new Gson();
Type type = new TypeToken<ArrayList<YoutubePlaylist>>(){}.getType();
ArrayList<YoutubePlaylist> youtubePlay = gson.fromJson(array, type);
for(YoutubePlaylist playlist : youtubePlay){
ArrayList<YoutubeVideo> videos = playlist.getVideos();
Log.e("###",videos.toString());
}
setArrayToAdapter(youtubePlay);
}
}
#Override
public void onFailure(Call<JsonObject> call, Throwable t) {
}
});
}
private void setArrayToAdapter(ArrayList<YoutubePlaylist> youtubePlay) {
videoListView = findViewById(R.id.videosView);
LinearLayoutManager manager = new LinearLayoutManager(this);
PlaylistAdapter adapter = new PlaylistAdapter(youtubePlay);
videoListView.setLayoutManager(manager);
videoListView.setAdapter(adapter);
}
}
playlist object output example:
YoutubePlaylist{title='Zen Work Music', videos=[YoutubeVideo{title='HEALING ZEN Music', linkToVideo='https://www.youtube.com/watch?v=SbCpzWMWb68', thumbnailLink='https://i.ytimg.com/vi_webp/SbCpzWMWb68/mqdefault.webp'}, YoutubeVideo{title='Relaxing Music - Meditation', linkToVideo='https://www.youtube.com/watch?v=qrx1vyvtRLY', thumbnailLink='https://i.ytimg.com/vi_webp/qrx1vyvtRLY/mqdefault.webp'}, YoutubeVideo{title='Relaxing Music - Background', linkToVideo='https://www.youtube.com/watch?v=loIZy6GqhUw', thumbnailLink='https://i.ytimg.com/vi_webp/loIZy6GqhUw/mqdefault.webp'}, YoutubeVideo{title='Delta Waves Sleep Music', linkToVideo='https://www.youtube.com/watch?v=EshmcHB3yMg', thumbnailLink='https://i.ytimg.com/vi_webp/EshmcHB3yMg/mqdefault.webp'}]}
im getting the groups binded and shown on list, but nothing happens onClick and i did'nt see anything regarding onClickListener in the documentation.
Thanks in advance!
The issue with this is that from the documentation here you don't have to pass your custom model YoutubePlaylist.java as a parameter as you did rather you replace it with ExpandableGroup so for your code
public class PlaylistViewHolder extends GroupViewHolder {
private TextView mPlaylistName, mPlaylistCount;
private ImageView arrow;
ExpandableListView.OnGroupClickListener listener;
public PlaylistViewHolder(View itemView) {
super(itemView);
mPlaylistName = itemView.findViewById(R.id.playlistNameView);
mPlaylistCount = itemView.findViewById(R.id.videosCount);
arrow = itemView.findViewById(R.id.expandBtn);
}
public void setCurrentPlaylist(ExpandableGroup playlist){
//the parameter has to be ExpandableGroup class and you can only get title
Log.e("###",playlist.getTitle());//you must call the getTitle() method
mPlaylistName.setText(playlist.getListTitle());
mPlaylistCount.setText("5");
}
}
This could be the way it was built. maybe a workaround this may be for
this method to then accept two parameters which is the ExpandableGroup
class and the custom model, class
public void setCurrentPlaylist(ExpandableGroup playlist, YoutubePlaylist playlist2){
//the parameter has to be ExpandableGroup class and you can only get title
Log.e("###",playlist.getTitle());//you must call the getTitle() method
mPlaylistName.setText(playlist2.getListTitle());
mPlaylistCount.setText("5");
}
In my application I should load data from server and for this job I use Retrofit library.
In my application i want load string data from server and i should load images from drawable folder .
I can load string from server and show it on textview, but when add images i don't know how can i it?!
DataModel:
public class Retrofit_ColoniesModel {
//Load from server
#SerializedName("id")
private Integer id;
#SerializedName("slug")
private String slug;
#SerializedName("title")
private String title;
#SerializedName("description")
private String description;
#SerializedName("parent")
private Integer parent;
#SerializedName("post_count")
private Integer post_count;
//Load from local
private int[] image;
public Retrofit_ColoniesModel(Integer id, String slug, String title, String description, Integer parent, Integer post_count,
int[] image) {
this.id = id;
this.slug = slug;
this.title = title;
this.description = description;
this.parent = parent;
this.post_count = post_count;
this.image = image;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getSlug() {
return slug;
}
public void setSlug(String slug) {
this.slug = slug;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getParent() {
return parent;
}
public void setParent(Integer parent) {
this.parent = parent;
}
public Integer getPost_count() {
return post_count;
}
public void setPost_count(Integer post_count) {
this.post_count = post_count;
}
public int[] getImage() {
return image;
}
public void setImage(int[] image) {
this.image= image;
}
DataModelResponse:
public class Retrofit_ColoniesModelResponse {
#SerializedName("status")
private String status;
#SerializedName("count")
private int count;
#SerializedName("categories")
private List<Retrofit_ColoniesModel> categories;
public List<Retrofit_ColoniesModel> getCategories() {
return categories;
}
public void setCategories(List<Retrofit_ColoniesModel> categories) {
this.categories = categories;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
Retrofit code in activity:
// Retrofit //////////
Retrofit_ApiInterface apiInterface = Retrofit_ApiClient.getClient().create(Retrofit_ApiInterface.class);
Call<Retrofit_ColoniesModelResponse> call = apiInterface.getResponse();
call.enqueue(new Callback<Retrofit_ColoniesModelResponse>() {
#Override
public void onResponse(Call<Retrofit_ColoniesModelResponse> call, Response<Retrofit_ColoniesModelResponse> response) {
List<Retrofit_ColoniesModel> models = response.body().getCategories();
mAdaper = new ColoniesAdapter(context, models);
colonies_RecyclerView.setAdapter(mAdaper);
}
#Override
public void onFailure(Call<Retrofit_ColoniesModelResponse> call, Throwable t) {
}
});
//////////////////////
I want save images into Array, such as :
final int[] colImages = {
R.drawable.colonies_image_food,
R.drawable.colonies_image_medical,
R.drawable.colonies_image_tecgnolegy,
R.drawable.colonies_image_entertenement,
R.drawable.colonies_image_car,
R.drawable.colonies_image_model,
R.drawable.colonies_image_sport,
};
Adapter:
public class ColoniesAdapter extends RecyclerView.Adapter<ColoniesAdapter.ViewHolder> {
private List<Retrofit_ColoniesModel> mDateSet;
private Context mContext;
private SparseBooleanArray expandState = new SparseBooleanArray();
public ColoniesAdapter(Context context, List<Retrofit_ColoniesModel> dataSet) {
this.mContext = context;
this.mDateSet = dataSet;
for (int i = 0; i < mDateSet.size(); i++) {
expandState.append(i, false);
}
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.colonies_row, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.colonies_title.setText(mDateSet.get(position).getTitle());
holder.colonies_title.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int pos = holder.getPosition();
Retrofit_ColoniesModel model = mDateSet.get(pos);
v.getContext().startActivity(new Intent(v.getContext(), Category_page.class)
.putExtra("categoryTitle", model.getTitle())
.putExtra("categoryID", model.getId()));
}
});
Glide.with(mContext)
.load(mDateSet.get(position).getImage()[position])
.placeholder(R.drawable.post_image)
.crossFade()
.override(700, 400)
.into(holder.colonies_image);
holder.colonies_image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int pos = holder.getPosition();
Retrofit_ColoniesModel model = mDateSet.get(pos);
v.getContext().startActivity(new Intent(v.getContext(), Category_page.class)
.putExtra("categoryTitle", model.getTitle())
.putExtra("categoryID", model.getId()));
}
});
holder.colonies_description.setText(mDateSet.get(position).getDescription());
holder.colonies_count.setText("مطالب موجود در کلونی : " + mDateSet.get(position).getPost_count());
holder.expandableLayout.setInterpolator(mDateSet.get(position).getInterpolator());
holder.expandableLayout.setExpanded(expandState.get(position));
holder.expandableLayout.setListener(new ExpandableLayoutListenerAdapter() {
#Override
public void onPreOpen() {
createRotateAnimator(holder.buttonLayout, 0f, 180f).start();
expandState.put(position, true);
}
#Override
public void onPreClose() {
createRotateAnimator(holder.buttonLayout, 180f, 0f).start();
expandState.put(position, false);
}
});
holder.buttonLayout.setRotation(expandState.get(position) ? 180f : 0f);
holder.buttonLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
onClickButton(holder.expandableLayout);
}
});
}
private void onClickButton(final ExpandableLayout expandableLayout) {
expandableLayout.toggle();
}
#Override
public int getItemCount() {
return mDateSet.size();
}
public void remove(int position) {
mDateSet.remove(position);
notifyItemRemoved(position);
}
public void clear() {
mDateSet.clear();
notifyDataSetChanged();
}
public void add(List<Retrofit_ColoniesModel> models) {
mDateSet.addAll(models);
notifyDataSetChanged();
}
public void update(List<Retrofit_ColoniesModel> models) {
mDateSet.clear();
mDateSet.addAll(models);
notifyDataSetChanged();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private TextView colonies_title, colonies_description, colonies_count;
private ImageView colonies_image;
private ExpandableLinearLayout expandableLayout;
private RelativeLayout buttonLayout;
public ViewHolder(View itemView) {
super(itemView);
colonies_title = (TextView) itemView.findViewById(R.id.colonies_colony_title_text);
colonies_image = (ImageView) itemView.findViewById(R.id.colonies_cover_image);
colonies_description = (TextView) itemView.findViewById(R.id.colonies_expandable_description_text);
colonies_count = (TextView) itemView.findViewById(R.id.colonies_count_title_text);
buttonLayout = (RelativeLayout) itemView.findViewById(R.id.colonies_expandable_button);
expandableLayout = (ExpandableLinearLayout) itemView.findViewById(R.id.colonies_expandable_layout);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/* v.getContext().startActivity(new Intent(v.getContext(), PostShow_page.class)
.putExtra("title", model.getTitle())
.putExtra("image", model.getThumbnail()));*/
}
});
}
}
public ObjectAnimator createRotateAnimator(final View target, final float from, final float to) {
ObjectAnimator animator = ObjectAnimator.ofFloat(target, "rotation", from, to);
animator.setDuration(300);
animator.setInterpolator(Utils.createInterpolator(Utils.LINEAR_INTERPOLATOR));
return animator;
}
}
I don't know how can i add int[] into my constructor, because in retrofit fill the constructor with List<Retrofit_ColoniesModel> models = response.body().getCategories(); .
How can i fix my issue? I really need this tutorial, please help me. Thanks all <3
If the array of images is static (don't depend on the server) then you could just do the following:
public class Retrofit_ColoniesModel {
...
private static final int[] colImages = {
R.drawable.colonies_image_food,
R.drawable.colonies_image_medical,
R.drawable.colonies_image_tecgnolegy,
R.drawable.colonies_image_entertenement,
R.drawable.colonies_image_car,
R.drawable.colonies_image_model,
R.drawable.colonies_image_sport,
};
But if this array is not static (depends on the server response) then I suggest you to map to an array of Strings that describe each image, and the server should respond with those Strings, for example:
"images":["IMAGE1", "IMAGE2"]
And then have a helper class that could map between those string keys to the actual R.drawable resources.
Let me know if you need sample code.
Non-static Nested Classes (Inner Classes)
Non-static nested classes in Java are also called inner classes. Inner classes are associated with an instance of the enclosing class. Thus, you must first create an instance of the enclosing class to create an instance of an inner class. Here is an example inner class definition:
public class Outer {
public class Inner {
}
}
Here is how you create an instance of the Inner class:
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
Notice how you put new after the reference to the outer class in order to create an instance of the inner class.
Non-static nested classes (inner classes) have access to the fields of the enclosing class, even if they are declared private. Here is an example of that:
I have a JSON nested object and i have been trying to pass for example, stations "name" to next activity in the onClick method. 2 questions:
1) Have i set my model class right or is there a better way?
2) What steps am i missing to pass "name" from stations from the specific object clicked onto the next activity?
My error for code provided (
java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
at com.example.ogure.trolleyproject.Adapter.StationsAdapter$ViewHolder.onClick(StationsAdapter.java:52)
Here is my JSON nested object
{
id: 1,
name: "Bus 1",
code: "TROL1",
tag_uid: "2HF4780H240827H40F284",
last_spotted_at: "2015-09-25 01:20:30",
last_spotted_station: 1,
stations: [
{
id: 1,
name: "Hennings Hall Station", //WHAT i want to pass
lat: "40.000000",
long: "41.000000",
_pivot_bus_id: 1,
_pivot_station_id: 1
}
}
Here is my model class
public class Bus implements Serializable {
#SerializedName("id")
private int mStationId;
#SerializedName("name")
private String mStationName;
#SerializedName("code")
private String mStationCode;
#SerializedName("tag_uid")
private String mStationTag_uid;
#SerializedName("last_spotted_at")
private String mStationLast_spot_time;
#SerializedName("last_spotted_station")
private int mStationLast_spot_station;
#SerializedName("stations")
private List<stations> stationInfo;
public int getmStationId() {
return mStationId;
}
public void setmStationId(int mStationId) {
this.mStationId = mStationId;
}
public List<stations> getStationInfo() {
return stationInfo;
}
public void setStationInfo(List<stations> stationInfo) {
this.stationInfo = stationInfo;
}
public int getmStationLast_spot_station() {
return mStationLast_spot_station;
}
public void setmStationLast_spot_station(int mStationLast_spot_station) {
this.mStationLast_spot_station = mStationLast_spot_station;
}
public String getmStationLast_spot_time() {
return mStationLast_spot_time;
}
public void setmStationLast_spot_time(String mStationLast_spot_time) {
this.mStationLast_spot_time = mStationLast_spot_time;
}
public String getmStationTag_uid() {
return mStationTag_uid;
}
public void setmStationTag_uid(String mStationTag_uid) {
this.mStationTag_uid = mStationTag_uid;
}
public String getmStationCode() {
return mStationCode;
}
public void setmStationCode(String mStationCode) {
this.mStationCode = mStationCode;
}
public String getmStationName() {
return mStationName;
}
public void setmStationName(String mStationName) {
this.mStationName = mStationName;
}
public class stations {
#SerializedName("id")
private int stationID;
#SerializedName("name")
private String stationName;
#SerializedName("lat")
private double stationLat;
#SerializedName("long")
private double stationLong;
#SerializedName("_pivot_bus_id")
private int mStationPiviotBusId;
#SerializedName("_pivot_station_id")
private int mStationPiviotStationId;
public int getmStationPiviotStationId() {
return mStationPiviotStationId;
}
public void setmStationPiviotStationId(int mStationPiviotStationId) {
this.mStationPiviotStationId = mStationPiviotStationId;
}
public int getmStationPiviotBusId() {
return mStationPiviotBusId;
}
public void setmStationPiviotBusId(int mStationPiviotBusId) {
this.mStationPiviotBusId = mStationPiviotBusId;
}
public double getStationLong() {
return stationLong;
}
public void setStationLong(double stationLong) {
this.stationLong = stationLong;
}
public double getStationLat() {
return stationLat;
}
public void setStationLat(double stationLat) {
this.stationLat = stationLat;
}
public String getStationName() {
return stationName;
}
public void setStationName(String stationName) {
this.stationName = stationName;
}
public int getStationID() {
return stationID;
}
public void setStationID(int stationID) {
this.stationID = stationID;
}
}
}
Here is my adapter class
public class StationsAdapter extends RecyclerView.Adapter<StationsAdapter.ViewHolder> {
private static List<Bus> mStations;
private static List<Bus.stations> mBusStations;
public StationsAdapter(List<Bus> stations) {
mStations = stations;
}
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView TvStationName;
public ImageView IvImageView;
public CardView mCardView;
public ViewHolder(View itemView) {
super(itemView);
TvStationName = (TextView) itemView.findViewById(R.id.station_name);
IvImageView = (ImageView) itemView.findViewById(R.id.station_photo);
mCardView = (CardView) itemView.findViewById(R.id.cv);
mCardView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
int position = getAdapterPosition();
Bus s = mStations.get(position);
Bus.stations og = mBusStations.get(position); //HERE
Intent i = new Intent(v.getContext(), StationActivity.class);
i.putExtra("station_name", og.getStationName()); //and HERE is my problem
v.getContext().startActivity(i);
}
}
#Override
public StationsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.rv_card_item, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
#Override
public void onBindViewHolder(StationsAdapter.ViewHolder holder, int position) {
Bus stationPos = mStations.get(position);
holder.TvStationName.setText(stationPos.getmStationName());
holder.mCardView.setTag(position);
}
#Override
public int getItemCount() {
return mStations.size();
}
Change your code to following:
public StationsAdapter(List<Bus> stations) {
List<Bus.stations> busStations = new ArrayList<>();
for (Bus bus : stations) {
busStations.addAll(bus.getStationInfo());
}
}
Issue is that you don't assign nothing to your mBusStations variable, that's why NPE throws.
Btw, don't use that confusing naming. Im talking about stationInfo in your Bus class.