Can't convert object of type java.lang.String to type sort.media.sound.audio.workshoplecture2.Contacts
This is the error I get.
import com.squareup.picasso.Picasso;
import de.hdodenhof.circleimageview.CircleImageView;
public class FindFriendActivity extends AppCompatActivity {
private Toolbar mToolBar;
private RecyclerView FindFriendsRecylerList;
private DatabaseReference UsersRef;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_friend);
UsersRef= FirebaseDatabase.getInstance().getReference().child("Users");
FindFriendsRecylerList=(RecyclerView) findViewById(R.id.find_friends_recycler_lists);
FindFriendsRecylerList.setLayoutManager(new LinearLayoutManager(this));
mToolBar=(Toolbar) findViewById(R.id.find_friends_toolbar);
setSupportActionBar(mToolBar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setTitle("Find Friends");
}
#Override
public void onStart()
{
super.onStart();
FirebaseRecyclerOptions<Contacts> options=new FirebaseRecyclerOptions.Builder<Contacts>
().setQuery(UsersRef,Contacts.class).build();
FirebaseRecyclerAdapter<Contacts,FindFriendsViewHolder> adapter=new FirebaseRecyclerAdapter<Contacts, FindFriendsViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull FindFriendsViewHolder holder, int position, #NonNull Contacts model) {
model=new Contacts();
holder.userName.setText(model.getName());
holder.userStatus.setText(model.getStatus());
Picasso.get().load(model.getImage()).into(holder.profileImage);
}
#NonNull
#Override
public FindFriendsViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.users_display_layout,viewGroup,false);
FindFriendsViewHolder findFriendsViewHolder=new FindFriendsViewHolder(view);
return findFriendsViewHolder;
}
};
FindFriendsRecylerList.setAdapter(adapter);
adapter.startListening();
}
public static class FindFriendsViewHolder extends RecyclerView.ViewHolder
{
TextView userName,userStatus;
CircleImageView profileImage;
public FindFriendsViewHolder(#NonNull View itemView) {
super(itemView);
userName=itemView.findViewById(R.id.users_profile_name);
userStatus=itemView.findViewById(R.id.users_status);
profileImage=itemView.findViewById(R.id.users_profile_image);
}
}
}
My Contacts Class
package sort.media.sound.audio.workshoplecture2;
public class Contacts {
String name;
String image;
String status;
public Contacts()
{
}
public Contacts(String name, String image, String status) {
this.name = name;
this.image = image;
this.status = status;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
Related
I'm a beginner in android development . i have created an app that display data(image and text ) from Firebase in RecyclerView . everything is working fine , there is also one data (url) and i want when the user will click on any item it will start new activity using Intent and will display this url in a Webview . i have created new activity and set up the WebView in it . i just don't know how to pass the url from firebase to this activity . i was searching for 3 days and i did know that i need to use setOnClickItemListener but i didn't find anything realted to my case . that's why i thought about posting it here .
sorry for my English . thank you
my Firebase data is like this
enter image description here
this is model.java
public class model {
String title, image, description ,url;
public model() {
this.url=url ;
this.title = title;
this.image = image;
this.description = description;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getTitle() {
return title;
}
public String getImage() {
return image;
}
public String getDescription() {
return description;
}
public void setTitle(String title) {
this.title = title;
}
public void setImage(String image) {
this.image = image;
}
public void setDescription(String description) {
this.description = description;
}
}
and this is the RecyclerAdapter.java
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder>{
private Context mContext;
private List<model> modelList;
public RecyclerAdapter(Context context, List<model> listData) {
this.modelList = listData;
this.mContext = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.row,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.title.setText(modelList.get(position).getTitle());
holder.description.setText((modelList.get(position).getDescription()));
Picasso.get().load(modelList.get(position).getImage()).into(holder.imageView);
}
#Override
public int getItemCount() {
return modelList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView title,description;
ImageView imageView;
public ViewHolder(View itemView){
super(itemView);
title=itemView.findViewById(R.id.Title);
description=itemView.findViewById(R.id.Description);
imageView=itemView.findViewById(R.id.ImageView);
}
}
}
and this is MainActivity.java
public class MainActivity extends AppCompatActivity {
private DatabaseReference reference ;
private StorageReference mStorageRef ;
private RecyclerView recyclerView;
private ArrayList<model> modelList;
private Context mContext = MainActivity.this;
private RecyclerAdapter recyclerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayoutManager layoutManager= new LinearLayoutManager(this );
recyclerView=findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
reference = FirebaseDatabase.getInstance().getReference();
mStorageRef = FirebaseStorage.getInstance().getReference();
modelList = new ArrayList<>();
init();
}
private void init(){
clearAll();
Query query = reference.child("Data");
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
model rmodel = new model();
rmodel.setImage(snapshot.child("image").getValue().toString());
rmodel.setTitle(snapshot.child("title").getValue().toString());
rmodel.setUrl(snapshot.child("url").getValue().toString());
rmodel.setDescription(snapshot.child("description").getValue().toString());
modelList.add(rmodel);
}
recyclerAdapter = new RecyclerAdapter(mContext , modelList);
recyclerView.setAdapter(recyclerAdapter);
recyclerAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
private void clearAll(){
if (modelList !=null){
modelList.clear();
if(recyclerAdapter != null)
recyclerAdapter.notifyDataSetChanged();
}
modelList = new ArrayList<>();
}
}
In the onBindViewHolder method:
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.title.setText(modelList.get(position).getTitle());
holder.description.setText((modelList.get(position).getDescription()));
Picasso.get().load(modelList.get(position).getImage()).into(holder.imageView);
holder.addOnClickListener(new OnClickListener() {
#override
public void onClick(View v) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(modelList.get(position).getUrl()));
startActivity(browserIntent);
}
});
}
This will add an action listener on the holder to open the URL in a browser.
I want to write a program to retrieve information from the server and display it in the Recycler View,But I have two problems.
When I add data to the table it will be added to the Recycler View list But when I delete, the list doesn't change.
2.Photos of each section cannot be loaded.
home.java
public class Home extends Fragment {
public Home() {
// Required empty public constructor
}
SwipeRefreshLayout srl;
RecyclerView rv;
FloatingActionButton add;
ArrayList<Post> al;
PostAdapter pa;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
srl = getActivity().findViewById(R.id.srl);
rv = getActivity().findViewById(R.id.rv);
// add = getActivity().findViewById(R.id.add);
AndroidNetworking.initialize(getContext());
al = new ArrayList<>();
pa = new PostAdapter(getContext(),al);
rv.setHasFixedSize(true);
rv.setItemAnimator(new DefaultItemAnimator());
rv.setLayoutManager(new LinearLayoutManager(getContext()));
rv.setAdapter(pa);
update();
srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
update();
}
});
}
private void update(){
String id = al.size() == 0 ? "0" : al.get(0).getId();
AndroidNetworking.post(Urls.host+Urls.post)
.addBodyParameter("id",id)
.build()
.getAsObjectList(Post.class, new ParsedRequestListener<List<Post>>() {
#Override
public void onResponse(List<Post> response) {
srl.setRefreshing(false);
for (Post p : response){
al.add(0,p);
pa.notifyDataSetChanged();
}
}
#Override
public void onError(ANError anError) {
srl.setRefreshing(false);
}
});
}
}
postadapter.java
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.ViewHolder> {
Context context;
ArrayList<Post> al;
LayoutInflater inflater;
public PostAdapter(Context ctx,ArrayList<Post> arl){
context = ctx;
al = arl;
inflater = LayoutInflater.from(context);
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.rv_item,null,false);
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(lp);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
final Post s = al.get(position);
holder.title.setText(s.getTitle());
holder.text.setText(s.getText());
holder.date.setText(s.getDate());
holder.image.setImageUrl(Urls.host+s.getImage());
holder.image.setDefaultImageResId(R.drawable.material);
holder.image.setErrorImageResId(R.drawable.material);
}
#Override
public int getItemCount() {
return al.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
ANImageView image;
AppCompatTextView title, text, date;
AppCompatImageView share;
public ViewHolder(View itemView) {
super(itemView);
image = itemView.findViewById(R.id.image);
title = itemView.findViewById(R.id.title);
text = itemView.findViewById(R.id.text);
date = itemView.findViewById(R.id.date);
}
}
}
post.java
public class Post {
String title,text, date,id;
int imageUrl;
public Post(int imageUrl, String title, String text, String time, String id) {
this.imageUrl = imageUrl;
this.title = title;
this.text = text;
this.date = time;
this.id = id;
}
public int getImage() {
return imageUrl;
}
public void setImage(int imageUrl) {
this.imageUrl = imageUrl;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
Try this
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.ViewHolder> {
Context context;
ArrayList<Post> al;
ArrayList<Post> newDataList;
LayoutInflater inflater;
public PostAdapter(Context ctx,ArrayList<Post> arl){
context = ctx;
al = arl;
inflater = LayoutInflater.from(context);
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.rv_item,null,false);
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(lp);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
final Post s = al.get(position);
holder.title.setText(s.getTitle());
holder.text.setText(s.getText());
holder.date.setText(s.getDate());
holder.image.setImageUrl(Urls.host+s.getImage());
holder.image.setDefaultImageResId(R.drawable.material);
holder.image.setErrorImageResId(R.drawable.material);
}
#Override
public int getItemCount() {
return al.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
ANImageView image;
AppCompatTextView title, text, date;
AppCompatImageView share;
public ViewHolder(View itemView) {
super(itemView);
image = itemView.findViewById(R.id.image);
title = itemView.findViewById(R.id.title);
text = itemView.findViewById(R.id.text);
date = itemView.findViewById(R.id.date);
}
public ArrayList<Post> getDataSet() {
return al;
}
public void refresh(ArrayList<Post> list) {
newDataList = new ArrayList<>();
newDataList.addAll(list);
al.clear();
al.addAll(newDataList);
notifyDataSetChanged();
}
}
Now at time when you wants to add or delete try getting adapter.getDataSet()
and update list with adapter.refresh(yourList)
I want to show a list of group chats in a TabLayout fragment. So, in order to do that, I used FirebaseRecyclerAdapter as my adapter and put it on RecyclerView.
These are my dependencies:
implementation 'com.firebaseui:firebase-ui-database:4.1.0'
implementation 'com.google.firebase:firebase-database:16.0.1'
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.android.support:support-v4:27.1.1
This is my firebase database structure
This is my model:
public class ChatConv {
private String image, last_msg, name, online_status;
public ChatConv() {
}
public ChatConv(String image, String last_msg, String name, String online_status) {
this.image = image;
this.last_msg = last_msg;
this.name = name;
this.online_status = online_status;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getLast_msg() {
return last_msg;
}
public void setLast_msg(String last_msg) {
this.last_msg = last_msg;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getOnline_status() {
return online_status;
}
public void setOnline_status(String online_status) {
this.online_status = online_status;
}
}
I have created an adapter class that extends the FirebaseRecyclerAdapter and this is my code:
public class ChatConvAdapter extends FirebaseRecyclerAdapter<ChatConv, ChatConvAdapter.ViewHolder> {
private static final String TAG = "ChatConvAdapter";
public ChatConvAdapter(#NonNull FirebaseRecyclerOptions<ChatConv> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull ViewHolder holder, int position, #NonNull ChatConv model) {
holder.bind(model);
Log.i(TAG, "onBind is called");
Log.i(TAG, "name= " + model.getName());
Log.i(TAG, "msg= " + model.getLast_msg());
Log.i(TAG, "image= " + model.getImage());
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.row_item_global_list, parent, false);
Log.i(TAG, "onCreate is called");
return new ViewHolder(view);
}
class ViewHolder extends RecyclerView.ViewHolder {
#BindView(R.id.item_image)
CircleImageView imageView;
#BindView(R.id.item_name)
TextView nameView;
#BindView(R.id.item_desc)
TextView msgView;
#BindView(R.id.item_time)
TextView timeView;
ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
public void bind(ChatConv chatConv) {
String name = chatConv.getName();
String image = chatConv.getImage();
String msg = chatConv.getLast_msg();
nameView.setText(name);
msgView.setText(msg);
Picasso.get().load(image).noFade().into(imageView);
}
}
}
In my fragment, I initialized my adapter and set the adapter inside the onCreateView()
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.layout_chat_list, container, false);
unbinder = ButterKnife.bind(this, view);
initObjects();
setUpRecyclerView();
setUpAdapter();
return view;
}
private void setUpAdapter() {
Query query = mWorldChatRef.orderByKey();
FirebaseRecyclerOptions<ChatConv> options = new FirebaseRecyclerOptions.Builder<ChatConv>()
.setQuery(query, ChatConv.class).build();
mAdapter = new ChatConvAdapter(options);
listChatView.setAdapter(mAdapter);
}
And startListening/stopListening in onStart()/onStop();
#Override
public void onStart() {
super.onStart();
mAdapter.startListening();
}
#Override
public void onStop() {
super.onStop();
mAdapter.stopListening();
}
I've searched for the possible answer here in StackOverflow but it didn't work for my case. Why am not getting logs? Am I missing something?
I have found my own solution by adding this line to the class that extends Application.
#Override
public void onCreate() {
super.onCreate();
Timber.plant(new Timber.DebugTree());
FirebaseDatabase instance = FirebaseDatabase.getInstance();
instance.setPersistenceEnabled(true);
}
I'm trying to show Blog Posts on my MainActivity (Home page). But Homepage Nothing shows I mean Homepage is empty.
Firebase database structure:
MainActivity:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private RecyclerView mBlogList;
private DatabaseReference mDataRef;
private FirebaseRecyclerAdapter<BlogInfo, ViewHolder> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate: started!");
mDataRef = FirebaseDatabase.getInstance().getReference().child("BlogApp").child("Posts");
mBlogList = findViewById(R.id.blogListId);
mBlogList.setHasFixedSize(true);
mBlogList.setLayoutManager(new LinearLayoutManager(this));
FirebaseRecyclerOptions<BlogInfo> options = new FirebaseRecyclerOptions.Builder<BlogInfo>()
.setQuery(mDataRef, BlogInfo.class).build();
adapter = new FirebaseRecyclerAdapter<BlogInfo, ViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull ViewHolder holder, int position, #NonNull BlogInfo model) {
String imgUrl = model.getImage();
Picasso.get().load(imgUrl).into(holder.post_image);
holder.post_title.setText(model.getTitle());
holder.post_desc.setText(model.getDescription());
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
Log.d(TAG, "onCreateViewHolder: initialized!");
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_blog_item, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
};
mBlogList.setAdapter(adapter);
}
#Override
protected void onStart() {
super.onStart();
adapter.startListening();
Log.d(TAG, "onStart: init");
}
#Override
protected void onStop() {
super.onStop();
adapter.stopListening();
Log.d(TAG, "onStop: stop Blog list");
}
// ViewHolder Class
public static class ViewHolder extends RecyclerView.ViewHolder{
private static final String TAG = "ViewHolder";
TextView post_title;
TextView post_desc;
ImageView post_image;
ViewHolder(View itemView) {
super(itemView);
Log.d(TAG, "ViewHolder: Initialized!");
post_title = itemView.findViewById(R.id.postTitleId);
post_desc = itemView.findViewById(R.id.postDescId);
post_image = itemView.findViewById(R.id.postImageId);
}
}
}
BlogInfo.class:
public class BlogInfo {
private String description;
private String image;
private String title;
public BlogInfo() {}
public BlogInfo(String title, String description, String image) {
this.title = title;
this.description = description;
this.image = image;
}
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 String getImage() {return image;}
public void setImage(String image) {this.image = image;}
}
This is my first time I'm trying to use FirebaseRecyclerAdapter to retrieve data from Firebase Database. But I don't know where the problem is. I tried to debug also but the compiler won't enter into onCreateViewHolder or onBindViewHolder function. But I'm totally following FirebaseUI for Realtime Database reference.
To solve this, you need to remove the following line of code:
mBlogList.setHasFixedSize(true);
This is needed only when you want your RecyclerView to have a fixed size. In your case is not necessary.
Currently im learning android studio by making an application chat, but i'm stuck in one situation.
i want the app show all user that registered in firebase by using recyclerview, but here's the thing, the activity is working fine, but no user in the activity
build.gradle
implementation 'com.google.firebase:firebase-auth:11.8.0'
implementation 'com.google.firebase:firebase-database:11.8.0'
implementation 'com.google.firebase:firebase-storage:11.8.0'
implementation 'com.firebaseui:firebase-ui-database:3.2.2'
AllFriendActivity.java
public class AllFriendActivity extends AppCompatActivity {
private DatabaseReference mDatabaseReference;
private Toolbar mToolbar;
private RecyclerView mFriendList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_all_friend);
mToolbar = (Toolbar) findViewById(R.id.all_friend_toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle("All Friend");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("User");
mFriendList = (RecyclerView) findViewById(R.id.friend_list);
mFriendList.setHasFixedSize(true);
mFriendList.setLayoutManager(new LinearLayoutManager(this));
}
#Override
protected void onStart() {
super.onStart();
FirebaseRecyclerOptions<Friend> options = new FirebaseRecyclerOptions.Builder<Friend>()
.setQuery(mDatabaseReference, Friend.class)
.build();
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Friend, FriendViewHolder>(options) {
#NonNull
#Override
public FriendViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.friend_layout, parent, false);
Toast.makeText(AllFriendActivity.this, "Test Toast", Toast.LENGTH_SHORT).show();
return new FriendViewHolder(view);
}
#Override
protected void onBindViewHolder(#NonNull FriendViewHolder holder, int position, #NonNull Friend model) {
holder.setName(model.getName());
holder.setStatus(model.getStatus());
}
};
mFriendList.setAdapter(adapter);
}
public static class FriendViewHolder extends RecyclerView.ViewHolder {
View mView;
public FriendViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setName(String name){
TextView friendDisplayName = (TextView) mView.findViewById(R.id.friend_display_name);
friendDisplayName.setText(name);
}
public void setStatus(String status){
TextView friendStatus = (TextView) mView.findViewById(R.id.friend_status);
friendStatus.setText(status);
}
}
}
Friend.java
public class Friend {
String name;
String status;
String avatar;
public Friend() {
}
public Friend(String name, String status, String avatar) {
this.name = name;
this.status = status;
this.avatar = avatar;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getAvatar() {
return avatar;
}
public void setAvatar(String avatar) {
this.avatar = avatar;
}
}
i've search the solution in internet, but still cant fix the problem