I'm developing an app like tinder using this swipe card https://github.com/Diolor/Swipecards . I made a custom adapter which set data to swipecard. everything is fine i checked logcat where data printing. i checked logcat of getItemCount(). it showing data's size. my problem is data not showing in the swipecard.
Update
In this code there are two methods
getPeople() which is working perfectly, showing data to the swipe cards.
getNearbyPeople which is also working but data not showing in the swipe cards. both the methods using same codes and adapter. sometimes its shows data but takes too long time almost half an hour. one is working and another one doesn't. hope you understand what i said. thanks in advance.
All codes are below
MainActivity.class
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import com.firebase.geofire.GeoFire;
import com.firebase.geofire.GeoLocation;
import com.firebase.geofire.GeoQueryDataEventListener;
import com.firebase.geofire.core.GeoHash;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.lorentzos.flingswipe.SwipeFlingAdapterView;
import com.pk.tinder.R;
import com.pk.tinder.cards.Card;
import com.pk.tinder.cards.CardAdapter;
import com.pk.tinder.matches.MatchActivity;
import java.util.ArrayList;
import de.hdodenhof.circleimageview.CircleImageView;
public class MainActivity extends AppCompatActivity {
FusedLocationProviderClient client;
private CardAdapter cardAdapter;
ArrayList<Card> cardList;
SwipeFlingAdapterView flingContainer;
CardView cardViewAccountButton;
CardView cardViewLike, cardViewDislike, cardViewSuperLike;
FirebaseAuth mAuth;
FirebaseUser mFirebaseUser;
DatabaseReference mUsersDb;
private String currentUid;
private String userSex;
private String oppositeUserSex;
GeoFire geoFire;
GeoLocation geoLocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
flingContainer = findViewById(R.id.frame);
cardList = new ArrayList<Card>();
//firebase
mAuth = FirebaseAuth.getInstance();
mFirebaseUser = mAuth.getCurrentUser();
currentUid = mFirebaseUser.getUid();
//database
mUsersDb = FirebaseDatabase.getInstance().getReference().child("Users");
//location updates
geoFire = new GeoFire(mUsersDb);
client = LocationServices.getFusedLocationProviderClient(this);
final LocationRequest locationRequest = new LocationRequest();
locationRequest.setInterval(1000);
locationRequest.setFastestInterval(100);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
client.requestLocationUpdates(locationRequest,
new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
if (locationResult != null) {
geoLocation = new GeoLocation(locationResult.getLastLocation().getLatitude(),
locationResult.getLastLocation().getLongitude());
GeoHash geoHash = new GeoHash(geoLocation);
if (mAuth.getCurrentUser() != null) {
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Users")
.child(mAuth.getCurrentUser().getUid());
ref.child("g").setValue(geoHash.getGeoHashString());
ref.child("l").child("0").setValue(geoLocation.latitude);
ref.child("l").child("1").setValue(geoLocation.longitude);
}
}
}
},
Looper.getMainLooper())
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
}
});
//getPeople();
showNearbyPeople();
flingContainer.setAdapter(cardAdapter);
}
//data fetching from firebase without location
public void getPeople() {
mUsersDb.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
Log.d("log", dataSnapshot.getValue().toString());
if (dataSnapshot.exists()){
Log.d("log", dataSnapshot.getValue().toString());
Card card;
card = new Card(dataSnapshot.child("account").child("name").getValue().toString(),
dataSnapshot.getKey(),
profileImageUrl);
cardList.add(card);
cardAdapter = new CardAdapter(getApplicationContext(), R.layout.swipe_card_item, cardList);
cardAdapter.notifyDataSetChanged();
Log.d("log386", String.valueOf(cardAdapter.getItemCount()));
}
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
//data fetching through firebase with nearby location
private void showNearbyPeople() {
Log.d("loggeo", geoLocation.toString());
geoFire.queryAtLocation(geoLocation, 5)
.addGeoQueryDataEventListener(new GeoQueryDataEventListener() {
@Override
public void onDataEntered(DataSnapshot dataSnapshot, GeoLocation location) {
Card card = null;
if (dataSnapshot.exists(){
Log.d("log", dataSnapshot.toString());
card = new Card(dataSnapshot.child("account").child("name").getValue().toString(),
dataSnapshot.getKey(),
profileImageUrl);
Log.d("log 382", "Name :" + card.getName() + "key :" + card.getUid() + " url :" + card.getProfileImageUrl());
cardList.add(card);
cardAdapter = new CardAdapter(getApplicationContext(), R.layout.swipe_card_item, cardList);
cardAdapter.notifyDataSetChanged();
Log.d("log386", String.valueOf(cardAdapter.getItemCount()));
Log.d("log 387", "Name :" + cardList.get(0).getName() + "key :" + cardList.get(0).getUid() + " url :" + cardList.get(0).getProfileImageUrl());
}
}
@Override
public void onDataExited(DataSnapshot dataSnapshot) {
}
@Override
public void onDataMoved(DataSnapshot dataSnapshot, GeoLocation location) {
}
@Override
public void onDataChanged(DataSnapshot dataSnapshot, GeoLocation location) {
}
@Override
public void onGeoQueryReady() {
}
@Override
public void onGeoQueryError(DatabaseError error) {
}
});
}
}
CardAdapter.class
public class CardAdapter extends ArrayAdapter<Card> {
private ArrayList<Card> cards = new ArrayList<>();
private Context context;
public CardAdapter(@NonNull Context context, int resourceId, ArrayList<Card> cards) {
super(context, resourceId, cards);
this.cards = cards;
this.context = context;
notifyDataSetChanged();
}
public View getView(int position, View convertView, @NotNull ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.swipe_card_item, parent, false);
}
Card card = cards.get(position);
Log.d("logsize", String.valueOf(getItemCount()));
Log.d("logadapter", "Name :" + card.getName() + "uId :" + card.getUid() + "url :" + card.getProfileImageUrl());
TextView textViewName = convertView.findViewById(R.id.textViewFullName);
ImageView imageViewPhoto = convertView.findViewById(R.id.imageViewPhoto);
textViewName.setText(card.getName());
Glide.with(getContext()).load(card.getProfileImageUrl()).centerCrop().into(imageViewPhoto);
Log.d("log32", "Name :" + card.getName() + "uId :" + card.getUid() + "url :" + card.getProfileImageUrl());
return convertView;
}
public int getItemCount() {
return cards.size();
}
@Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
}
Card.class
public class Card {
private String name;
private String uid;
private String profileImageUrl;
private String location;
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getProfileImageUrl() {
return profileImageUrl;
}
public void setProfileImageUrl(String profileImageUrl) {
this.profileImageUrl = profileImageUrl;
}
public Card(String name, String uid, String profileImageUrl) {
this.name = name;
this.uid = uid;
this.profileImageUrl = profileImageUrl;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
}
swipe_card_item.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="16dp"
android:background="#fff"
android:elevation="2dp"
app:cardCornerRadius="16dp">
<ImageView
android:id="@+id/imageViewPhoto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher_foreground" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="vertical"
android:background="@drawable/gradient1"
android:padding="16dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|start"
android:orientation="horizontal">
<TextView
android:id="@+id/textViewFullName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:textColor="@color/colorWhite"
android:textSize="20sp"
tools:text="UserName" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="8dp"
android:gravity="center"
android:text="19"
android:textColor="@color/colorWhite"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mumbai, India"
android:textColor="@color/colorWhite" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</FrameLayout>
Logcat
2019-11-23 13:20:41.409 16591-16656/com.pk.tindertest D/log375: Name :Sanjukey :UJykVjc9RuQNPPehHbbYZ0PpY8h1 url :https://firebasestorage.googleapis.com/v0/b/tinder-pk.appspot.com/o/profileImages%2FUJykVjc9RuQNPPehHbbYZ0PpY8h1?alt=media&token=3bf3130d-4940-4bef-904f-a520894f1d81
2019-11-23 13:20:41.410 16591-16656/com.pk.tindertest D/log377: Name :Menkakey :73iFRWoBcJW02m2W1QU1gW1glLH3 url :https://firebasestorage.googleapis.com/v0/b/tinder-pk.appspot.com/o/profileImages%2F73iFRWoBcJW02m2W1QU1gW1glLH3?alt=media&token=0ee8adc3-f483-41b9-910c-ef5017a98dfe
2019-11-23 13:20:41.410 16591-16656/com.pk.tindertest D/log381: 5
2019-11-23 13:20:45.011 16591-16617/com.pk.tindertest V/FA: Inactivity, disconnecting from the service
2019-11-23 13:21:00.496 16591-16596/com.pk.tindertest I/zygote64: Do partial code cache collection, code=60KB, data=38KB
2019-11-23 13:21:00.496 16591-16596/com.pk.tindertest I/zygote64: After code cache collection, code=60KB, data=38KB
2019-11-23 13:21:00.496 16591-16596/com.pk.tindertest I/zygote64: Increasing code cache capacity to 256KB
2019-11-23 13:22:56.359 16591-16596/com.pk.tindertest I/zygote64: Do full code cache collection, code=123KB, data=82KB
2019-11-23 13:22:56.364 16591-16596/com.pk.tindertest I/zygote64: After code cache collection, code=97KB, data=58KB
I think your code isn't working because of background thread. your fetched data is in background thread.They are not in the main thread.