I am posting this question because I haven't found any proper solution related to my problem.
I am trying to retrieve my sensor data from Firebase but I am unable to do it. Please point out where am I doing wrong
Try.java
public class Try extends AppCompatActivity {
Button button;
ListView list;
DatabaseReference databaseTry;
List<TryObjectClass> tryObjectList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_try);
databaseTry =
FirebaseDatabase.getInstance().getReference("sensor_data");
button = (Button) findViewById(R.id.button);
list = (ListView) findViewById(R.id.list);
//list.setVisibility(View.INVISIBLE);
tryObjectList = new ArrayList<>();
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
#Override
protected void onStart() {
super.onStart();
databaseTry.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
tryObjectList.clear();
for (DataSnapshot TrySnapshot : dataSnapshot.getChildren()){
TryObjectClass tryObjectClass =
TrySnapshot.getValue(TryObjectClass.class);
tryObjectList.add(tryObjectClass);
}
TryAdapter adapter = new TryAdapter(Try.this, tryObjectList);
list.setAdapter(adapter);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
This is my object class
TryObjectClass.java
public class TryObjectClass {
private String date;
private String time;
private String humidity;
private String motion;
private String distance;
private String temperature;
public TryObjectClass(){
}
public TryObjectClass(String date, String time, String humidity, String
motion, String distance, String temperature) {
this.date = date;
this.time = time;
this.humidity = humidity;
this.motion = motion;
this.distance = distance;
this.temperature = temperature;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getHumidity() {
return humidity;
}
public void setHumidity(String humidity) {
this.humidity = humidity;
}
public String getMotion() {
return motion;
}
public void setMotion(String motion) {
this.motion = motion;
}
public String getDistance() {
return distance;
}
public void setDistance(String distance) {
this.distance = distance;
}
public String getTemperature() {
return temperature;
}
public void setTemperature(String temperature) {
this.temperature = temperature;
}
}
This is my adapter class
TryAdapter.java
public class TryAdapter extends ArrayAdapter<TryObjectClass> {
private Activity context;
private List<TryObjectClass> objectList;
public TryAdapter(#NonNull Activity context, List<TryObjectClass>
objectList) {
super(context, R.layout.new_list,0);
this.context = context;
this.objectList = objectList;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull
ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.new_list,null,true);
TextView date = (TextView) listViewItem.findViewById(R.id.date);
TextView distance = (TextView)
listViewItem.findViewById(R.id.distance);
TextView humidity = (TextView)
listViewItem.findViewById(R.id.humidity);
TextView motion = (TextView) listViewItem.findViewById(R.id.motion);
TextView temperature = (TextView)
listViewItem.findViewById(R.id.temperature);
TextView time = (TextView) listViewItem.findViewById(R.id.time);
TryObjectClass tryObjectClass = objectList.get(position);
date.setText(tryObjectClass.getDate());
distance.setText(tryObjectClass.getDistance());
humidity.setText(tryObjectClass.getHumidity());
motion.setText(tryObjectClass.getMotion());
temperature.setText(tryObjectClass.getTemperature());
time.setText(tryObjectClass.getTime());
return super.getView(position, convertView, parent);
}
}
try_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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=".Try"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Retrieved data"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"
android:padding="10dp"
android:layout_marginLeft="90dp"
android:layout_marginTop="20dp"/>
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="click here to retrieve the data"/>
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
This is my list item
new_list.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date -"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#000"
android:padding="10dp"
android:layout_marginLeft="20dp"/>
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Distance -"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#000"
android:padding="10dp"
android:layout_marginLeft="20dp"/>
<TextView
android:id="#+id/distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Humidity -"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#000"
android:padding="10dp"
android:layout_marginLeft="20dp"/>
<TextView
android:id="#+id/humidity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Motion -"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#000"
android:padding="10dp"
android:layout_marginLeft="20dp"/>
<TextView
android:id="#+id/motion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Temperature -"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#000"
android:padding="10dp"
android:layout_marginLeft="20dp"/>
<TextView
android:id="#+id/temperature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time -"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#000"
android:padding="10dp"
android:layout_marginLeft="20dp"/>
<TextView
android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
This is my database
smartborder-cef61
sensor_data
100
date: 1875
distance: "50M"
humidity: 4573
motion: "yes"
temperature: "30c"
time: 7377200
200
date: 23111996
distance: "50M"
humidity: 45
motion: "yes"
temperature: "30C"
time: 73775
I found something, try to put your setAdapter() outside your listener.
Inside your listener you will put this code:
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot TrySnapshot : dataSnapshot.getChildren()){
TryObjectClass tryObjectClass = TrySnapshot.getValue(TryObjectClass.class);
tryObjectList.add(tryObjectClass);
}
adapter.notifyDataSetChanged();
}
And try to put your declared things in onCreate method, like your ValueEventListener, so you can add it in onStart and remove it in onDestroy when your activity have not to listen your changes anymore when your activity is destroyed. Here's part of my fragment ContatsFragment, it is working fine:
public class ContatsFragment extends Fragment {
private ListView listView;
private ArrayAdapter adapter;
private ArrayList<Contat> contats;
private DatabaseReference databaseReference;
private ValueEventListener valueEventListenerContatos;
private Context context;
public ContatsFragment() {
// Required empty public constructor
}
#Override
public void onStart() {
super.onStart();
context = getActivity();
databaseReference.addValueEventListener(valueEventListenerContatos);
}
#Override
public void onStop() {
super.onStop();
databaseReference.removeEventListener(valueEventListenerContatos);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
contats = new ArrayList<>();
View view = inflater.inflate(R.layout.fragment_contats, container, false);
listView = (ListView) view.findViewById(R.id.lv_contatos);
adapter = new ContatAdapter(getActivity(), contats);
listView.setAdapter(adapter);
Preferences preferences = new Preferences(getActivity());
String loggedUserId = preferences.getLoggedUserId();
databaseReference = FirebaseConfig.getFirebaseReference();
databaseReference = databaseReference.child("contats")
.child(loggedUserId);
valueEventListenerContatos = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
contats.clear();
for (DataSnapshot dados : dataSnapshot.getChildren()) {
Contat contat = dados.getValue(Contat.class);
contats.add(contat);
}
adapter.notifyDataSetChanged();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
};
return view;
}
}
I have got the solution...I made mistake in the giving the list object name...
Related
Here are my codes for MyAdapter, ActivityMain, MinActivity, and User
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
Context context;
ArrayList<User> list;
public MyAdapter(Context context, ArrayList<User> list) {
this.context = context;
this.list = list;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
return new MyViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
// here is the problem that I don't know how to fix][1]
// I'm tryin to pass data from firebase to Recyclerview
// error I'm getting here is:
// [Cannot resolve method 'getFirstName' in 'User'
// Cannot resolve method 'getLastName' in 'User'
// Cannot resolve method 'getAge' in 'User']
User user = list.get(position);
holder.FirstName.setText(user.getFirstName());
holder.LastName.setText(user.getLastName());
holder.Age.setText(user.getAge());
}
#Override
public int getItemCount() {
return list.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
TextView FirstName, LastName, Age;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
FirstName = itemView.findViewById(R.id.tvFirstName);
LastName = itemView.findViewById(R.id.tvLastName);
Age = itemView.findViewById(R.id.tvAge);
}
}
}
This is MainActivity code
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
DatabaseReference database;
MyAdapter myAdapter;
ArrayList<User> list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_Main);
recyclerView = findViewById(R.id.userList);
database = FirebaseDatabase.getInstance().getReference("User");
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
list = new ArrayList<>();
myAdapter = new MyAdapter(this,list);
recyclerView.setAdapter(myAdapter);
database.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot : snapshot.getChildren()){
User user = dataSnapshot.getValue(User.class);
list.add(user);
}
myAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
}
This is User codes which I generated with getter
public class User {
String FirstName, LastName, Age;
public String getFirstName() {
return FirstName;
}
public String getLastName() {
return LastName;
}
public String getAge() {
return Age;
}
{
}
}
This is my layout file
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardElevation="8dp"
app:cardCornerRadius="8dp"
android:layout_margin="16dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Name :"
android:textColor="#color/black"
android:textSize="26sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/tvFirstName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:text="Arya"
android:textColor="#color/black"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/last_name"
android:textColor="#color/black"
android:textSize="26sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/tvLastName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="#string/stark"
android:textColor="#color/black"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/age"
android:textColor="#color/black"
android:textSize="26sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/tvAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:text="18"
android:textColor="#color/black"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
When I hit RUN it gives me this error:
I'm new to coding.
you can change your code like this
holder.FirstName.setText(list.getFirstName().get(position));
holder.LastName.setText(list.getLastName().get(position));
holder.Age.setText(list.getAge().get(position);
and you should change this code position
myAdapter = new MyAdapter(this,list);
recyclerView.setAdapter(myAdapter);
into under this code
database.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot : snapshot.getChildren()){
User user = dataSnapshot.getValue(User.class);
list.add(user);
}
myAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
hope it can solve your problem :)
I am build the navigation view using fragment method. in this fragment i create some method to show the view, such as spinner when click value in spinner, it will display in the recycle view. but the problem is in the adapter is show null object references.
public class LRPFragment extends Fragment {
private String mInstituteLocation = "";
private Spinner sp;
DatabaseReference db;
FirebaseHelper helper;
RecyclerView recyclerView;
LRPAdapter adapter;
List<Region> routesList;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
db = FirebaseDatabase.getInstance().getReference();
helper=new FirebaseHelper(db);
View RootView = inflater.inflate(R.layout.fragment_lrp, container, false);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView = RootView.findViewById(R.id.rv_rec_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(llm);
sp = (Spinner) RootView.findViewById(R.id.subreg);
loadRoute("Region");
return RootView;
}
private void loadRoute(final String referenceKey){
db = FirebaseDatabase.getInstance().getReference(referenceKey);
db.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
final List<String> subregionMark = new ArrayList<>();
subregionMark.add(0, "Choose Subregion");
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
for (DataSnapshot dataSnapshot1: snapshot.getChildren()){
subregionMark.add(dataSnapshot1.child("subregionMark").getValue(Region.class).getSubregionMark());//null object references on getSubregionMark
}
}
ArrayAdapter <String> subAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_dropdown_item, subregionMark);
subAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(subAdapter);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getActivity().setTitle("Menu LRPFragment");
}
}
This is constructor
public class Region {
String name;
String start;
String end;
String distance;
String regionMark;
String subregionMark;
public String getStart() {
return start;
}
public void setStart(String start) {
this.start = start;
}
public String getEnd() {
return end;
}
public void setEnd(String end) {
this.end = end;
}
public String getDistance() {
return distance;
}
public void setDistance(String distance) {
this.distance = distance;
}
public String getSubregionMark() {
return subregionMark;
}
public void setSubregionMark(String subregionMark) {
this.subregionMark = subregionMark;
}
public Region() {
}
public String getName() {
return name;
}
public String getRegionMark() {
return regionMark;
}
public void setName(String name) {
this.name = name;
}
public void setRegionMark(String regionMark) {
this.regionMark = regionMark;
}
This is layout where i call fragment
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/toolbar"
android:id="#+id/rel"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="#id/tvreg"
android:layout_centerVertical="true"
android:layout_marginTop="-57dp"
android:text="LRP"
android:textAlignment="center"
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:id="#+id/tvreg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="17dp"
android:layout_marginTop="49dp"
android:layout_marginEnd="23dp"
android:layout_toStartOf="#+id/regionMark"
android:text="REGION" />
<Spinner
android:id="#+id/regionMark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="47dp"
android:layout_marginEnd="13dp"
android:contextClickable="true"
android:entries="#array/regionMark"
android:spinnerMode="dropdown" />
<TextView
android:layout_width="113dp"
android:layout_height="wrap_content"
android:layout_below="#+id/tvreg"
android:layout_alignParentStart="true"
android:layout_marginStart="20dp"
android:layout_marginTop="17dp"
android:layout_marginEnd="20dp"
android:layout_toStartOf="#+id/subreg"
android:text="SUBREGION" />
<Spinner
android:id="#+id/subreg"
android:layout_width="230dp"
android:layout_height="wrap_content"
android:layout_below="#+id/regionMark"
android:layout_alignParentEnd="true"
android:layout_marginTop="13dp"
android:layout_marginEnd="20dp"
android:spinnerMode="dropdown"
android:visibility="visible" />
<TextView
android:id="#+id/or"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OR"
android:layout_below="#+id/subreg"
android:layout_centerInParent="true"/>
<SearchView
android:id="#+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:layout_centerInParent="true"
android:layout_below="#+id/or"
android:imeOptions="actionSearch"
android:inputType="textAutoComplete|textAutoCorrect"
android:textColor="#FFFFFF"
>
<requestFocus />
</SearchView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/filterTCBtn"
android:text="Filter"
android:layout_below="#+id/editText1"/>
</RelativeLayout>
<TextView
android:layout_below="#+id/rel"
android:id="#+id/route_view_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Route View"
android:textSize="20dp"
android:layout_centerHorizontal="true" />
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_rec_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/route_view_menu"
android:layout_marginTop="-2dp"
android:padding="3dp" />
</RelativeLayout>
This is myfirebase
I have made a recyclerview and populating it with data from server with volley, but this error keep coming back, I have tried all the solutions available but it won't work. Some help will be really appreciated.
I am using MySQL database my api for getting data from database is working fine, but recyclerview is giving this error
"android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText"
Data Class:
public class bus_data {
private int id;
private String bus_number;
private String bus_total_seats;
private String bus_available_seats;
private String bus_route;
private String bus_leaving_time;
private String bus_reaching_time;
private String bus_driver_name;
private String bus_ticketchecker_name;
private String bus_rating;
private String bus_break_time;
private String bus_company;
public bus_data(int id,String bus_number,String bus_total_seats,String bus_available_seats,
String bus_route,String bus_leaving_time,String bus_reaching_time,
String bus_driver_name,String bus_ticketchecker_name,String bus_rating,String bus_break_time,String bus_company) {
this.id = id;
this.bus_number = bus_number;
this.bus_total_seats = bus_total_seats;
this.bus_available_seats = bus_available_seats;
this.bus_route = bus_route;
this.bus_leaving_time = bus_leaving_time;
this.bus_reaching_time = bus_reaching_time;
this.bus_driver_name = bus_driver_name;
this.bus_ticketchecker_name = bus_ticketchecker_name;
this.bus_rating = bus_rating;
this.bus_break_time = bus_break_time;
this.bus_company = bus_company;
}
public int getId() {
return id;
}
public String getbus_number() {
return bus_number;
}
public String getbus_total_seats() {
return bus_total_seats;
}
public String getbus_available_seats() {
return bus_available_seats;
}
public String getbus_route() {
return bus_route;
}
public String getbus_leaving_time() {
return bus_leaving_time;
}
public String getbus_reaching_time() {
return bus_reaching_time;
}
public String getbus_driver_name() {
return bus_driver_name;
}
public String getbus_ticketchecker_name() {
return bus_ticketchecker_name;
}
public String getbus_rating() {
return bus_rating;
}
public String getbus_break_time() {
return bus_break_time;
}
public String getbus_company() {
return bus_company;
}
}
Adapter Class:
public class adapter_class extends RecyclerView.Adapter<adapter_class.bus_dataViewHolder> {
private Context mCtx;
private List<bus_data> productList;
public adapter_class(Context mCtx, List<bus_data> productList) {
this.mCtx = mCtx;
this.productList = productList;
}
#Override
public bus_dataViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.bus_detail_list, null);
return new bus_dataViewHolder(view);
}
#Override
public void onBindViewHolder(bus_dataViewHolder holder, int position) {
bus_data busDetail = productList.get(position);
//loading the image
holder.bus_number.setText(busDetail.getbus_number());
holder.total_seats.setText(busDetail.getbus_total_seats());
holder.available_seats.setText(String.valueOf(busDetail.getbus_available_seats()));
holder.bus_route.setText(String.valueOf(busDetail.getbus_route()));
holder.bus_leaving_time.setText(String.valueOf(busDetail.getbus_leaving_time()));
holder.bus_reaching_time.setText(String.valueOf(busDetail.getbus_reaching_time()));
holder.bus_driver_name.setText(String.valueOf(busDetail.getbus_driver_name()));
holder.bus_ticketchecker_name.setText(String.valueOf(busDetail.getbus_ticketchecker_name()));
holder.bus_rating.setText(String.valueOf(busDetail.getbus_rating()));
holder.bus_break_time.setText(String.valueOf(busDetail.getbus_break_time()));
holder.bus_company.setText(String.valueOf(busDetail.getbus_company()));
}
#Override
public int getItemCount() {
return productList.size();
}
class bus_dataViewHolder extends RecyclerView.ViewHolder {
EditText bus_number, total_seats, available_seats, bus_route, bus_leaving_time, bus_reaching_time,
bus_driver_name, bus_ticketchecker_name, bus_rating, bus_break_time, bus_company;
public bus_dataViewHolder(View itemView) {
super(itemView);
bus_number = itemView.findViewById(R.id.bus_number);
total_seats = itemView.findViewById(R.id.total_seats);
available_seats = itemView.findViewById(R.id.available_seats);
bus_route = itemView.findViewById(R.id.route);
bus_leaving_time = itemView.findViewById(R.id.leaving_time);
bus_reaching_time = itemView.findViewById(R.id.reaching_time);
bus_driver_name = itemView.findViewById(R.id.driver_name);
bus_ticketchecker_name = itemView.findViewById(R.id.tk_checker_name);
bus_rating = itemView.findViewById(R.id.rating);
bus_break_time = itemView.findViewById(R.id.break_time);
bus_company = itemView.findViewById(R.id.bus_company);
}
}
}
Main Class:
public class Bus_Details extends AppCompatActivity {
private static final String URL_PRODUCTS = "http://192.168.10.17/AutoBus/api.php";
//a list to store all the products
List<bus_data> productList;
//the recyclerview
RecyclerView recyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bus_details);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout);
recyclerView = findViewById(R.id.recylcerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//initializing the productlist
productList = new ArrayList<>();
//this method will fetch and parse json
//to display it in recyclerview
loadProducts();
}
private void loadProducts() {
/*
* Creating a String Request
* The request type is GET defined by first parameter
* The URL is defined in the second parameter
* Then we have a Response Listener and a Error Listener
* In response listener we will get the JSON response as a String
* */
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_PRODUCTS,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
//converting the string to json array object
JSONArray array = new JSONArray(response);
//traversing through all the object
for (int i = 0; i < array.length(); i++) {
//getting product object from json array
JSONObject data = array.getJSONObject(i);
//adding the product to product list
productList.add(new bus_data(
data.getInt("id"),
data.getString("bus_number"),
data.getString("bus_total_seats"),
data.getString("bus_available_seats"),
data.getString("bus_route"),
data.getString("bus_leaving_time"),
data.getString("bus_reaching_time"),
data.getString("bus_driver_name"),
data.getString("bus_ticketchecker_name"),
data.getString("bus_rating"),
data.getString("bus_break_time"),
data.getString("bus_company")
));
}
//creating adapter object and setting it to recyclerview
adapter_class adapter = new adapter_class(Bus_Details.this, productList);
recyclerView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(Bus_Details.this, "Error"+e.toString(), Toast.LENGTH_SHORT).show();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Bus_Details.this, "Error"+error.toString(), Toast.LENGTH_SHORT).show();
}
});
//adding our stringrequest to queue
Volley.newRequestQueue(this).add(stringRequest);
}
}
Here are the layout files.
recycler_activity:
<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"
tools:context=".Passenger.Bus_Details">
<android.support.v7.widget.RecyclerView
android:id="#+id/recylcerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="745dp"
tools:layout_editor_absoluteY="-51dp" />
</RelativeLayout>
List_activity:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:id="#+id/layout">
<TextView
android:id="#+id/bus_company"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Daewo"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Small"
android:textColor="#ffffff" />
<TextView
android:id="#+id/bus_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/bus_company"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:text="GJN-1234"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Small" />
<TextView
android:id="#+id/total_seats"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/bus_number"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:background="#color/colorPrimary"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:text="70"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Small.Inverse"
android:textStyle="bold" />
<TextView
android:id="#+id/available_seats"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/total_seats"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:text="50"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"
android:textStyle="bold" />
<TextView
android:id="#+id/leaving_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/available_seats"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:text="50"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"
android:textStyle="bold" />
<TextView
android:id="#+id/reaching_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/leaving_time"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:text="50"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"
android:textStyle="bold" />
<TextView
android:id="#+id/driver_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/reaching_time"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:text="50"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"
android:textStyle="bold" />
<TextView
android:id="#+id/tk_checker_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/driver_name"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:text="50"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"
android:textStyle="bold" />
<TextView
android:id="#+id/break_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tk_checker_name"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:text="50"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"
android:textStyle="bold" />
<TextView
android:id="#+id/rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/break_time"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:text="50"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"
android:textStyle="bold" />
<TextView
android:id="#+id/route"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/rating"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:text="50"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>
This is the error I am getting
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.autobus, PID: 10266
java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText
Screenshot of error:
In your bus_dataViewHolder class replace EditText to TextView.
because in layout you can use TextView and inside the program you use Edittext, that's while this issue generated.
This issue is because you take TextView in your adapter xml and get it like as a EditText.
So change in your adapter onCreateViewHolder from EditText to TextView .
Change below line in your adapter class.
TextView bus_number, total_seats, available_seats, bus_route, bus_leaving_time, bus_reaching_time,
bus_driver_name, bus_ticketchecker_name, bus_rating, bus_break_time, bus_company;
I am creating a weather forecast aplication. For this I want to use RecyclerView to show items. The problem is that my RecyclerView is showing only one item instead of all. I am using the OpenWeatherMap API forecast.
This is my layout:
<LinearLayout 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:layout_gravity="center"
android:background="#drawable/weather_background2"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="#+id/txt_close_weather"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="end"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:background="#drawable/circle"
android:gravity="center"
android:text="X"
android:textColor="#color/blackTransparent"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8"
android:orientation="vertical">
<TextView
android:id="#+id/city_country_weather"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="0dp"
android:text="KOTA"
android:textColor="#color/blackTransparent"
android:textSize="24dp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="2dp"
android:text="Today"
android:textColor="#color/blackTransparent"
android:textSize="16dp" />
<TextView
android:id="#+id/current_date_weather"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="4dp"
android:textColor="#color/blackTransparent"
android:textSize="16dp" />
<ImageView
android:id="#+id/weather_icon"
android:layout_width="160dp"
android:layout_height="160dp"
android:layout_gravity="center|center_horizontal"
android:layout_marginTop="8dp"
android:contentDescription="#string/app_name"
android:src="#drawable/img_02d" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="2dp"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:weightSum="3">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/tv_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hummidity"
android:textColor="#color/blackTransparent"
android:textSize="14dp"
android:textStyle="bold" />
<TextView
android:id="#+id/wind_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="#string/app_name"
android:textColor="#color/blackTransparent"
android:textSize="14dp" />
<TextView
android:id="#+id/temperature_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="#string/app_name"
android:textColor="#color/blackTransparent"
android:textSize="16dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:gravity="center_horizontal"
android:background="#color/whiteTr"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2.5"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/weather_daily_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal"
android:scrollbars="none" />
</LinearLayout>
</LinearLayout>
This my code in activity :
public class DialogWeather extends android.app.DialogFragment {
public static final int WIDTH = 60;
public static final int HEIGHT = 80;
public static final String DATE_FORMAT_WITHOUT_TIME = "dd/MM";
private static final String ICON_PREFIX = "img_";
private static final String API_KEY = "2fc56d498a3b4d87ba298c232e65e6b0";
private static final String UNITS = "metric";
final String q = "Jakarta";
PopupWindow myPopupWindow;
Dialog myDialog;
DialogWeather dialogWeather;
private TextView txtCloseWeather, txtCityCountry,
txtCurrentDate, tvDescription, tvWindResult, tvTemperatureResult;
private ImageView weatherIcon;
private RecyclerView mRecyclerViewWeather;
DialogWeatherAdapter dialogWeatherAdapter;
private ArrayList<Example2> weathers = new ArrayList<>();
Example2 example2List;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_dialog_weather,
container, false);
getDialog().getWindow().setBackgroundDrawable(new
ColorDrawable(Color.TRANSPARENT));
tvDescription = rootView.findViewById(R.id.tv_description);
tvWindResult = rootView.findViewById(R.id.wind_result);
tvTemperatureResult = rootView.findViewById(R.id.temperature_result);
weatherIcon = rootView.findViewById(R.id.weather_icon);
txtCityCountry = rootView.findViewById(R.id.city_country_weather);
txtCityCountry.setTypeface(Typeface.DEFAULT_BOLD);
txtCurrentDate = rootView.findViewById(R.id.current_date_weather);
txtCloseWeather = rootView.findViewById(R.id.txt_close_weather);
txtCloseWeather.setTypeface(Typeface.DEFAULT_BOLD);
Fragment fragment = this;
txtCloseWeather.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Button Close",
Toast.LENGTH_LONG).show();
getActivity().getFragmentManager().beginTransaction()
.remove(fragment).commit();
}
});
mRecyclerViewWeather = rootView.findViewById(R.id.weather_daily_list);
LinearLayoutManager layoutManager = new
LinearLayoutManager(getApplicationContext());
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
mRecyclerViewWeather.setLayoutManager(layoutManager);
mRecyclerViewWeather.setHasFixedSize(true);
dialogWeatherAdapter = new DialogWeatherAdapter(weathers);
mRecyclerViewWeather.setAdapter(dialogWeatherAdapter);
loadWeathers(q, API_KEY, UNITS);
return rootView;
}
private void loadWeathers(String q, String apiKey, String units) {
ApiServicesWeather weather = InitRetrofitWeather.getServicesWeather();
Call<Example2> exampleCall = weather.getDetailWeather(q, apiKey, units);
exampleCall.enqueue(new Callback<Example2>() {
#Override
public void onResponse(Call<Example2> call, Response<Example2>
response) {
if (response.isSuccessful()) {
Toast.makeText(getApplicationContext(), "Success",
Toast.LENGTH_LONG).show();
example2List = response.body();
weathers.addAll(Collections.singleton(example2List));
loadSetView();
dialogWeatherAdapter.updateList(weathers);
dialogWeatherAdapter.notifyDataSetChanged();
}
}
#Override
public void onFailure(Call<Example2> call, Throwable t) {
Toast.makeText(getApplicationContext(), "Gagal " +
t.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
private void loadSetView() {
txtCityCountry.setText(weathers.get(getId()).getCity().getName());
txtCurrentDate.setText(Util.formatDate(weathers
.get(getId()).getList().get(getId()).getDtTxt(), "EEE MMM dd, yyyy"));
weatherIcon.setImageResource(Util
.getDrawableResourceIdByName(getApplicationContext(),ICON_PREFIX + weathers
.get(getId()).getList().get(getId()).getWeather().get(getId()).getIcon()));
tvDescription.setText(weathers.get(getId()).getList().get(getId())
.getWeather().get(getId()).getDescription());
tvWindResult.setText("Wind : " +
weathers.get(getId()).getList().get(getId()).getWind().getSpeed() + " m/s");
double mTemperature = (double)
weathers.get(getId()).getList().get(getId()).getMain().getTemp();
tvTemperatureResult.setText(String.valueOf(weathers
.get(getId()).getList().get(ge
tId()).getMain().getTemp()) + " °C");
}
#Override
public void onResume() {
super.onResume();
ViewGroup.LayoutParams params = getDialog().getWindow().getAttributes();
params.width = 700;
params.height = 1100;
getDialog().getWindow().setAttributes((WindowManager.LayoutParams)
params);
}
this My Adapter :
public class DialogWeatherAdapter extends
RecyclerView.Adapter<DialogWeatherAdapter.DialogViewHolder> {
ArrayList<Example2> data;
Context context;
Activity c;
public DialogWeatherAdapter(ArrayList<Example2> data, Context context,
Activity c) {
this.data = data;
this.context = context;
this.c = c;
}
public DialogWeatherAdapter(ArrayList<Example2> weathers) {
this.data = weathers;
}
#Override
public DialogViewHolder onCreateViewHolder(ViewGroup parent, int
viewType) {
LayoutInflater layoutInflater = (LayoutInflater)
parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.item_weathers, parent,
false);
return new DialogViewHolder(view);
}
#Override
public void onBindViewHolder(DialogViewHolder holder, int position) {
if (holder instanceof DialogViewHolder) {
final DialogViewHolder viewHolder = (DialogViewHolder) holder;
if (data != null) {
for (int i = 0; i < data.size(); i++) {
viewHolder.tvCurrentDateItem.setText(Util.formatDate(data
.get(position).getList()
.get(position).getDtTxt(), "dd/MM"));
viewHolder.ivIconItem.setImageResource(Util
.getDrawableResourceIdByName(getApplicationContext(), ICON_PREFIX + data
.get(getId()).getList().get(getId()).getWeather().get(getId()).getIcon()));
viewHolder.tvDescriptionItem.setText(data.get(position)
.getList().get(position).getWeather().get(position).getDescription());
viewHolder.tvTemperatureItem.setText(String.valueOf(weathers
.get(getId()).getList
().get(getId()).getMain().getTemp()) + " °C");
}
}
}
}
#Override
public int getItemCount() {
return data.size();
}
public void updateList(ArrayList<Example2> weathers) {
this.data = weathers;
}
public class DialogViewHolder extends RecyclerView.ViewHolder {
TextView tvCurrentDateItem, tvDescriptionItem, tvTemperatureItem;
ImageView ivIconItem;
public DialogViewHolder(View itemView) {
super(itemView);
tvCurrentDateItem = itemView.findViewById(R.id.current_date_weather_item);
tvDescriptionItem = itemView.findViewById(R.id.description_item);
tvTemperatureItem = itemView.findViewById(R.id.temperature_item);
ivIconItem = itemView.findViewById(R.id.icon_weather_item);
}
}
}
enter image description here
The reason for this is -
The Item file (R.layout.item_weathers) has height match parent. Change it to wrap_content as shown -
<LinearLayout 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="wrap_content"/>
and it will show all the items.
Hi I ma trying to build an app that has a list of events retrieved from a firebase database but when I run it it displays just the first event stored.
there is the code I used to generate the list:
private RecyclerView recyclerView;
private FirebaseRecyclerAdapter<Event,EventViewHolder> firebaseRecyclerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calendario);
recyclerView = (RecyclerView) findViewById(R.id.eventiList);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
Query query = FirebaseDatabase.getInstance().getReference("Eventi");
FirebaseRecyclerOptions<Event> options =
new FirebaseRecyclerOptions.Builder<Event>().setQuery(query, Event.class).build();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Event, EventViewHolder>(options) {
#NonNull
#Override
public EventViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.event_layout, parent, false);
return new EventViewHolder(view);
}
#Override
protected void onBindViewHolder(#NonNull EventViewHolder holder, final int position, #NonNull Event model) {
holder.setDay(model.getData().substring(0,2));
holder.setMonth(model.getMese());
holder.setName(model.getNome());
holder.setLuogo(model.getLuogo());
holder.setendStart(model.getOra_inizio()+ " - "+ model.getOra_fine());
holder.mview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);
}
public static class EventViewHolder extends RecyclerView.ViewHolder
{
View mview;
public EventViewHolder(View itemView)
{
super(itemView);
mview=itemView;
}
public void setDay(String day)
{
TextView mtext= (TextView)mview.findViewById(R.id.day);
mtext.setText(day);
}
public void setName(String name)
{
TextView maut=(TextView)mview.findViewById(R.id.Event_Name);
maut.setText(name);
}
public void setMonth(String month)
{
TextView maut=(TextView)mview.findViewById(R.id.month);
maut.setText(month);
}
public void setLuogo(String luogo)
{
TextView maut=(TextView)mview.findViewById(R.id.luogo);
maut.setText(luogo);
}
public void setendStart(String endStart)
{
TextView maut=(TextView)mview.findViewById(R.id.start_end);
maut.setText(endStart);
}
}
#Override
protected void onStart() {
super.onStart();
firebaseRecyclerAdapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
//mAdapter.stopListening();
firebaseRecyclerAdapter.stopListening();
}
this is the code of the model class
public class Event {
private String Nome;
private String data;
private String descrizione;
private String luogo;
private String mese;
private String ora_fine;
private String ora_inizio;
private String type;
public Event()
{
}
public Event(String nome, String data, String descrizione, String luogo, String mese, String ora_fine, String ora_inizio, String type) {
Nome = nome;
this.data = data;
this.descrizione = descrizione;
this.luogo = luogo;
this.mese = mese;
this.ora_fine = ora_fine;
this.ora_inizio = ora_inizio;
this.type = type;
}
public String getNome() {
return Nome;
}
public String getData() {
return data;
}
public String getDescrizione() {
return descrizione;
}
public String getLuogo() {
return luogo;
}
public String getMese() {
return mese;
}
public String getOra_fine() {
return ora_fine;
}
public String getOra_inizio() {
return ora_inizio;
}
public String getType() {
return type;
}
}
this is the Calendar activity layout
<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=".Calendario"
android:id="#+id/Calendar_draw">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/eventiList"></android.support.v7.widget.RecyclerView>
</android.support.v4.widget.DrawerLayout>
This is the code for the recycler view row
<?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="match_parent">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="119dp"
android:background="#color/darkBlue">
<TextView
android:id="#+id/day"
android:layout_width="86dp"
android:layout_height="66dp"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/Event_Name"
android:layout_marginStart="18dp"
android:textColor="#color/white"
android:textSize="50dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="18dp" />
<TextView
android:id="#+id/month"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignStart="#+id/day"
android:layout_marginBottom="11dp"
android:textColor="#color/white"
android:textStyle="bold"
android:layout_alignLeft="#+id/day" />
<TextView
android:id="#+id/Event_Name"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="124dp"
android:layout_marginTop="13dp"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:textColor="#color/white"
android:textStyle="bold"
android:layout_alignParentLeft="true"
android:layout_marginLeft="124dp" />
<TextView
android:id="#+id/start_end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginStart="135dp"
android:textAppearance="#style/TextAppearance.AppCompat.Small"
android:textColor="#color/white"
android:textStyle="italic" />
<TextView
android:id="#+id/luogo"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_alignStart="#+id/start_end"
android:layout_below="#+id/day"
android:textAppearance="#style/TextAppearance.AppCompat.Small"
android:textColor="#color/white"
android:textStyle="italic" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
As you can see from the image that follows it displays just one event and I have 5 in the database
Display
The code worked fine for other lists that are in the app I don't understand why it is not working for this one hope I can get an answer here, thanks
Edit: this is the Database tree
Database Tree
Problem Solved, the layout_height parameter was set to match_parent in the event_layout LinearLayout and CardView. I set it to wrap_content on both and it worked.