recyclerview not displaying sqlite data in fragment - java

Code
public class UsageSettings2 extends Fragment {
MainData mDatabaseHelper;
RecyclerView mRecyclerView;
UserDataHelper mHelper;
private List<UsageHelper> usage = new ArrayList<>();
private LinearLayoutManager linearLayoutManager;
private UsageAdapter mAdapter;
SQLiteDatabase db;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.settings_usage2, container, false);
final MainData myDBHlpr = new MainData(getActivity());
db = myDBHlpr.getWritableDatabase();
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.usagelist);
//Add the data first
linearLayoutManager = new LinearLayoutManager(getActivity());
//and then create a object and pass the lis
mAdapter = new UsageAdapter(usage);
mRecyclerView.setLayoutManager(linearLayoutManager);
mRecyclerView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
Cursor csr = myDBHlpr.getAllQuestions2(getActivity());
while (csr.moveToNext()) {
String name = csr.getString(csr.getColumnIndex("Name"));
int sent = csr.getInt(csr.getColumnIndex("MessagesSent"));
int recieved = csr.getInt(csr.getColumnIndex("MessagesRecieved"));
int total = csr.getInt(csr.getColumnIndex("Messages"));
String time = csr.getString(csr.getColumnIndex("TimeSpent"));
new UsageHelper(name,sent,recieved,total,time);
int profile_counts = myDBHlpr.getProfilesCount2();
Log.d("FFF", String.valueOf(profile_counts));
}
return rootView;
}
}
Adapter
public class UsageAdapter extends RecyclerView.Adapter<UsageAdapter.UsageViewHolder>{
private List<UsageHelper> mUsageHelper;
public UsageAdapter(List<UsageHelper> UsageAdapter) {
this.mUsageHelper = UsageAdapter;
}
public class UsageViewHolder extends RecyclerView.ViewHolder{
public TextView mName;
public TextView mSent;
public TextView mRecieved;
public TextView mTotal;
public TextView mTime;
public UsageViewHolder(View view) {
super(view);
mName = (TextView)view.findViewById(R.id.name);
mSent = (TextView)view.findViewById(R.id.sent);
mRecieved = (TextView)view.findViewById(R.id.recieved);
mTotal = (TextView)view.findViewById(R.id.total);
mTime = (TextView)view.findViewById(R.id.time);
}
}
#Override
public UsageAdapter.UsageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View V = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_activity_usage,parent,false);
return new UsageAdapter.UsageViewHolder(V);
}
#Override
public void onBindViewHolder(final UsageViewHolder holder, int position) {
final UsageHelper usageHelper = mUsageHelper.get(position);
holder.mName.setText(usageHelper.getName());
holder.mSent.setText(usageHelper.getSent());
holder.mRecieved.setText(usageHelper.getRecieved());
holder.mTotal.setText(usageHelper.getTotal());
holder.mTime.setText(usageHelper.getTime());
}
#Override
public int getItemCount() {
return mUsageHelper.size();
}
Modal
public class UsageHelper {
private String Name;
private int Sent;
private int Recieved;
private int Total;
private String Time;
public UsageHelper() {
}
public UsageHelper(String Name, int Sent ,int Recieved, int Total, String Time) {
this.Name = Name;
this.Sent = Sent;
this.Recieved = Recieved;
this.Total = Total;
this.Time = Time;
}
public String getName() {
return Name;
}
public void setName(String name) {
this.Name = name;
}
public int getSent() {
return Sent;
}
public void setSent(int sent) {
this.Sent = sent;
}
public int getRecieved() {
return Recieved;
}
public void setRecieved(int recieved) {
this.Recieved = recieved;
}
public String getTime() {
return Time;
}
public void setTime(String time) {
this.Time = time;
}
public int getTotal() {
return Total;
}
public void setTotal(int total) {
this.Total = total;
}
}
The problem is that its not showing anything... the fragment is blank when i open it although the data is present... Any idea why its now showing any data? Im not getting any error just that the data isnt showing..........................................................................
Got an error after making changes according to answers
Custom
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/usage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black">
<RelativeLayout
android:weightSum="1"
android:id="#+id/linear"
android:padding="1dp"
android:background="#color/white"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textSize="15dp"
android:layout_toLeftOf="#+id/mid"
android:text="Name"
android:layout_alignParentStart="true"
android:id="#+id/name"
android:paddingVertical="10dp"
android:background="#color/black"
android:gravity="center_horizontal"
android:textColor="#color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/sent"
android:layout_toRightOf="#id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/black"
android:gravity="center_horizontal"
android:paddingVertical="10dp"
android:text="Sent"
android:textColor="#color/white"
android:textSize="15dp" />
<TextView
android:id="#+id/recieved"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/sent"
android:background="#color/black"
android:gravity="center_horizontal"
android:paddingVertical="10dp"
android:text="Recieved"
android:textColor="#color/white"
android:textSize="15dp" />
<TextView
android:id="#+id/total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/recieved"
android:background="#color/black"
android:gravity="center_horizontal"
android:paddingVertical="10dp"
android:text="Total"
android:textColor="#color/white"
android:textSize="15dp" />
<TextView
android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/recieved"
android:background="#color/black"
android:gravity="center_horizontal"
android:paddingVertical="10dp"
android:text="Time \nSpent"
android:textColor="#color/white"
android:textSize="15dp" />
</RelativeLayout>

The usage list is always empty because you are not adding the new "UsageHelper" you are creating from your Cursor into the list.
What you need to do is the following:
Cursor csr = myDBHlpr.getAllQuestions2(getActivity());
while (csr.moveToNext()) {
...
UsageHelper newListItem = new UsageHelper(name,sent,recieved,total,time);
usage.add(newListItem);
...
}
mAdapter.notifyDataSetChanged();
When you finish adding elements to the list, you want to notify your adapter that the list has new items, so you have to call to mAdapter.notifyDataSetChanged(); and the new elements will be displayed

add mAdapter.notifyDataSetChanged(); in while condition or after adding your data in model class.

In the while loop of onCreateView(), add each row from the db to the list:
usage.add(new UsageHelper(name,sent,recieved,total,time));
instead of just:
new UsageHelper(name,sent,recieved,total,time)
which does nothing.
And after the loop finishes:
mAdapter.notifyDataSetChanged();
so the RecyclerView loads all the added rows to the list.
Edit
Replace
holder.mSent.setText(usageHelper.getSent());
with
holder.mSent.setText("" + usageHelper.getSent());
because usageHelper.getSent() is int and it is treated as a resource id.

Related

How to change TextView in Activity by delete button in RecyclerView?

When I add an item by button Scan Now, the public double sumCost value incrementes by the cost value in the AddItem method and then TextView(android:id="#+id/SumText") assigns this value. And How to decrease sumCost by the number that is in cost and set new text in TextView when pressing the Delete button? Thanks for any help
enter image description here
My full code:
MainActivity:
package com.example.testfirst;
...
public class MainActivity extends AppCompatActivity {
TextView sumText;
Button buttonAdd;
List<Contact> contacts = new LinkedList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_users);
buttonAdd = (Button) findViewById(R.id.scanBtn);
sumText = (TextView) findViewById(R.id.SumText);
buttonAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AddItem("Tom", 2.45);
sumText.setText(Double.toString(sumCost));
}
});
}
public void AddItem(String name, double cost){
sumCost += cost;
RecyclerView rvContacts = (RecyclerView) findViewById(R.id.recyclerView);
ContactsAdapter adapter = new ContactsAdapter(contacts);
rvContacts.setAdapter(adapter);
rvContacts.setLayoutManager(new LinearLayoutManager(this));
contacts.add(new Contact(name,Double.toString(cost)));
}
public double sumCost = 0;
}
Contact(Model class):
...
public class Contact {
private String mName;
private String mCost;
public Contact(String name, String cost) {
mName = name;
mCost = cost;
}
public String getName() {
return mName;
}
public String getCost() {
return mCost;
}
}
ContactsAdapter:
...
public class ContactsAdapter extends RecyclerView.Adapter<ViewHolder>{
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View contactView = inflater.inflate(R.layout.item_contact, parent, false);
return new ViewHolder(contactView).linkAdapter(this);
}
///////////////
#NonNull
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
Contact contact = mContacts.get(position);
TextView textViewId = holder.nameId;
textViewId.setText(contact.getName());
TextView textViewCost = holder.nameCost;
textViewCost.setText(contact.getCost());
}
#Override
public int getItemCount() {
return mContacts.size();
}
List<Contact> mContacts;
public ContactsAdapter(List<Contact> contacts) {
mContacts = contacts;
}
}
class ViewHolder extends RecyclerView.ViewHolder {
private ContactsAdapter adapter;
public TextView nameId;
public TextView nameCost;
public ViewHolder(#NonNull View itemView) {
super(itemView);
nameId = (TextView) itemView.findViewById(R.id.text);
nameCost = (TextView) itemView.findViewById(R.id.textCost);
itemView.findViewById(R.id.delete).setOnClickListener(view -> {
adapter.mContacts.remove(getAdapterPosition());
adapter.notifyItemRemoved(getAdapterPosition());
});
}
public ViewHolder linkAdapter(ContactsAdapter adapter){
this.adapter = adapter;
return this;
}
}
item_contact.xml:
<?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"
android:layout_margin="10dp"
app:cardCornerRadius="10dp"
app:cardElevation="5dp"
app:contentPadding="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="#string/app_name"
android:textSize="20sp" />
<TextView
android:id="#+id/textCost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text"
android:text="#string/app_name"
android:textSize="20sp" />
<Button
android:id="#+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="3dp"
android:layout_marginEnd="3dp"
android:text="Delete" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
activity_users.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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=".MainActivity"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="#+id/SumText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="0.00"
android:textSize="30sp"/>
<Button
android:id="#+id/scanBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scan now"
android:textSize="30sp"
android:layout_gravity="center_horizontal"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
One easy way to do that would be do add a "Decrementer" interface in the adapter that it can call to decrement the total cost, like this:
// Define the interface
public interface Decrementer {
void onDelete(double cost);
}
// Define an instance of the interface to hold
private final Decrementer mDecrementer;
// Pass in a decrementer at construction
public ContactsAdapter(List<Contact> contacts, Decrementer decr) {
mContacts = contacts;
mDecrementer = decr;
}
#NonNull
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
// get views and such ...
// Call the decrementer method when the button is clicked
// Could set this in onBindViewHolder, or pass the decrementer
// into the ViewHolder itself and use it there.
deleteButton.setOnClickListener(view -> {
int pos = holder.getAdapterPosition();
Contact c = mContacts.get(pos);
// add getCostAmount to return a double, or better yet, just
// store it as a Double instead of a string in Contact
mDecrementer.onDelete(c.getCostAmount());
mContacts.remove(pos);
notifyItemRemoved(pos);
});
}
and you would define the Decrementer when you create the adapter so it can access the Activity class members, like this:
ContactsAdapter adapter = new ContactsAdapter(contacts, new ContactsAdapter.Decrementer() {
#Override
public void onDelete(double cost) {
sumCost -= cost;
// change activity TextViews and such here too
sumText.setText(Double.toString(sumCost));
}
});
Side note: you don't need to create a whole new adapter every time you add an item, just add it to the contacts array and call an appropriate notifyDataSetChanged method on the existing adapter.
private ContactsAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_users);
//...
RecyclerView rvContacts = (RecyclerView) findViewById(R.id.recyclerView);
ContactsAdapter adapter = new ContactsAdapter(contacts);
rvContacts.setAdapter(adapter);
rvContacts.setLayoutManager(new LinearLayoutManager(this));
}
public void AddItem(String name, double cost){
sumCost += cost;
contacts.add(new Contact(name,Double.toString(cost)));
adapter.notifyDataSetChanged(); // or a less expensive notify call
}

Checkbox on RecyclerView and their State

After some research on this one of the complex issue
I did this like this for my currency convertor project:
Below are steps:
Created A Custom Adapter with inner ViewHolder
Created an Interference for Selection changed and to notify the
Adaper in MainActivty
Created an Model Class Created an layout for My list
Coding:
Adapter and View Holder
class Adapter extends RecyclerView.Adapter<Adapter.VH> {
ArrayList<Currency> list, checkedCurrencies = new ArrayList<>();
CurrencySelectionChangelistener listener;
public Adapter(#NonNull ArrayList<Currency> list, CurrencySelectionChangelistener listener) {
this.list = list;
this.listener = listener;
}
#NonNull
#Override
public VH onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new VH(LayoutInflater.from(getContext()).inflate(R.layout.layout_list_of_all_items, parent, false));
}
#Override
public void onBindViewHolder(#NonNull VH holder, #SuppressLint("RecyclerView") int position) {
Currency currentCurrency = list.get(position);
holder.checkBox.setChecked(currentCurrency.isChecked());
holder.mDis.setText(currentCurrency.getDescription());
//ignore below line it's just for sorting the string to set On TextView and with another purpose
String name = currentCurrency.getName().toLowerCase().replace(" ", "");
holder.mName.setText(name.toUpperCase());
holder.mLogo.setImageResource(currentCurrency.getLogo(name));
holder.checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (holder.checkBox.isChecked()) {
list.get(position).setChecked(true);
checkedCurrencies.add(currentCurrency);
} else if (!holder.checkBox.isChecked()) {
list.get(position).setChecked(false);
checkedCurrencies.remove(currentCurrency);
}
//calling the interference's function
onSelection(checkedCurrencies);
}
});
}
#Override
public int getItemCount() {
return list.size();
}
class VH extends RecyclerView.ViewHolder {
TextView mName, mDis;
ImageView mLogo;
CheckBox checkBox;
public VH(#NonNull View itemView) {
super(itemView);
checkBox = itemView.findViewById(R.id.checkboxAllItems);
mLogo = itemView.findViewById(R.id.ImageViewAllItemLogo);
mName = itemView.findViewById(R.id.textViewAllItemCName);
mDis = itemView.findViewById(R.id.textViewAllItemCDis);
}
}
}
Interface
public interface CurrencySelectionChangelistener {
public void onSelection(ArrayList<Currency> currencies);}
Fragment in Which I'm using RecyclerView
public class AddMoreCurrencyFragment extends Fragment implements CurrencySelectionChangelistener {
FragmentAddNewCurrencyBinding binding;
ArrayList<Currency> currencies;
Adapter adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
binding = FragmentAddNewCurrencyBinding.inflate(inflater);
CurrencyContainerActivity.title.setVisibility(View.VISIBLE);
CurrencyContainerActivity.title.setText("Add Currency");
CurrencyContainerActivity.backBtn.setVisibility(View.VISIBLE);
currencies = new ArrayList<>();
for (Currency c : CurrencyContainerActivity.listOfCurrencies) {
currencies.add(new Currency(c.getName(), c.getDescription(), false));
}
adapter = new Adapter(currencies, this);
binding.recView.setAdapter(adapter);
binding.done.setOnClickListener(view -> {
});
return binding.getRoot();
}
#Override
public void onSelection(ArrayList<Currency> currencies) {
CurrencyContainerActivity.listToAdd.addAll(currencies);
Toast.makeText(getContext(), currencies.toString(), Toast.LENGTH_LONG).show();
adapter.notifyDataSetChanged();
}
}
xml layout for each item
<?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"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#color/white"
android:orientation="vertical"
android:weightSum="3">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="59dp"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="10dp"
android:weightSum="3">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.89"
app:cardCornerRadius="16dp">
<ImageView
android:id="#+id/ImageViewAllItemLogo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/flag" />
</androidx.cardview.widget.CardView>
<TextView
android:id="#+id/textViewAllItemCName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="19dp"
android:layout_weight=".75"
android:fontFamily="#font/poppins"
android:text="#string/pkr"
android:textAlignment="center"
android:textAllCaps="true"
android:textColor="#color/black"
android:textSize="16sp" />
<TextView
android:id="#+id/textViewAllItemCDis"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_weight="0.47"
android:fontFamily="#font/poppins"
android:text="#string/pkr"
android:textAlignment="textStart"
android:textColor="#color/black"
android:textSize="13sp" />
<CheckBox
android:id="#+id/checkboxAllItems"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_weight="0.90"
android:background="#drawable/radio_selector"
android:button="#android:color/transparent" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#3F000000" />
</LinearLayout>
Model Class
public class Currency {
private String Name, Description;
private int logo;
boolean isChecked;
public Currency(String name, String description, boolean isChecked) {
Name = name;
Description = description;
this.isChecked = isChecked;
}
public boolean isChecked() {
return isChecked;
}
public void setChecked(boolean checked) {
isChecked = checked;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
}
if anything i missed can for that. Thanks

How to refer FIrebase Child in Child record in Firebase Recycler adapter to show in recyclerview

I have written code to list contacts in recycler view. But it doesn't display value in recycler view. The code is given below.
HomeActivity.java
private DatabaseReference mDatabase;
private RecyclerView PList;
private FirebaseRecyclerAdapter<project, ProjViewHolder> FBRA;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
PList = findViewById(R.id.ProjList);
PList.setHasFixedSize(true);
PList.setLayoutManager(new LinearLayoutManager(this));
mDatabase = FirebaseDatabase.getInstance().getReference().child("Contacts");
}
#Override
protected void onStart() {
super.onStart();
FirebaseRecyclerOptions<project> options = new FirebaseRecyclerOptions.Builder<project>().setQuery(mDatabase, project.class).build();
FBRA = new FirebaseRecyclerAdapter<project, ProjViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull ProjViewHolder holder, int position, #NonNull project model) {
String proj_key = getRef(position).getKey();
holder.cpname.setText(model.getCname());
holder.cplocation.setText(model.getCcity());
holder.ccname.setText(model.getPcontact());
holder.ccdesig.setText(model.getPdesig());
}
#Override
public ProjViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.proj_row, parent, false);
return new ProjViewHolder(view);
}
};
PList.setAdapter(FBRA);
FBRA.startListening();
}
public class ProjViewHolder extends RecyclerView.ViewHolder {
TextView cpname, cplocation, ccname, ccdesig;
ProjViewHolder(#NonNull View itemView) {
super(itemView);
cpname = itemView.findViewById(R.id.FCName);
cplocation = itemView.findViewById(R.id.FCLocation);
ccname = itemView.findViewById(R.id.FPName);
ccdesig = itemView.findViewById(R.id.FPDesig);
}
}
#Override
protected void onStop() {
super.onStop();
FBRA.stopListening();
}
FormActivity.java
public class FormActivity extends AppCompatActivity {
private EditText CName, PContact,PDesig, CCity;
private FirebaseDatabase mDatabase;
private Button Fbtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_form);
CName = findViewById(R.id.CompName);
PContact = findViewById(R.id.PContact);
PDesig = findViewById(R.id.PDesig);
CCity = findViewById(R.id.CCity);
mDatabase = FirebaseDatabase.getInstance();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.save, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
String dCName = CName.getText().toString().trim();
String dPContact = PContact.getText().toString().trim();
String dPDesig = PDesig.getText().toString().trim();
String dCCity = CCity.getText().toString().trim();
String name = CName.getText().toString().trim();
DatabaseReference mDbRef = mDatabase.getReference().child("Contacts").child(name).push();
mDbRef.child("cname").setValue(dCName);
mDbRef.child("pcontact").setValue(dPContact);
mDbRef.child("pdesig").setValue(dPDesig);
mDbRef.child("ccity").setValue(dCCity);
Toast.makeText(FormActivity.this, "Contact Submitted Successfully", Toast.LENGTH_LONG).show();
Intent intent = new Intent(FormActivity.this, HomeActivity.class);
startActivity(intent);
finish();
}
}
return super.onOptionsItemSelected(item);
}
}
Project.class
public class project {
String cname, pcontact, pdesig, ccity;
public project(){
}
public project(String cname, String pcontact, String pdesig, String ccity) {
this.cname = cname;
this.pcontact = pcontact;
this.pdesig = pdesig;
this.ccity = ccity;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public String getPcontact() {
return pcontact;
}
public void setPcontact(String pcontact) {
this.pcontact = pcontact;
}
public String getPdesig() {
return pdesig;
}
public void setPdesig(String pdesig) {
this.pdesig = pdesig;
}
public String getCcity() {
return ccity;
}
public void setCcity(String ccity) {
this.ccity = ccity;
}
}
HomeActivity.xml
<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:orientation="vertical"
tools:context=".HomeActivity">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:id="#+id/ProjList"
/>
</RelativeLayout>
ProjRow.xml
<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"
android:layout_margin="2dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/FCName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:elegantTextHeight="true"
android:textColor="#color/colorBlue"
android:maxLength="25"
android:paddingTop="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textSize="16dp"
android:textStyle="bold"
android:layout_marginTop="10dp"
/>
<TextView
android:id="#+id/FCLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_alignParentRight="true"
android:textAllCaps="true"
android:elegantTextHeight="true"
android:textColor="#color/colorDark"
android:layout_marginTop="13dp"
android:paddingTop="5dp"
android:textSize="14dp"
/>
<TextView
android:id="#+id/FPName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/FCName"
android:layout_alignParentLeft="true"
android:elegantTextHeight="true"
android:textStyle="bold"
android:textColor="#color/colorText"
android:paddingTop="5dp"
android:paddingRight="3dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:textSize="15dp"
/>
<TextView
android:id="#+id/FPDesig"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/FCName"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:layout_marginRight="5dp"
android:padding="5dp"
android:textSize="14dp"
android:textColor="#color/colorNext"
android:elegantTextHeight="true"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
the code is working fine without error. But the intended value is not captured from database. If push() is removed from DatabaseReference mDbRef = mDatabase.getReference().child("Contacts").child(name).push(); , it works absolutely fine. but it overwrites on existing data while adding new data for the same 'name'. After adding push, the data are getting stored in sub child record. Please guide me how to refer and get back the data in recycleview adapter.
The each record stored under 'Contacts' before adding push()but overwrites when added a person for the same company.
After adding push() each record stored under company name with unique key.

Screen not reflecting changes to Firebase database in Android

I'm writing a shopping list app that will save data to a Firebase Realtime database and update the screen in real time. As of right now I have it so that when you click the "+ List item" button, it adds a ShoppingListItem to the database with the name "test". It does that correctly, but it isn't updating the screen. I've double checked that the methods are being called, but the screen isn't reflecting the updated data. I have also tried using notifyDataSetChanged() and that hasn't seemed to help either.
This is the code for the ShoppingListFragment:
public class ShoppingListFragment extends Fragment {
private ArrayList<String> listItems = new ArrayList<String>();
private ListView mListView;
private TextView addItemButton;
private LinearLayout ll;
private RecyclerView rv;
private FirebaseRecyclerAdapter<ShoppingListItem, ShoppingListHolder> firebaseRecyclerAdapter;
private DatabaseReference mDatabase;
public ShoppingListFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
mDatabase = FirebaseDatabase.getInstance().getReference();
//get reference to user
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
Query query = mDatabase.child("users").child(uid).child("shopping");
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_shopping_list, container, false);
rv = view.findViewById(R.id.shopping_list);
rv.setLayoutManager(new LinearLayoutManager(getContext()));
// find the + List Item button
final TextView addCheckBox = view.findViewById(R.id.add_check_box);
addCheckBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addNewCheckBox(container);
}
});
FirebaseRecyclerOptions<ShoppingListItem> options = new FirebaseRecyclerOptions.Builder<ShoppingListItem>()
.setQuery(query, ShoppingListItem.class)
.build();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<ShoppingListItem, ShoppingListHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull ShoppingListHolder holder, int position, #NonNull ShoppingListItem item) {
holder.setItem(item);
}
#NonNull
#Override
public ShoppingListHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.shopping_list_item, parent, false);
return new ShoppingListHolder(view);
}
};
return view;
}
#Override
public void onStart() {
super.onStart();
firebaseRecyclerAdapter.startListening();
rv.setAdapter(firebaseRecyclerAdapter);
}
#Override
public void onStop() {
super.onStop();
if (firebaseRecyclerAdapter != null) {
firebaseRecyclerAdapter.stopListening();
}
}
public void addNewCheckBox(ViewGroup container) {
String key = mDatabase.child("users").child(getUid()).child("shopping_list").push().getKey();
ShoppingListItem item = new ShoppingListItem("test");
mDatabase.child("users").child(getUid()).child("shopping_list").child(key).setValue(item);
firebaseRecyclerAdapter.notifyDataSetChanged();
}
public int convertToPx(int input) {
DisplayMetrics metrics = getResources().getDisplayMetrics();
int px = (int) (input * ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT));
return px;
}
private class ShoppingListHolder extends RecyclerView.ViewHolder {
private final EditText itemName;
public ShoppingListHolder(View itemView) {
super(itemView);
itemName = itemView.findViewById(R.id.item_name);
}
public void setItem(ShoppingListItem item) {
itemName.setText(item.getItemName());
}
}
private String getUid() {
return FirebaseAuth.getInstance().getCurrentUser().getUid();
}
}
and the ShoppingListItem:
public class ShoppingListItem {
private String itemName;
private String type;
private String unit;
private int quantity;
private String location;
public ShoppingListItem() {
}
public ShoppingListItem(String name) {
this.itemName = name;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
}
XML for the shopping list view:
<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="match_parent"
android:id="#+id/home_fragment_main_layout"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.jggdevelopment.wannacook.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Shopping List"
android:textStyle="bold"
android:textSize="18sp"
android:layout_marginStart="16dp"
android:layout_marginBottom="16dp"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/shopping_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:clipToPadding="false"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/add_check_box"
android:text="+ List item"
android:textSize="16sp"
android:layout_marginStart="40dp"
android:layout_marginTop="16dp"/>
</LinearLayout>
and XML for each line item in the shopping list:
<?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:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/item_name"
android:layout_weight="1"
android:layout_marginStart="12dp"
android:layout_marginEnd="32dp"
android:inputType="text"
android:background="#android:color/transparent"
/>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:id="#+id/xbutton"
android:layout_marginEnd="12dp"
android:src="#drawable/ic_close_white_24dp"
android:layout_gravity="center_vertical"/>
</LinearLayout>
This is not working because you forgot to set the adapter in the onCreate() method. To solve this, add rv.setAdapter(firebaseRecyclerAdapter);, right before you are returning the view like this:
rv.setAdapter(firebaseRecyclerAdapter);
return view;
And you can remove that line of code from the onStart() method because is not needed there.
If you are interested, I have created a tutorial in which I'm explaining step by step, how to build a Shopping List App using Cloud Firestore and Android.

Android RecyclerView with different layouts doesn't work

I am trying to implement a RecyclerView with two different layouts. In the MainActivity I first create ListItem object and add it to the list and then add that to the adapter, but it doesn't show. And also I am calling a method that creates ListItem object and adds it to the list and then notifies the adapter, but it still doesn't show. Here is my code so far:
activity_main.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="nl.iansoft.www.recyclerviewproblem.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/rvList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="100dp"
/>
</android.support.constraint.ConstraintLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private ArrayList<ListItem> listItems;
private CustomAdapter customAdapter;
private RecyclerView rvList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rvList = (RecyclerView)findViewById(R.id.rvList);
listItems = new ArrayList<>();
ListItem listItem = new ListItem("MyName", "MyLatsName", "MyEmail");
listItems.add(listItem);
customAdapter = new CustomAdapter(listItems);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
rvList.setLayoutManager(mLayoutManager);
rvList.setItemAnimator(new DefaultItemAnimator());
rvList.setAdapter(customAdapter);
addItemToTheList();
}
private void addItemToTheList(){
ListItem listItem = new ListItem("MyName", "MyLatsName", "MyEmail");
listItems.add(listItem);
customAdapter.notifyDataSetChanged();
}
}
CustomAdapter.java
public class CustomAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private ArrayList<ListItem> listItems;
public class ListItem1ViewHolder extends RecyclerView.ViewHolder {
public TextView tvName;
public TextView tvLastName;
public TextView tvEmail;
public ListItem1ViewHolder(View view) {
super(view);
tvName = (TextView) view.findViewById(R.id.tvName);
tvLastName = (TextView) view.findViewById(R.id.tvLastName);
tvEmail = (TextView) view.findViewById(R.id.tvEmail);
}
}
public class ListItem2ViewHolder extends RecyclerView.ViewHolder {
public TextView tvName2;
public TextView tvLastName2;
public TextView tvEmail2;
public ListItem2ViewHolder(View view) {
super(view);
tvName2 = (TextView) view.findViewById(R.id.tvName2);
tvLastName2 = (TextView) view.findViewById(R.id.tvLastName2);
tvEmail2 = (TextView) view.findViewById(R.id.tvEmail2);
}
}
public CustomAdapter(ArrayList<ListItem> listItems) {
this.listItems = listItems;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView;
if(viewType == 1){
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_list_item, parent, false);
return new CustomAdapter.ListItem1ViewHolder(itemView);
}else{
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_list_item2, parent, false);
return new CustomAdapter.ListItem2ViewHolder(itemView);
}
}
#Override
public int getItemViewType(int position) {
if(listItems.get(position).getName().equals("MyName")){
return 1;
}else{
return 2;
}
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
ListItem listItem = listItems.get(position);
if(holder.getItemViewType() == 1) {
ListItem1ViewHolder listItem1ViewHolder = (ListItem1ViewHolder) holder;
listItem1ViewHolder.tvName.setText(listItem.getName());
listItem1ViewHolder.tvLastName.setText(listItem.getLastName());
listItem1ViewHolder.tvEmail.setText(listItem.getEmail());
}else{
ListItem2ViewHolder listItem2ViewHolder = (ListItem2ViewHolder)holder;
listItem2ViewHolder.tvName2.setText(listItem.getName());
listItem2ViewHolder.tvLastName2.setText(listItem.getLastName());
listItem2ViewHolder.tvEmail2.setText(listItem.getEmail());
}
}
#Override
public int getItemCount() {
return listItems.size();
}
}
ListItem.java
public class ListItem {
private String name;
private String lastName;
private String email;
public ListItem() {
}
public ListItem(String name, String lastName, String email) {
this.name = name;
this.lastName = lastName;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
my_list_item2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tvName2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="Name2"
android:layout_marginTop="15dp"
android:padding="5dp"/>
<TextView
android:id="#+id/tvLastName2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="Last Name2"
android:layout_below="#+id/tvName2"
android:layout_marginTop="10dp"
android:padding="5dp" />
<TextView
android:id="#+id/tvEmail2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="my#email.com"
android:layout_below="#+id/tvLastName2"
android:layout_marginTop="5dp"
android:padding="5dp" />
</RelativeLayout>
my_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="Name"
android:layout_marginTop="15dp"
android:padding="5dp"/>
<TextView
android:id="#+id/tvLastName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="Last Name"
android:layout_below="#+id/tvName"
android:layout_marginTop="10dp"
android:padding="5dp" />
<TextView
android:id="#+id/tvEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="my#email.com"
android:layout_below="#+id/tvLastName"
android:layout_marginTop="5dp"
android:padding="5dp" />
</RelativeLayout>
The issue is in ConstraintLayout. Because you haven't specified any constraints to your RecyclerView.
Either specify constraints, or move to another layout (e.g. FrameLayout).
But specifically in this case you do not need parent layout, because it is useless, as long as you have only RecyclerView in your view hierarchy.

Categories

Resources