gpt4 book ai didi

android - 使用自定义适配器不显示 SwipeCards

转载 作者:行者123 更新时间:2023-11-29 22:41:45 26 4
gpt4 key购买 nike

我正在使用这张刷卡开发类似 tinder 的应用 https://github.com/Diolor/Swipecards .我制作了一个自定义适配器,将数据设置为刷卡。一切都很好我检查了数据打印的logcat。我检查了 getItemCount() 的 logcat。它显示数据的大小。我的问题是数据未显示在刷卡中。

更新

这段代码中有两个方法

  1. getPeople() 运行良好,向刷卡显示数据。

  2. getNearbyPeople 也能正常工作,但数据未显示在刷卡中。这两种方法都使用相同的代码和适配器。有时它显示数据但花费的时间太长几乎半小时。一个在工作,另一个不工作。希望你明白我说的。提前致谢。

所有代码如下

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>

逻辑猫

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

最佳答案

我认为您的代码因后台线程而无法正常工作。您获取的数据在后台线程中。它们不在主线程中。

关于android - 使用自定义适配器不显示 SwipeCards,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58971750/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com