Deleting or hide An list item from Listview in FirebaseAdapter
I am developing an app for finding nearby donor. If any user needs blood they can post for blood..In notification fragment user will see who needs which blood? I want to make list view from notification table using firebaseAdapter but i want an user will not his/her notification..that's why in populateView() i checked if the id of finder(who post for blood) and the id of the currrent user matches or not..if matches i make the parrent layout of the layout that is using for listview VIEW.GONE...that helps to hide currrent user's notification but items remain empty in the list...that's why next items are added after that empty items..How can i remove that empty items or hide current user's notifications
Notification Fragment
package com.blz.prisoner.lifeshare;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.firebase.client.Firebase;
import com.firebase.ui.database.FirebaseListAdapter;
import com.firebase.ui.database.FirebaseListOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
*/
public class NotificationsFragment extends Fragment {
SwipeMenuListView listView;
FirebaseListAdapter adapter;
TextView notif_text,time_text,address_text,distance_text,phone;
LinearLayout notification_linear;
Query query;
View rootView;
FirebaseListOptions<NotificationData> options;
/* List<Integer> list_position = new ArrayList<Integer>();*/
FirebaseAuth firebaseAuth;
GPSTracker gps;
String lattitude,longtitude,phoneCall;
double latt1,longt1,latt2,longt2;
public NotificationsFragment() {
// Required empty public constructor
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView =inflater.inflate(R.layout.fragment_donors, container, false);
listView = rootView.findViewById(R.id.listview);
firebaseAuth = FirebaseAuth.getInstance();
gps = new GPSTracker(getActivity());
latt1 = gps.getLatitude();
longt1 = gps.getLongitude();
query =FirebaseDatabase.getInstance().getReference().child("Notifications");
setQuery(query);
SwipeMenuCreator creator = new SwipeMenuCreator() {
@Override
public void create(SwipeMenu menu) {
// create "delete" item
SwipeMenuItem deleteItem = new SwipeMenuItem(getActivity());
// set item background
deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
0x3F, 0x25)));
// set item width
deleteItem.setWidth(170);
// set a icon
deleteItem.setIcon(R.drawable.ic_phone);
// add to menu
menu.addMenuItem(deleteItem);
}
};
listView.setMenuCreator(creator);
listView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
switch (index) {
case 0:
// open
String s ="tel:"+phoneCall;
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse(s));
startActivity(intent);
break;
}
// false : close the menu; true : not close the menu
return false;
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView txt = view.findViewById(R.id.phone);
phoneCall = txt.getText().toString();
Toast.makeText(getActivity(),listView.getItemAtPosition(position).toString(),Toast.LENGTH_SHORT).show();
}
});
/*
}*/
//
return rootView;
}
@Override
public void onStart() {
super.onStart();
adapter.startListening();
}
@Override
public void onStop() {
super.onStop();
adapter.stopListening();
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.blood_group_menu,menu);
}
public void setQuery(Query query){
/*list_position.clear();*/
options = new FirebaseListOptions.Builder<NotificationData>()
.setLayout(R.layout.notificatiion_list)
.setQuery(query,NotificationData.class)
.build();
adapter = new FirebaseListAdapter(options) {
@Override
protected void populateView(@NonNull View v, @NonNull Object model, int position) {
NotificationData notif = (NotificationData) model;
notification_linear = v.findViewById(R.id.notification_linear);
if (notif.getUserid().toString().equals(firebaseAuth.getUid())){
notification_linear.setVisibility(View.GONE);
/*list_position.add(position);*/
/*Toast.makeText(getActivity(),Integer.toString(position),Toast.LENGTH_SHORT).show();*/
}
else if(!notif.getUserid().toString().equals(firebaseAuth.getUid())){
listView.setVisibility(View.VISIBLE);
notif_text = v.findViewById(R.id.notif_text);
time_text = v.findViewById(R.id.time_text);
address_text = v.findViewById(R.id.address_text);
distance_text = v.findViewById(R.id.distance_text);
phone = v.findViewById(R.id.phone_text);
String txt;
txt = notif.getFullName() + " Needs " + notif.getBloodGroup() + "Blood";
notif_text.setText(txt);
address_text.setText(notif.getAddress().toString());
phone.setText(notif.getPhone().toString());
lattitude = notif.getLattitude();
longtitude = notif.getLongitude();
latt2 = Double.parseDouble(lattitude);
longt2 = Double.parseDouble(longtitude);
float dist;
float result = new float[1];
android.location.Location.distanceBetween(latt1,longt1,latt2,longt2, result);
if(result.length>=1){
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
dist = result[0]/1000;
/*Toast.makeText(MainActivity.this,Float.toString(dist),Toast.LENGTH_LONG).show();*/
distance_text.setText(df.format(dist) + " Km");
}
Calendar calendar = Calendar.getInstance();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yy HH:mm:ss");
String dateStop = simpleDateFormat.format(calendar.getTime());
String dateStart = notif.getDateTime();
Date d1 = null;
Date d2 = null;
try {
d1 = simpleDateFormat.parse(dateStart);
d2 = simpleDateFormat.parse(dateStop);
//in milliseconds
long diff = d2.getTime() - d1.getTime();
/* long diffSeconds = diff / 1000 % 60;*/
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000) % 24;
long diffDays = diff / (24 * 60 * 60 * 1000);
if(diffMinutes<1){
time_text.setText("Now");
}
else if(diffMinutes>=1 && diffMinutes<60){
time_text.setText(Long.toString(diffMinutes) + " Minutes Ago");
}
else if(diffMinutes>=60 && diffHours<24){
time_text.setText(Long.toString(diffHours) + " Hours Ago");
}
else if(diffHours>=24){
time_text.setText(Long.toString(diffDays) + " Days Ago");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
listView.setAdapter(adapter);
adapter.startListening();
}
}
notification_fragment XML
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".NotificationsFragment">
<com.baoyz.swipemenulistview.SwipeMenuListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible">
</com.baoyz.swipemenulistview.SwipeMenuListView>
</FrameLayout>
notificatiion_list layout is used to create list view
notificatiion_list 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"
android:orientation="vertical"
android:paddingStart="15dp"
android:paddingLeft="15dp"
android:paddingEnd="10dp"
android:paddingRight="10dp"
android:paddingBottom="15dp"
android:background="@drawable/profile_backgroud"
tools:context=".MainActivity">
<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="Bishal Imtiaz Needs AB+ Blood"
android:layout_weight="8"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="20 minutes"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ago"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="20 minutes"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ago"/>
</LinearLayout>
</LinearLayout>
NotificationData class is used to store firebase data
NotificationData Class JAVA
package com.blz.prisoner.lifeshare;
public class NotificationData {
String fullName;
String dateTime;
String bloodGroup;
String phone;
String lattitude;
String longitude;
String address;
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
String userid;
public String getLattitude() {
return lattitude;
}
public void setLattitude(String lattitude) {
this.lattitude = lattitude;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public NotificationData(String fullName, String dateTime, String bloodGroup, String phone, String address, String lattitude, String longitude,String userid) {
this.fullName = fullName;
this.dateTime = dateTime;
this.bloodGroup = bloodGroup;
this.phone = phone;
this.lattitude = lattitude;
this.longitude = longitude;
this.address = address;
this.userid = userid;
}
public NotificationData(){
//empty constructor
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getDateTime() {
return dateTime;
}
public void setDateTime(String dateTime) {
this.dateTime = dateTime;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getBloodGroup() {
return bloodGroup;
}
public void setBloodGroup(String bloodGroup) {
this.bloodGroup = bloodGroup;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
Here is the screenshot of outputs
enter image description here
i don't want those empty items above..i need solution..if you have any alternative idea to do so tell me since i am noob developer :(
java
add a comment |
I am developing an app for finding nearby donor. If any user needs blood they can post for blood..In notification fragment user will see who needs which blood? I want to make list view from notification table using firebaseAdapter but i want an user will not his/her notification..that's why in populateView() i checked if the id of finder(who post for blood) and the id of the currrent user matches or not..if matches i make the parrent layout of the layout that is using for listview VIEW.GONE...that helps to hide currrent user's notification but items remain empty in the list...that's why next items are added after that empty items..How can i remove that empty items or hide current user's notifications
Notification Fragment
package com.blz.prisoner.lifeshare;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.firebase.client.Firebase;
import com.firebase.ui.database.FirebaseListAdapter;
import com.firebase.ui.database.FirebaseListOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
*/
public class NotificationsFragment extends Fragment {
SwipeMenuListView listView;
FirebaseListAdapter adapter;
TextView notif_text,time_text,address_text,distance_text,phone;
LinearLayout notification_linear;
Query query;
View rootView;
FirebaseListOptions<NotificationData> options;
/* List<Integer> list_position = new ArrayList<Integer>();*/
FirebaseAuth firebaseAuth;
GPSTracker gps;
String lattitude,longtitude,phoneCall;
double latt1,longt1,latt2,longt2;
public NotificationsFragment() {
// Required empty public constructor
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView =inflater.inflate(R.layout.fragment_donors, container, false);
listView = rootView.findViewById(R.id.listview);
firebaseAuth = FirebaseAuth.getInstance();
gps = new GPSTracker(getActivity());
latt1 = gps.getLatitude();
longt1 = gps.getLongitude();
query =FirebaseDatabase.getInstance().getReference().child("Notifications");
setQuery(query);
SwipeMenuCreator creator = new SwipeMenuCreator() {
@Override
public void create(SwipeMenu menu) {
// create "delete" item
SwipeMenuItem deleteItem = new SwipeMenuItem(getActivity());
// set item background
deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
0x3F, 0x25)));
// set item width
deleteItem.setWidth(170);
// set a icon
deleteItem.setIcon(R.drawable.ic_phone);
// add to menu
menu.addMenuItem(deleteItem);
}
};
listView.setMenuCreator(creator);
listView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
switch (index) {
case 0:
// open
String s ="tel:"+phoneCall;
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse(s));
startActivity(intent);
break;
}
// false : close the menu; true : not close the menu
return false;
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView txt = view.findViewById(R.id.phone);
phoneCall = txt.getText().toString();
Toast.makeText(getActivity(),listView.getItemAtPosition(position).toString(),Toast.LENGTH_SHORT).show();
}
});
/*
}*/
//
return rootView;
}
@Override
public void onStart() {
super.onStart();
adapter.startListening();
}
@Override
public void onStop() {
super.onStop();
adapter.stopListening();
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.blood_group_menu,menu);
}
public void setQuery(Query query){
/*list_position.clear();*/
options = new FirebaseListOptions.Builder<NotificationData>()
.setLayout(R.layout.notificatiion_list)
.setQuery(query,NotificationData.class)
.build();
adapter = new FirebaseListAdapter(options) {
@Override
protected void populateView(@NonNull View v, @NonNull Object model, int position) {
NotificationData notif = (NotificationData) model;
notification_linear = v.findViewById(R.id.notification_linear);
if (notif.getUserid().toString().equals(firebaseAuth.getUid())){
notification_linear.setVisibility(View.GONE);
/*list_position.add(position);*/
/*Toast.makeText(getActivity(),Integer.toString(position),Toast.LENGTH_SHORT).show();*/
}
else if(!notif.getUserid().toString().equals(firebaseAuth.getUid())){
listView.setVisibility(View.VISIBLE);
notif_text = v.findViewById(R.id.notif_text);
time_text = v.findViewById(R.id.time_text);
address_text = v.findViewById(R.id.address_text);
distance_text = v.findViewById(R.id.distance_text);
phone = v.findViewById(R.id.phone_text);
String txt;
txt = notif.getFullName() + " Needs " + notif.getBloodGroup() + "Blood";
notif_text.setText(txt);
address_text.setText(notif.getAddress().toString());
phone.setText(notif.getPhone().toString());
lattitude = notif.getLattitude();
longtitude = notif.getLongitude();
latt2 = Double.parseDouble(lattitude);
longt2 = Double.parseDouble(longtitude);
float dist;
float result = new float[1];
android.location.Location.distanceBetween(latt1,longt1,latt2,longt2, result);
if(result.length>=1){
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
dist = result[0]/1000;
/*Toast.makeText(MainActivity.this,Float.toString(dist),Toast.LENGTH_LONG).show();*/
distance_text.setText(df.format(dist) + " Km");
}
Calendar calendar = Calendar.getInstance();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yy HH:mm:ss");
String dateStop = simpleDateFormat.format(calendar.getTime());
String dateStart = notif.getDateTime();
Date d1 = null;
Date d2 = null;
try {
d1 = simpleDateFormat.parse(dateStart);
d2 = simpleDateFormat.parse(dateStop);
//in milliseconds
long diff = d2.getTime() - d1.getTime();
/* long diffSeconds = diff / 1000 % 60;*/
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000) % 24;
long diffDays = diff / (24 * 60 * 60 * 1000);
if(diffMinutes<1){
time_text.setText("Now");
}
else if(diffMinutes>=1 && diffMinutes<60){
time_text.setText(Long.toString(diffMinutes) + " Minutes Ago");
}
else if(diffMinutes>=60 && diffHours<24){
time_text.setText(Long.toString(diffHours) + " Hours Ago");
}
else if(diffHours>=24){
time_text.setText(Long.toString(diffDays) + " Days Ago");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
listView.setAdapter(adapter);
adapter.startListening();
}
}
notification_fragment XML
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".NotificationsFragment">
<com.baoyz.swipemenulistview.SwipeMenuListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible">
</com.baoyz.swipemenulistview.SwipeMenuListView>
</FrameLayout>
notificatiion_list layout is used to create list view
notificatiion_list 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"
android:orientation="vertical"
android:paddingStart="15dp"
android:paddingLeft="15dp"
android:paddingEnd="10dp"
android:paddingRight="10dp"
android:paddingBottom="15dp"
android:background="@drawable/profile_backgroud"
tools:context=".MainActivity">
<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="Bishal Imtiaz Needs AB+ Blood"
android:layout_weight="8"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="20 minutes"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ago"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="20 minutes"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ago"/>
</LinearLayout>
</LinearLayout>
NotificationData class is used to store firebase data
NotificationData Class JAVA
package com.blz.prisoner.lifeshare;
public class NotificationData {
String fullName;
String dateTime;
String bloodGroup;
String phone;
String lattitude;
String longitude;
String address;
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
String userid;
public String getLattitude() {
return lattitude;
}
public void setLattitude(String lattitude) {
this.lattitude = lattitude;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public NotificationData(String fullName, String dateTime, String bloodGroup, String phone, String address, String lattitude, String longitude,String userid) {
this.fullName = fullName;
this.dateTime = dateTime;
this.bloodGroup = bloodGroup;
this.phone = phone;
this.lattitude = lattitude;
this.longitude = longitude;
this.address = address;
this.userid = userid;
}
public NotificationData(){
//empty constructor
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getDateTime() {
return dateTime;
}
public void setDateTime(String dateTime) {
this.dateTime = dateTime;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getBloodGroup() {
return bloodGroup;
}
public void setBloodGroup(String bloodGroup) {
this.bloodGroup = bloodGroup;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
Here is the screenshot of outputs
enter image description here
i don't want those empty items above..i need solution..if you have any alternative idea to do so tell me since i am noob developer :(
java
Check it out. If you are removing the data from firebaseAdaper it will remove data from database too means the node will be removed. stackoverflow.com/questions/36252478/… If you do not want to remove data permanently then, Try to fetch the dataList from firebase and then populate it in RecyclerView. Now you can perform add and delete operations on this list without affecting the data in database.
– Sandeep Insan
Nov 25 '18 at 3:17
add a comment |
I am developing an app for finding nearby donor. If any user needs blood they can post for blood..In notification fragment user will see who needs which blood? I want to make list view from notification table using firebaseAdapter but i want an user will not his/her notification..that's why in populateView() i checked if the id of finder(who post for blood) and the id of the currrent user matches or not..if matches i make the parrent layout of the layout that is using for listview VIEW.GONE...that helps to hide currrent user's notification but items remain empty in the list...that's why next items are added after that empty items..How can i remove that empty items or hide current user's notifications
Notification Fragment
package com.blz.prisoner.lifeshare;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.firebase.client.Firebase;
import com.firebase.ui.database.FirebaseListAdapter;
import com.firebase.ui.database.FirebaseListOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
*/
public class NotificationsFragment extends Fragment {
SwipeMenuListView listView;
FirebaseListAdapter adapter;
TextView notif_text,time_text,address_text,distance_text,phone;
LinearLayout notification_linear;
Query query;
View rootView;
FirebaseListOptions<NotificationData> options;
/* List<Integer> list_position = new ArrayList<Integer>();*/
FirebaseAuth firebaseAuth;
GPSTracker gps;
String lattitude,longtitude,phoneCall;
double latt1,longt1,latt2,longt2;
public NotificationsFragment() {
// Required empty public constructor
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView =inflater.inflate(R.layout.fragment_donors, container, false);
listView = rootView.findViewById(R.id.listview);
firebaseAuth = FirebaseAuth.getInstance();
gps = new GPSTracker(getActivity());
latt1 = gps.getLatitude();
longt1 = gps.getLongitude();
query =FirebaseDatabase.getInstance().getReference().child("Notifications");
setQuery(query);
SwipeMenuCreator creator = new SwipeMenuCreator() {
@Override
public void create(SwipeMenu menu) {
// create "delete" item
SwipeMenuItem deleteItem = new SwipeMenuItem(getActivity());
// set item background
deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
0x3F, 0x25)));
// set item width
deleteItem.setWidth(170);
// set a icon
deleteItem.setIcon(R.drawable.ic_phone);
// add to menu
menu.addMenuItem(deleteItem);
}
};
listView.setMenuCreator(creator);
listView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
switch (index) {
case 0:
// open
String s ="tel:"+phoneCall;
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse(s));
startActivity(intent);
break;
}
// false : close the menu; true : not close the menu
return false;
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView txt = view.findViewById(R.id.phone);
phoneCall = txt.getText().toString();
Toast.makeText(getActivity(),listView.getItemAtPosition(position).toString(),Toast.LENGTH_SHORT).show();
}
});
/*
}*/
//
return rootView;
}
@Override
public void onStart() {
super.onStart();
adapter.startListening();
}
@Override
public void onStop() {
super.onStop();
adapter.stopListening();
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.blood_group_menu,menu);
}
public void setQuery(Query query){
/*list_position.clear();*/
options = new FirebaseListOptions.Builder<NotificationData>()
.setLayout(R.layout.notificatiion_list)
.setQuery(query,NotificationData.class)
.build();
adapter = new FirebaseListAdapter(options) {
@Override
protected void populateView(@NonNull View v, @NonNull Object model, int position) {
NotificationData notif = (NotificationData) model;
notification_linear = v.findViewById(R.id.notification_linear);
if (notif.getUserid().toString().equals(firebaseAuth.getUid())){
notification_linear.setVisibility(View.GONE);
/*list_position.add(position);*/
/*Toast.makeText(getActivity(),Integer.toString(position),Toast.LENGTH_SHORT).show();*/
}
else if(!notif.getUserid().toString().equals(firebaseAuth.getUid())){
listView.setVisibility(View.VISIBLE);
notif_text = v.findViewById(R.id.notif_text);
time_text = v.findViewById(R.id.time_text);
address_text = v.findViewById(R.id.address_text);
distance_text = v.findViewById(R.id.distance_text);
phone = v.findViewById(R.id.phone_text);
String txt;
txt = notif.getFullName() + " Needs " + notif.getBloodGroup() + "Blood";
notif_text.setText(txt);
address_text.setText(notif.getAddress().toString());
phone.setText(notif.getPhone().toString());
lattitude = notif.getLattitude();
longtitude = notif.getLongitude();
latt2 = Double.parseDouble(lattitude);
longt2 = Double.parseDouble(longtitude);
float dist;
float result = new float[1];
android.location.Location.distanceBetween(latt1,longt1,latt2,longt2, result);
if(result.length>=1){
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
dist = result[0]/1000;
/*Toast.makeText(MainActivity.this,Float.toString(dist),Toast.LENGTH_LONG).show();*/
distance_text.setText(df.format(dist) + " Km");
}
Calendar calendar = Calendar.getInstance();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yy HH:mm:ss");
String dateStop = simpleDateFormat.format(calendar.getTime());
String dateStart = notif.getDateTime();
Date d1 = null;
Date d2 = null;
try {
d1 = simpleDateFormat.parse(dateStart);
d2 = simpleDateFormat.parse(dateStop);
//in milliseconds
long diff = d2.getTime() - d1.getTime();
/* long diffSeconds = diff / 1000 % 60;*/
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000) % 24;
long diffDays = diff / (24 * 60 * 60 * 1000);
if(diffMinutes<1){
time_text.setText("Now");
}
else if(diffMinutes>=1 && diffMinutes<60){
time_text.setText(Long.toString(diffMinutes) + " Minutes Ago");
}
else if(diffMinutes>=60 && diffHours<24){
time_text.setText(Long.toString(diffHours) + " Hours Ago");
}
else if(diffHours>=24){
time_text.setText(Long.toString(diffDays) + " Days Ago");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
listView.setAdapter(adapter);
adapter.startListening();
}
}
notification_fragment XML
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".NotificationsFragment">
<com.baoyz.swipemenulistview.SwipeMenuListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible">
</com.baoyz.swipemenulistview.SwipeMenuListView>
</FrameLayout>
notificatiion_list layout is used to create list view
notificatiion_list 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"
android:orientation="vertical"
android:paddingStart="15dp"
android:paddingLeft="15dp"
android:paddingEnd="10dp"
android:paddingRight="10dp"
android:paddingBottom="15dp"
android:background="@drawable/profile_backgroud"
tools:context=".MainActivity">
<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="Bishal Imtiaz Needs AB+ Blood"
android:layout_weight="8"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="20 minutes"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ago"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="20 minutes"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ago"/>
</LinearLayout>
</LinearLayout>
NotificationData class is used to store firebase data
NotificationData Class JAVA
package com.blz.prisoner.lifeshare;
public class NotificationData {
String fullName;
String dateTime;
String bloodGroup;
String phone;
String lattitude;
String longitude;
String address;
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
String userid;
public String getLattitude() {
return lattitude;
}
public void setLattitude(String lattitude) {
this.lattitude = lattitude;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public NotificationData(String fullName, String dateTime, String bloodGroup, String phone, String address, String lattitude, String longitude,String userid) {
this.fullName = fullName;
this.dateTime = dateTime;
this.bloodGroup = bloodGroup;
this.phone = phone;
this.lattitude = lattitude;
this.longitude = longitude;
this.address = address;
this.userid = userid;
}
public NotificationData(){
//empty constructor
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getDateTime() {
return dateTime;
}
public void setDateTime(String dateTime) {
this.dateTime = dateTime;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getBloodGroup() {
return bloodGroup;
}
public void setBloodGroup(String bloodGroup) {
this.bloodGroup = bloodGroup;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
Here is the screenshot of outputs
enter image description here
i don't want those empty items above..i need solution..if you have any alternative idea to do so tell me since i am noob developer :(
java
I am developing an app for finding nearby donor. If any user needs blood they can post for blood..In notification fragment user will see who needs which blood? I want to make list view from notification table using firebaseAdapter but i want an user will not his/her notification..that's why in populateView() i checked if the id of finder(who post for blood) and the id of the currrent user matches or not..if matches i make the parrent layout of the layout that is using for listview VIEW.GONE...that helps to hide currrent user's notification but items remain empty in the list...that's why next items are added after that empty items..How can i remove that empty items or hide current user's notifications
Notification Fragment
package com.blz.prisoner.lifeshare;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.firebase.client.Firebase;
import com.firebase.ui.database.FirebaseListAdapter;
import com.firebase.ui.database.FirebaseListOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
*/
public class NotificationsFragment extends Fragment {
SwipeMenuListView listView;
FirebaseListAdapter adapter;
TextView notif_text,time_text,address_text,distance_text,phone;
LinearLayout notification_linear;
Query query;
View rootView;
FirebaseListOptions<NotificationData> options;
/* List<Integer> list_position = new ArrayList<Integer>();*/
FirebaseAuth firebaseAuth;
GPSTracker gps;
String lattitude,longtitude,phoneCall;
double latt1,longt1,latt2,longt2;
public NotificationsFragment() {
// Required empty public constructor
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView =inflater.inflate(R.layout.fragment_donors, container, false);
listView = rootView.findViewById(R.id.listview);
firebaseAuth = FirebaseAuth.getInstance();
gps = new GPSTracker(getActivity());
latt1 = gps.getLatitude();
longt1 = gps.getLongitude();
query =FirebaseDatabase.getInstance().getReference().child("Notifications");
setQuery(query);
SwipeMenuCreator creator = new SwipeMenuCreator() {
@Override
public void create(SwipeMenu menu) {
// create "delete" item
SwipeMenuItem deleteItem = new SwipeMenuItem(getActivity());
// set item background
deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
0x3F, 0x25)));
// set item width
deleteItem.setWidth(170);
// set a icon
deleteItem.setIcon(R.drawable.ic_phone);
// add to menu
menu.addMenuItem(deleteItem);
}
};
listView.setMenuCreator(creator);
listView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
switch (index) {
case 0:
// open
String s ="tel:"+phoneCall;
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse(s));
startActivity(intent);
break;
}
// false : close the menu; true : not close the menu
return false;
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView txt = view.findViewById(R.id.phone);
phoneCall = txt.getText().toString();
Toast.makeText(getActivity(),listView.getItemAtPosition(position).toString(),Toast.LENGTH_SHORT).show();
}
});
/*
}*/
//
return rootView;
}
@Override
public void onStart() {
super.onStart();
adapter.startListening();
}
@Override
public void onStop() {
super.onStop();
adapter.stopListening();
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.blood_group_menu,menu);
}
public void setQuery(Query query){
/*list_position.clear();*/
options = new FirebaseListOptions.Builder<NotificationData>()
.setLayout(R.layout.notificatiion_list)
.setQuery(query,NotificationData.class)
.build();
adapter = new FirebaseListAdapter(options) {
@Override
protected void populateView(@NonNull View v, @NonNull Object model, int position) {
NotificationData notif = (NotificationData) model;
notification_linear = v.findViewById(R.id.notification_linear);
if (notif.getUserid().toString().equals(firebaseAuth.getUid())){
notification_linear.setVisibility(View.GONE);
/*list_position.add(position);*/
/*Toast.makeText(getActivity(),Integer.toString(position),Toast.LENGTH_SHORT).show();*/
}
else if(!notif.getUserid().toString().equals(firebaseAuth.getUid())){
listView.setVisibility(View.VISIBLE);
notif_text = v.findViewById(R.id.notif_text);
time_text = v.findViewById(R.id.time_text);
address_text = v.findViewById(R.id.address_text);
distance_text = v.findViewById(R.id.distance_text);
phone = v.findViewById(R.id.phone_text);
String txt;
txt = notif.getFullName() + " Needs " + notif.getBloodGroup() + "Blood";
notif_text.setText(txt);
address_text.setText(notif.getAddress().toString());
phone.setText(notif.getPhone().toString());
lattitude = notif.getLattitude();
longtitude = notif.getLongitude();
latt2 = Double.parseDouble(lattitude);
longt2 = Double.parseDouble(longtitude);
float dist;
float result = new float[1];
android.location.Location.distanceBetween(latt1,longt1,latt2,longt2, result);
if(result.length>=1){
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
dist = result[0]/1000;
/*Toast.makeText(MainActivity.this,Float.toString(dist),Toast.LENGTH_LONG).show();*/
distance_text.setText(df.format(dist) + " Km");
}
Calendar calendar = Calendar.getInstance();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yy HH:mm:ss");
String dateStop = simpleDateFormat.format(calendar.getTime());
String dateStart = notif.getDateTime();
Date d1 = null;
Date d2 = null;
try {
d1 = simpleDateFormat.parse(dateStart);
d2 = simpleDateFormat.parse(dateStop);
//in milliseconds
long diff = d2.getTime() - d1.getTime();
/* long diffSeconds = diff / 1000 % 60;*/
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000) % 24;
long diffDays = diff / (24 * 60 * 60 * 1000);
if(diffMinutes<1){
time_text.setText("Now");
}
else if(diffMinutes>=1 && diffMinutes<60){
time_text.setText(Long.toString(diffMinutes) + " Minutes Ago");
}
else if(diffMinutes>=60 && diffHours<24){
time_text.setText(Long.toString(diffHours) + " Hours Ago");
}
else if(diffHours>=24){
time_text.setText(Long.toString(diffDays) + " Days Ago");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
listView.setAdapter(adapter);
adapter.startListening();
}
}
notification_fragment XML
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".NotificationsFragment">
<com.baoyz.swipemenulistview.SwipeMenuListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible">
</com.baoyz.swipemenulistview.SwipeMenuListView>
</FrameLayout>
notificatiion_list layout is used to create list view
notificatiion_list 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"
android:orientation="vertical"
android:paddingStart="15dp"
android:paddingLeft="15dp"
android:paddingEnd="10dp"
android:paddingRight="10dp"
android:paddingBottom="15dp"
android:background="@drawable/profile_backgroud"
tools:context=".MainActivity">
<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="Bishal Imtiaz Needs AB+ Blood"
android:layout_weight="8"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="20 minutes"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ago"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="20 minutes"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ago"/>
</LinearLayout>
</LinearLayout>
NotificationData class is used to store firebase data
NotificationData Class JAVA
package com.blz.prisoner.lifeshare;
public class NotificationData {
String fullName;
String dateTime;
String bloodGroup;
String phone;
String lattitude;
String longitude;
String address;
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
String userid;
public String getLattitude() {
return lattitude;
}
public void setLattitude(String lattitude) {
this.lattitude = lattitude;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public NotificationData(String fullName, String dateTime, String bloodGroup, String phone, String address, String lattitude, String longitude,String userid) {
this.fullName = fullName;
this.dateTime = dateTime;
this.bloodGroup = bloodGroup;
this.phone = phone;
this.lattitude = lattitude;
this.longitude = longitude;
this.address = address;
this.userid = userid;
}
public NotificationData(){
//empty constructor
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getDateTime() {
return dateTime;
}
public void setDateTime(String dateTime) {
this.dateTime = dateTime;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getBloodGroup() {
return bloodGroup;
}
public void setBloodGroup(String bloodGroup) {
this.bloodGroup = bloodGroup;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
Here is the screenshot of outputs
enter image description here
i don't want those empty items above..i need solution..if you have any alternative idea to do so tell me since i am noob developer :(
java
java
edited Nov 25 '18 at 9:41
Alex Mamo
44.9k82863
44.9k82863
asked Nov 24 '18 at 22:59
Bishal ImtiazBishal Imtiaz
11
11
Check it out. If you are removing the data from firebaseAdaper it will remove data from database too means the node will be removed. stackoverflow.com/questions/36252478/… If you do not want to remove data permanently then, Try to fetch the dataList from firebase and then populate it in RecyclerView. Now you can perform add and delete operations on this list without affecting the data in database.
– Sandeep Insan
Nov 25 '18 at 3:17
add a comment |
Check it out. If you are removing the data from firebaseAdaper it will remove data from database too means the node will be removed. stackoverflow.com/questions/36252478/… If you do not want to remove data permanently then, Try to fetch the dataList from firebase and then populate it in RecyclerView. Now you can perform add and delete operations on this list without affecting the data in database.
– Sandeep Insan
Nov 25 '18 at 3:17
Check it out. If you are removing the data from firebaseAdaper it will remove data from database too means the node will be removed. stackoverflow.com/questions/36252478/… If you do not want to remove data permanently then, Try to fetch the dataList from firebase and then populate it in RecyclerView. Now you can perform add and delete operations on this list without affecting the data in database.
– Sandeep Insan
Nov 25 '18 at 3:17
Check it out. If you are removing the data from firebaseAdaper it will remove data from database too means the node will be removed. stackoverflow.com/questions/36252478/… If you do not want to remove data permanently then, Try to fetch the dataList from firebase and then populate it in RecyclerView. Now you can perform add and delete operations on this list without affecting the data in database.
– Sandeep Insan
Nov 25 '18 at 3:17
add a comment |
1 Answer
1
active
oldest
votes
When using a FirebaseListAdapter, populateView() method is called for every child at that location. This means you are making a view for every child. Unfortunately, you cannot choose whether or not a view will appear for that item.
It looks like you're assuming that your if statement that you are using in the populateView() method will skip over those items but what is actually happening is that you're simply allowing an empty view to occupy that spot in your list.
What can you do instead to solve this issue, is to create a query that generates only the items of interest to your list. This means you'll have to tell Firebase to filter for children that meet your criteria.
There is also another approach in which you'll have to read the entire contents of the location, manually filter out the items you don't want to have in your list, and build a a new list of items you do want. You can then build an Adapter with that list, and it can then become the input to a ListView (or better, a RecyclerView).
Is there everything alright, can I help you with other informations?
– Alex Mamo
Nov 26 '18 at 6:42
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463107%2fdeleting-or-hide-an-list-item-from-listview-in-firebaseadapter%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
When using a FirebaseListAdapter, populateView() method is called for every child at that location. This means you are making a view for every child. Unfortunately, you cannot choose whether or not a view will appear for that item.
It looks like you're assuming that your if statement that you are using in the populateView() method will skip over those items but what is actually happening is that you're simply allowing an empty view to occupy that spot in your list.
What can you do instead to solve this issue, is to create a query that generates only the items of interest to your list. This means you'll have to tell Firebase to filter for children that meet your criteria.
There is also another approach in which you'll have to read the entire contents of the location, manually filter out the items you don't want to have in your list, and build a a new list of items you do want. You can then build an Adapter with that list, and it can then become the input to a ListView (or better, a RecyclerView).
Is there everything alright, can I help you with other informations?
– Alex Mamo
Nov 26 '18 at 6:42
add a comment |
When using a FirebaseListAdapter, populateView() method is called for every child at that location. This means you are making a view for every child. Unfortunately, you cannot choose whether or not a view will appear for that item.
It looks like you're assuming that your if statement that you are using in the populateView() method will skip over those items but what is actually happening is that you're simply allowing an empty view to occupy that spot in your list.
What can you do instead to solve this issue, is to create a query that generates only the items of interest to your list. This means you'll have to tell Firebase to filter for children that meet your criteria.
There is also another approach in which you'll have to read the entire contents of the location, manually filter out the items you don't want to have in your list, and build a a new list of items you do want. You can then build an Adapter with that list, and it can then become the input to a ListView (or better, a RecyclerView).
Is there everything alright, can I help you with other informations?
– Alex Mamo
Nov 26 '18 at 6:42
add a comment |
When using a FirebaseListAdapter, populateView() method is called for every child at that location. This means you are making a view for every child. Unfortunately, you cannot choose whether or not a view will appear for that item.
It looks like you're assuming that your if statement that you are using in the populateView() method will skip over those items but what is actually happening is that you're simply allowing an empty view to occupy that spot in your list.
What can you do instead to solve this issue, is to create a query that generates only the items of interest to your list. This means you'll have to tell Firebase to filter for children that meet your criteria.
There is also another approach in which you'll have to read the entire contents of the location, manually filter out the items you don't want to have in your list, and build a a new list of items you do want. You can then build an Adapter with that list, and it can then become the input to a ListView (or better, a RecyclerView).
When using a FirebaseListAdapter, populateView() method is called for every child at that location. This means you are making a view for every child. Unfortunately, you cannot choose whether or not a view will appear for that item.
It looks like you're assuming that your if statement that you are using in the populateView() method will skip over those items but what is actually happening is that you're simply allowing an empty view to occupy that spot in your list.
What can you do instead to solve this issue, is to create a query that generates only the items of interest to your list. This means you'll have to tell Firebase to filter for children that meet your criteria.
There is also another approach in which you'll have to read the entire contents of the location, manually filter out the items you don't want to have in your list, and build a a new list of items you do want. You can then build an Adapter with that list, and it can then become the input to a ListView (or better, a RecyclerView).
answered Nov 25 '18 at 9:40
Alex MamoAlex Mamo
44.9k82863
44.9k82863
Is there everything alright, can I help you with other informations?
– Alex Mamo
Nov 26 '18 at 6:42
add a comment |
Is there everything alright, can I help you with other informations?
– Alex Mamo
Nov 26 '18 at 6:42
Is there everything alright, can I help you with other informations?
– Alex Mamo
Nov 26 '18 at 6:42
Is there everything alright, can I help you with other informations?
– Alex Mamo
Nov 26 '18 at 6:42
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463107%2fdeleting-or-hide-an-list-item-from-listview-in-firebaseadapter%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Check it out. If you are removing the data from firebaseAdaper it will remove data from database too means the node will be removed. stackoverflow.com/questions/36252478/… If you do not want to remove data permanently then, Try to fetch the dataList from firebase and then populate it in RecyclerView. Now you can perform add and delete operations on this list without affecting the data in database.
– Sandeep Insan
Nov 25 '18 at 3:17