Everything works as in other projects but for some reason I'm unable to set the projectListItems ( a recycler view ) adapter because it won't resolve setAdapter()...
binding.projectListItems.setAdapter(adapter); in the ProjectsActivity.java
Cleaned and Rebuilt project... also Invalidated cache and restarted.
ProjectsActivity.java
package com.example.poleprofilingapp.ui;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import com.example.poleprofilingapp.R;
import com.example.poleprofilingapp.databinding.ActivityProjectsBinding;
import com.example.poleprofilingapp.project.Project;
import com.example.poleprofilingapp.project.ProjectAdapter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class ProjectsAcitivty extends AppCompatActivity {
private ProjectAdapter adapter;
private ActivityProjectsBinding binding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_projects);
Intent intent = getIntent();
List <Project> projectsList = new ArrayList<>();
Project project = new Project();
project.setName("Project 1");
project.setAddress("Address 1");
project.setDescription("Test Project 1");
project.setType("NOVEC");
project.setCompanyKey("333555");
projectsList.add(project);
binding = DataBindingUtil.setContentView(this,
R.layout.activity_projects);
adapter = new ProjectAdapter(projectsList, this);
binding.projectListItems.setAdapter(adapter); // setAdapter not resoleved
}
}
ProjectAdaptor.java
package com.example.poleprofilingapp.project;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;
import com.example.poleprofilingapp.R;
import com.example.poleprofilingapp.databinding.ProjectListItemBinding;
import java.util.List;
public class ProjectAdapter extends RecyclerView.Adapter<ProjectAdapter.ViewHolder> {
private List<Project> projects;
private Context context;
public ProjectAdapter(List<Project> projects, Context context) {
this.projects = projects;
this.context = context;
}
#NonNull
#Override
public ProjectAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
ProjectListItemBinding binding = DataBindingUtil
.inflate(LayoutInflater.from(parent.getContext()),
R.layout.project_list_item,
parent,
false);
return new ViewHolder(binding);
}
#Override
public void onBindViewHolder(#NonNull ProjectAdapter.ViewHolder holder, int position) {
Project project = projects.get(position);
holder.projectListItemBinding.setProject(project);
}
#Override
public int getItemCount() {
return projects.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
// Binding Vars
public ProjectListItemBinding projectListItemBinding;
// Constructor to do view lookups for each subview
public ViewHolder(ProjectListItemBinding projectLayoutBinding){
super(projectLayoutBinding.getRoot());
projectListItemBinding = projectLayoutBinding;
}
}
}
Project.java ( data model )
package com.example.poleprofilingapp.project;
public class Project {
private String id;
private String name;
private String address;
private String description;
private String type;
private String companyKey;
public Project() {
}
public Project(String name, String address, String description, String type, String companyKey) {
this.name = name;
this.address = address;
this.description = description;
this.type = type;
this.companyKey = companyKey;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getCompanyKey() {
return companyKey;
}
public void setCompanyKey(String companyKey) {
this.companyKey = companyKey;
}
}
XML - activity_projects.xml
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="project"
type="com.example.poleprofilingapp.project.Project"/>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.ProjectsAcitivty">
<android.support.v7.widget.RecyclerView
android:id="#+id/projectListItems"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</android.support.v7.widget.RecyclerView>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
projects_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="project"
type="com.example.poleprofilingapp.project.Project"/>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/projectListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:paddingBottom="8dp"
tools:background="#color/colorPrimaryDark">
<TextView
android:id="#+id/name"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="#{project.name}"
android:textColor="#android:color/white"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Test Project"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Inside the onCreate intialize your bindiing variable
ActivityMainBinding mBinding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
mBinding = DataBindingUtil.setContentView(this,R.layout.activity_main);
}
In onStart method set you layoutManager and adapter
#Override
protected void onStart() {
super.onStart();
mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
UserAdapter adapter = new UserAdapter(getApplicationContext(),usersList());
mBinding.recyclerView.setAdapter(adapter);
}
and this is list
private List<Animals> usersList(){
List<Animals> animalsList = new ArrayList<>();
String[] animals = {"Tiger","Monkey","Dog","Lion","Zebra","Horse","Cat"};
String[] types = {"Carnivore","Herbivore","Omnivore","Carnivore","Herbivore","Herbivore","Omnivore"};
int[] image = {R.drawable.all_call,R.drawable.done,R.drawable.hide,R.drawable.all_call};
for (int i=0;i<animals.length;i++){
Animals users = new Animals(
animals[i],
types[i],
image[i]
);
animalsList.add(users);
}
return animalsList;
}
So the issue was that I migrated the project over to androix but never changed the RecyclerView to support androix method. So in the build folder my generated binding class was trying to us android.support.v7..... in place of androidx.recyclerview.....
So what fixed this was updating the XML then running Invalidating the Chache / Restart...
Whala all fixed up.
Related
MY PROBLEM -
my cardview is going over the screen(can be seen in the image) even if the says matchparent.
my database node
Please help....... is this a bug or smth?
What am i doing = i m trying to retrieve the data from firebase in a recycler view, the data is being retrieved but the item of the recyclerview is giving problem, as the cardview is half outside the screen even if the cardview is set to matchparent and wrapcontent please help if possible
My recycler Item
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/Cdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hi"
android:textSize="50dp"
android:layout_gravity="center"
android:layout_marginTop="20dp"/>
</androidx.cardview.widget.CardView>
my Adapterclass
package com.sjsbhints.pdfmanager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
public class CAdapter extends FirebaseRecyclerAdapter<Cmodel,CAdapter.myViewHolder> {
/**
* Initialize a {#link RecyclerView.Adapter} that listens to a Firebase query. See
* {#link FirebaseRecyclerOptions} for configuration options.
*
* #param options
*/
public CAdapter(#NonNull FirebaseRecyclerOptions<Cmodel> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull myViewHolder holder, int position, #NonNull Cmodel model) {
// if(model.Cdate.isEmpty()){
// holder.Cdate.setText("Null");
// }
holder.Cdate.setText(model.getCdate());
// holder.Cheading.setText(model.getCheading());
// holder.Cbody.setText(model.getCbody());
}
#NonNull
#Override
public myViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.circular_item,parent,false);
return new myViewHolder(view);
}
class myViewHolder extends RecyclerView.ViewHolder{
TextView Cdate,Cheading,Cbody,Ndate,Nheading,Nsheading,Nbody,Tdate,Tsubject,Ttopic,Ttdate;
public myViewHolder(#NonNull View itemView) {
super(itemView);
Cdate = itemView.findViewById(R.id.Cdate);
// Cheading = itemView.findViewById(R.id.cheading);
// Cbody = itemView.findViewById(R.id.cbody);
}
}
}
my model class
package com.sjsbhints.pdfmanager;
public class Cmodel {
String Cdate,Cheading,Cbody,Ndate,Nheading,Nsheading,Nbody,Tdate,Tsubject,Ttopic,Ttdate;
Cmodel(){
}
public Cmodel(String cdate, String cheading, String cbody, String ndate, String nheading, String nsheading, String nbody, String tdate, String tsubject, String ttopic, String ttdate) {
Cdate = cdate;
Cheading = cheading;
Cbody = cbody;
Ndate = ndate;
Nheading = nheading;
Nsheading = nsheading;
Nbody = nbody;
Tdate = tdate;
Tsubject = tsubject;
Ttopic = ttopic;
Ttdate = ttdate;
}
public String getCdate() {
return Cdate;
}
public void setCdate(String cdate) {
Cdate = cdate;
}
public String getCheading() {
return Cheading;
}
public void setCheading(String cheading) {
Cheading = cheading;
}
public String getCbody() {
return Cbody;
}
public void setCbody(String cbody) {
Cbody = cbody;
}
public String getNdate() {
return Ndate;
}
public void setNdate(String ndate) {
Ndate = ndate;
}
public String getNheading() {
return Nheading;
}
public void setNheading(String nheading) {
Nheading = nheading;
}
public String getNsheading() {
return Nsheading;
}
public void setNsheading(String nsheading) {
Nsheading = nsheading;
}
public String getNbody() {
return Nbody;
}
public void setNbody(String nbody) {
Nbody = nbody;
}
public String getTdate() {
return Tdate;
}
public void setTdate(String tdate) {
Tdate = tdate;
}
public String getTsubject() {
return Tsubject;
}
public void setTsubject(String tsubject) {
Tsubject = tsubject;
}
public String getTtopic() {
return Ttopic;
}
public void setTtopic(String ttopic) {
Ttopic = ttopic;
}
public String getTtdate() {
return Ttdate;
}
public void setTtdate(String ttdate) {
Ttdate = ttdate;
}
}
my MainActivity
package com.sjsbhints.pdfmanager;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.FirebaseDatabase;
public class CircularAct extends AppCompatActivity {
RecyclerView recyclerView;
CAdapter cAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_circular2);
recyclerView = findViewById(R.id.rv);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
FirebaseRecyclerOptions<Cmodel> options =
new FirebaseRecyclerOptions.Builder<Cmodel>()
.setQuery(FirebaseDatabase.getInstance().getReference().child("CircularData"), Cmodel.class)
.build();
cAdapter = new CAdapter(options);
recyclerView.setAdapter(cAdapter);
}
#Override
protected void onStart() {
super.onStart();
cAdapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
cAdapter.stopListening();
}
}
my mainactivity Xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CircularAct">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv"
android:layout_width="599dp"
android:layout_height="960dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Your CardView definitely matched the parent's width because it inherited the width of the RecyclerView since that's its parent.
Your RecyclerView, on the other hand, has a width of 599dp. The fixed size means that it will ignore the device's width. To match the device's width and height, set it to match_parent or use 0dp since you're using a constraint layout in your case.
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
I am trying to display items from my firebase realtime database, I have two nodes in my database, I have used the Log method on my Adapter class getItemCount() function, and it shows that there is 2, but it isn't displayed in my UI, the two-item views are getting generate but the content with the names and mob, isn't displayed.
Output in AVD
Database Screenshot
Main Function Code
package com.example.jamsecure;
import android.os.Bundle;
import android.widget.Toast;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.List;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
public class User_Select_Jam_Studio extends AppCompatActivity {
List<FetchData> fetchData;
RecyclerView recyclerView;
HelperAdapter helperAdapter;
DatabaseReference databaseReference;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user__select__jam__studio);
recyclerView=findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager((new LinearLayoutManager(this)));
fetchData=new ArrayList<>();
helperAdapter=new HelperAdapter(fetchData);
recyclerView.setAdapter(helperAdapter);
databaseReference= FirebaseDatabase.getInstance().getReference("Owners");
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot ds:dataSnapshot.getChildren())
{
FetchData data=ds.getValue(FetchData.class);
fetchData.add(data);
helperAdapter.notifyDataSetChanged();
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
Toast.makeText(User_Select_Jam_Studio.this,"Data base problem",Toast.LENGTH_LONG).show();
}
});
}
}
Adapter Class
package com.example.jamsecure;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class HelperAdapter extends RecyclerView.Adapter {
List<FetchData> fetchDataList;
public HelperAdapter(List<FetchData> fetchDataList) {
this.fetchDataList = fetchDataList;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.jamrooms,parent, false);
ViewHolderClass viewHolderClass = new ViewHolderClass(view);
return viewHolderClass;
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
ViewHolderClass viewHolderClass=(ViewHolderClass)holder;
FetchData fetchData=fetchDataList.get(position);
viewHolderClass.name.setText(fetchData.getName());
viewHolderClass.jamr.setText(fetchData.getJamrate());
viewHolderClass.mob.setText(fetchData.getMob());
}
#Override
public int getItemCount() {
int a =fetchDataList.size();
String b=Integer.toString(a);
Log.d("TAG",b);
return fetchDataList.size();
}
public class ViewHolderClass extends RecyclerView.ViewHolder{
TextView name,jamr,mob;
public ViewHolderClass(#NonNull View itemView) {
super(itemView);
name=itemView.findViewById(R.id.name);
jamr=itemView.findViewById(R.id.jamrate);
mob=itemView.findViewById(R.id.mob);
}
}
}
Fetch Data Class
package com.example.jamsecure;
public class FetchData {
String sname;
String jam_rate;
String phone;
public FetchData(){}
public FetchData( String sname, String jam_rate, String phone) {
this.sname = sname;
this.jam_rate = jam_rate;
this.phone = phone;
}
public void setSname(String sname) {
this.sname = sname;
}
public void setJam_rate(String jam_rate) {
this.jam_rate = jam_rate;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getName() {
return sname;
}
public String getJamrate() {
return jam_rate;
}
public String getMob() {
return phone;
}
}
Inflator Activity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/item_layout"
android:orientation="vertical"
android:visibility="visible"
tools:visibility="visible">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/name"
android:textColor="#000000"
android:textSize="40sp" />
<TextView
android:id="#+id/mob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/mob"
android:textColor="#000000"
android:textSize="40sp" />
<TextView
android:id="#+id/jamrate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/jam_rate"
android:textColor="#000000"
android:textSize="40sp" />
</LinearLayout>
Recycler View Activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/back6"
tools:context=".User_Select_Jam_Studio">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
The problem with your code lies in the fact that the names of the getters in your "FetchData" class are not correct. A getter for the "phone" field, for example, should be getPhone() and not getMob(). So your "FetchData" class should look like this:
public class FetchData {
private String jam_rate, location, phone, semail, sname;
public FetchData() {}
public FetchData(String jam_rate, String location, String phone, String semail, String sname) {
this.jam_rate = jam_rate;
this.location = location;
this.phone = phone;
this.semail = semail;
this.sname = sname;
}
public String getJam_rate() {
return jam_rate;
}
public void setJam_rate(String jam_rate) {
this.jam_rate = jam_rate;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getSemail() {
return semail;
}
public void setSemail(String semail) {
this.semail = semail;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
}
Please note that the setters and getters are not required. Setters are always optional because if there is no setter for a JSON property, the Firebase client will set the value directly onto the field. If you make the fields public, the getters are optional too. So a more simplified version for your class might look like this:
public class FetchData {
public String jam_rate, location, phone, semail, sname;
public FetchData() {}
public FetchData(String jam_rate, String location, String phone, String semail, String sname) {
this.jam_rate = jam_rate;
this.location = location;
this.phone = phone;
this.semail = semail;
this.sname = sname;
}
}
I hope someone there will help me solve my problem. I have an Android application that uses the fragment. I want to download data using Volley from a JSON file. The program does not throw out any errors but CardView does not display with the ordered data. I looked at other topics with a similar problem, but I sit on it for a long time and nothing good happens.
AdapterZabytki.java
package eu.aisen.kamil.miejskiprzewodnik;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.List;
import zabytki.Zabytki;
public class AdapterZabytki extends RecyclerView.Adapter<AdapterZabytki.ViewHolder> {
private List<Zabytek>list_data;
private Zabytki context;
public AdapterZabytki(List<Zabytek> list_data, Zabytki context) {
this.list_data = list_data;
this.context = context;
}
#Override
public AdapterZabytki.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
CardView view= (CardView) LayoutInflater.from(parent.getContext()).inflate(R.layout.obiectcard,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Zabytek zabytek = list_data.get(position);
Picasso.get()
.load(zabytek
.getImage_url())
.into(holder.img);
holder.txtname.setText(zabytek.getName());
}
#Override
public int getItemCount() {
return list_data.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private ImageView img;
private TextView txtname;
public ViewHolder(View view) {
super(view);
img=(ImageView)itemView.findViewById(R.id.info_image);
txtname=(TextView)itemView.findViewById(R.id.info_text);
}
}
}
Zabytek.java
package eu.aisen.kamil.miejskiprzewodnik;
public class Zabytek {
private String name;
private String image_url;
private String opis;
public Zabytek(String name, String image_url, String opis) {
this.name = name;
this.image_url = image_url;
this.opis = opis;
}
public String getName() {
return name;
}
public String getImage_url() {
return image_url;
}
public String getOpis() {
return opis;
}
}
Zabytki.java (fragment)
package zabytki;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import eu.aisen.kamil.miejskiprzewodnik.AdapterZabytki;
import eu.aisen.kamil.miejskiprzewodnik.R;
import eu.aisen.kamil.miejskiprzewodnik.Zabytek;
public class Zabytki extends Fragment {
private static final String HI = "https://wydzialedukacji.rzeszow.pl/testowy.json";
private ArrayList<Zabytek>list_data;
private AdapterZabytki mSensorAdapter;
private RecyclerView mRyclerView;
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.zabytki_fragment, container, false);
mRyclerView = view.findViewById(R.id.main_list);
mRyclerView.setHasFixedSize(true);
mRyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
list_data = new ArrayList<>();
getData();
mSensorAdapter = new AdapterZabytki(list_data, this);
mRyclerView.setAdapter(mSensorAdapter);
mSensorAdapter.notifyDataSetChanged();
return view;
}
private void getData() {
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, HI, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("results");
for(int i=0; i < jsonArray.length(); i++){
JSONObject object = jsonArray.getJSONObject(i);
String name = object.getString("name");
String image_url = object.getString("image_url");
String opis = object.getString("opis");
list_data.add(new Zabytek(name, image_url, opis));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
Volley.newRequestQueue(getActivity()).add(request);
}
}
zabytki_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView android:id="#+id/main_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
</android.support.v7.widget.RecyclerView>
obiectcard.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/card_view"
android:layout_margin="5dp"
card_view:cardCornerRadius="4dp">
<android.support.constraint.ConstraintLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp">
<ImageView
android:id="#+id/info_image"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="24dp"
android:layout_weight="1.0"
android:scaleType="centerCrop"
card_view:layout_constraintBottom_toBottomOf="parent"
card_view:srcCompat="#android:drawable/sym_def_app_icon"
tools:ignore="MissingConstraints" />
<TextView
android:id="#+id/info_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginEnd="16dp"
android:text="tytułzabytku"
card_view:layout_constraintEnd_toEndOf="parent"
card_view:layout_constraintTop_toBottomOf="#+id/info_image" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
I have 2 concerns about your code:
1. in onResponse, after you added all items to to list_data, you must call notifyDatasetChanged on RecyclerView adapter (AdapterZabytki)
2. Also, info_image in your ViewHolder seems to have zero height, because it has no top alignment and layout_height="0dp"
I am still wondering how to transfer this data to ZabytekDetailActivity.class
I have this:
mSensorAdapter.setListener(new AdapterZabytki.Listener() {
public void onClick(int position) {
ArrayList<Zabytek> object = new ArrayList<Zabytek>();
Intent intent = new Intent(getActivity(), ZabytekDetailActivity.class);
}
});
Now the question is how to attach the results from list_data so that they can be displayed in MonumentDetailActivity.class
I am populating a ListView of songs using Firebase database and Firebase-UI, all the dependencies are initialized correctly and the app is connected with the database but when it displays the list it doesn't display the text, just empty boxes like that:
When an item is added to the database then a box is added but it doesn't show the text. Here is the code:
Song class:
package com.example.gloriadesideri.animas;
public class Song
{
private String myName;
private String myURL;
private String myAuthor;
public Song(){
/*myName="";
myURL="";
myAuthor="";*/
}
public Song(String Author, String Song, String URL) {
this.myName=Song;
this.myURL=URL;
this.myAuthor=Author;
}
public String getName()
{
return myName;
}
public String getURL()
{
return myURL;
}
public String getAuthor()
{
return myAuthor;
}
public void setName(String name)
{
this.myName=name;
}
public void setURL ( String URL)
{
this.myURL=URL;
}
public void setAuthor(String author)
{
this.myAuthor=author;
}
}
Song Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="20dp"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/songName"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/author"/>
</LinearLayout>
Activity that should have the list view:
package com.example.gloriadesideri.animas;
import android.content.Intent;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.firebase.ui.database.FirebaseListAdapter;
import com.firebase.ui.database.FirebaseListOptions;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.List;
public class Canzoni extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener
{
//nav view parameters
private DrawerLayout cDrawerLayout;
private ActionBarDrawerToggle cToggle;
//list view parameters
private ListView mListView;
//firebase parameters
private FirebaseListAdapter mAdapter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_canzoni);
//nav view code
cDrawerLayout= (DrawerLayout) findViewById(R.id.draw_viewC);
cToggle= new ActionBarDrawerToggle(this, cDrawerLayout,R.string.Open, R.string.Close);
cDrawerLayout.addDrawerListener(cToggle);
cToggle.syncState();
ActionBar actionBar = getSupportActionBar();
if (actionBar != null)
actionBar.setDisplayHomeAsUpEnabled(true);
NavigationView cNavigationView = (NavigationView) findViewById(R.id.nav_viewC);
if (cNavigationView != null)
{
cNavigationView.setNavigationItemSelectedListener(this);
}
Query mQuery= FirebaseDatabase.getInstance().getReference().child("songs");
mListView= (ListView) findViewById(R.id.canzoni_list);
FirebaseListOptions<Song> mOptions= new FirebaseListOptions.Builder<Song>()
.setLayout(R.layout.song_layout)
.setQuery(mQuery, Song.class)
.setLifecycleOwner(this)
.build();
mAdapter= new FirebaseListAdapter <Song>(mOptions){
#Override
protected void populateView(View v, Song model, int position) {
TextView songName= v.findViewById(R.id.songName);
TextView songAuthor=v.findViewById(R.id.author);
songName.setText(model.getName());
songAuthor.setText(model.getAuthor());
}
};
mListView.setAdapter(mAdapter);
}
#Override
protected void onStart() {
super.onStart();
mAdapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
mAdapter.stopListening();
}
#Override
public boolean onOptionsItemSelected(MenuItem Item)
{
if(cToggle.onOptionsItemSelected(Item))
{
return true;
}
return super.onOptionsItemSelected(Item);
}
#Override
public boolean onNavigationItemSelected(MenuItem Item)
{
int id = Item.getItemId();
Intent intent;
if (id == R.id.preghiere)
{
intent= new Intent(this, Preghiere.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
this.startActivity(intent);
}
else if ( id== R.id.bans)
{
intent= new Intent(this, Bans.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
this.startActivity(intent);
}
else if (id== R.id.canzoni)
{
intent= new Intent(this, this.getClass());
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
this.startActivity(intent);
}
else if (id==R.id.calendario)
{
intent= new Intent(this, Calendario.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
this.startActivity(intent);
}
else if (id== R.id.per_riflettere)
{
intent= new Intent(this, perRiflettere.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
this.startActivity(intent);
}
else if( id== R.id.home)
{
intent= new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
this.startActivity(intent);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.draw_viewC);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
layout of the above activity
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Canzoni"
android:id="#+id/draw_viewC">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/canzoni_list">
</ListView>
<android.support.design.widget.NavigationView
android:layout_width="239dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/white"
android:fitsSystemWindows="true"
app:headerLayout="#layout/header"
app:itemIconTint="#color/black"
app:itemTextColor="#color/black"
app:menu="#menu/drawem_menu"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp"
tools:ignore="MissingConstraints"
android:id="#+id/nav_viewC"/>
</android.support.v4.widget.DrawerLayout>
Any suggestion on how to fix this?
Edit:
the database looks like this:
both writing and reading rules are true
Edit
I have changed the private names in the Song class so they are the same as the database.
private String Author;
private String Song;
private String Url;
it starts working the problem is that now it displays just the author
Update 2
I this code fetch the user data from firebase and show data into recycled view.
In layout i used constaraint layout you can change it..
display_data.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/rvData"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
<TextView
android:id="#+id/dlTvEmpty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No Found"
android:textSize="16dp"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
then after row_layout bind into recycler view..
row_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:id="#+id/rlTvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:textSize="20dp" />
<TextView
android:id="#+id/rlTvEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email"
android:textSize="20dp"
app:layout_constraintTop_toBottomOf="#+id/rlTvName" />
<TextView
android:id="#+id/rlTvPwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
android:textSize="20dp"
app:layout_constraintTop_toBottomOf="#+id/rlTvEmail" />
</android.support.constraint.ConstraintLayout>
then after make pojo class for user that insert into firebase..
User.java
public class User {
public String name;
public String email;
public String pwd;
// Default constructor required for calls to
// DataSnapshot.getValue(User.class)
public User() {
}
public User(String name, String email,String pwd) {
this.name = name;
this.email = email;
this.pwd=pwd;
}
}
then after make adapter class.
DisplayAllData.java
public class DisplayAllData extends RecyclerView.Adapter<DisplayAllData.ItemViewHolder> {
private List<User> mUserLsit = new ArrayList<>();
private Context mContext;
#Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout, parent, false);
return new ItemViewHolder(view);
}
public DisplayAllData(Context mContext, List<User> mUserLsit) {
this.mContext = mContext;
this.mUserLsit = mUserLsit;
}
#Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
User user = mUserLsit.get(position);
holder.mTvName.setText(user.name);
holder.mTvEmail.setText(user.email);
holder.mTvPwd.setText(user.pwd);
}
#Override
public int getItemCount() {
return mUserLsit.size();
}
public class ItemViewHolder extends RecyclerView.ViewHolder {
TextView mTvName, mTvEmail, mTvPwd;
public ItemViewHolder(View itemView) {
super(itemView);
mTvEmail = itemView.findViewById(R.id.rlTvEmail);
mTvName = itemView.findViewById(R.id.rlTvName);
mTvPwd = itemView.findViewById(R.id.rlTvPwd);
}
}
}
then after finally make display class to fetch user record from firebase and bind into recyclerview.
DisplayActivity.java
public class DisplayActivity extends AppCompatActivity {
private RecyclerView mRvData;
private DisplayAllData allDataAdapter;
private DatabaseReference mDatabase;
private TextView mTvEmpty;
private FirebaseDatabase mFirebaseInstance;
private List<User> mUserList = new ArrayList<>();
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_data);
initView();
}
private void initView() {
mFirebaseInstance = FirebaseDatabase.getInstance();
mDatabase = mFirebaseInstance.getReference("usersDb/UserTable");
mRvData = findViewById(R.id.rvData);
mTvEmpty = findViewById(R.id.dlTvEmpty);
mRvData.setLayoutManager(new LinearLayoutManager(this));
mDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
mUserList.clear();
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
User user = dataSnapshot1.getValue(User.class);
mUserList.add(user);
}
allDataAdapter = new DisplayAllData(DisplayActivity.this, mUserList);
mRvData.setAdapter(allDataAdapter);
allDataAdapter.notifyDataSetChanged();
if (mUserList.isEmpty())
mTvEmpty.setVisibility(View.VISIBLE);
else
mTvEmpty.setVisibility(View.GONE);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
After few experiments with code like change this
songName.setText(model.getName());
songAuthor.setText(model.getAuthor());
to this
songName.setText(model.getURL());
songAuthor.setText(model.getAuthor());
I have learned that the problem was really the names I gave to the privates and the names I gave to the get-methods in the Song class. The names of the privates should be the same as the database fields and the names of the methods should be getYourPrivateName().
In your database, you have a property under the name of the song called myAuthor but in the model class, you have the field named Author, both should be the same.
To solve this, you have two solutions. First one would be to delete all the record from your database and add fresh data according to your model class. Make sure that the properties in your database are added using the name od your field that exist in your model class: myName, myURL, myAuthor and NOT Author, Song, Url.
If you are not in testing mode, then just use annotation in your model class like this:
#PropertyName("Song")
private String myName;
#PropertyName("Url")
private String myURL;
#PropertyName("Author")
private String myAuthor;
According to your edited question, you fileds are incorrect. Please how you Song class should look like:
public class Song {
private String author, song, url;
public Song() {}
public Song(String author, String song, String url) {
this.author = author;
this.song = song;
this.url = url;
}
public String getAuthor() { return author; }
public String getSong() { return song; }
public String getUrl() { return url; }
}
See the fields? Are named with the first letter lower case.
I'm currently using a method made by Prabeesh R K from YouTube. I'm currently following all the steps without a miss, and I'm only modifying it a little, but the problem is the RecyclerView in my phone is not showing anything. Moreover, there is an error in log : E/RecyclerView: No adapter attached; skipping layout. Can you please help me resolve this. By the way this is my code
GenreView.java (Activity)
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
/**
* Created by A46CB on 2/21/2016.
*/
public class GenreView extends AppCompatActivity{
RecyclerView rvgenre;
GenreAdapter adaptergenre;
RecyclerView.LayoutManager genrelayoutmanager;
String[] judulgenre;
int []imgres = {R.drawable.w06, R.drawable.w100, R.drawable.w102, R.drawable.w144, R.drawable.w173,
R.drawable.w179, R.drawable.w182, R.drawable.w36, R.drawable.w77, R.drawable.w79, R.drawable.w93,
R.drawable.w97};
ArrayList<GenreContent> arrayList = new ArrayList<GenreContent>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_genre);
rvgenre = (RecyclerView)findViewById(R.id.genre_recycler_view);
rvgenre.setHasFixedSize(true);
judulgenre = getResources().getStringArray(R.array.genre_title);
int i = 0;
for(String name : judulgenre)
{
GenreContent genrecontent = new GenreContent(imgres[i],judulgenre[i]);
arrayList.add(genrecontent);
i++;
}
genrelayoutmanager = new GridLayoutManager(this, 2);
rvgenre.setLayoutManager(genrelayoutmanager);
adaptergenre = new GenreAdapter(arrayList);
rvgenre.setAdapter(adaptergenre);
}
}
GenreAdapter.Java (Adapter)
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by A46CB on 2/21/2016.
*/
public class GenreAdapter extends RecyclerView.Adapter<GenreAdapter.GenreRecyclerViewHolder> {
private ArrayList<GenreContent> arrayList = new ArrayList<GenreContent>();
public GenreAdapter(ArrayList<GenreContent> arrayList)
{
this.arrayList = arrayList;
}
#Override
public GenreRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.genre_layout, parent, false);
GenreRecyclerViewHolder grvh = new GenreRecyclerViewHolder(view);
return grvh;
}
#Override
public void onBindViewHolder(GenreRecyclerViewHolder holder, int position) {
GenreContent genreContent = arrayList.get(position);
holder.imgView.setImageResource(genreContent.getImg());
holder.tvgenre.setText(genreContent.getTitle());
}
#Override
public int getItemCount() {
return arrayList.size();
}
public static class GenreRecyclerViewHolder extends RecyclerView.ViewHolder {
ImageView imgView;
TextView tvgenre;
public GenreRecyclerViewHolder (View view){
super (view);
imgView = (ImageView)view.findViewById(R.id.genrepic);
tvgenre = (TextView)view.findViewById(R.id.titlegenre);
}
}
}
GenreContent.java
public class GenreContent {
private int img;
private String title;
public GenreContent(int img, String title){
this.setImg(img);
this.setTitle(title);
}
public int getImg() {
return img;
}
public void setImg(int img) {
this.img = img;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
My Layout
TabGenre.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/genre_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
GenreLayout.xml (Custom Layout)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dip"
android:orientation="vertical">
<ImageView
android:layout_width="160dp"
android:layout_height="120dp"
android:src="#drawable/bgheader"
android:id="#+id/genrepic"/>
<TextView
android:id="#+id/titlegenre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mystery"
android:layout_marginTop="-5dp"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
You should change this line
adaptergenre = new GenreAdapter(new ArrayList<>(arrayList));
to
adaptergenre = new GenreAdapter(arrayList);
There is new need to create new ArrayList