以下はリストビューのカスタム行のレイアウトです。ListViewが奇妙に更新されています
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/layoutTop"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Well Hello Motherfucker"
android:id="@+id/geventProfileName"
android:textSize="20sp"
android:paddingLeft="50dp"/>
</RelativeLayout>
<FrameLayout
android:id="@+id/layoutBottom"
android:layout_width="match_parent"
android:layout_height="300dp"
android:alpha="1"
android:layout_below="@+id/layoutTop"
android:layout_alignParentStart="true">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/no_picture"
android:id="@+id/geventEventPhoto"
android:layout_gravity="top|left"
android:alpha="1"/>
<TextView
android:layout_width="350dp"
android:layout_height="wrap_content"
android:text="asfda"
android:layout_gravity="center|left"
android:textSize="35sp"
android:textColor="#FFF"
android:textAllCaps="true"
android:id="@+id/geventTitle"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Read More"
android:textColor="@color/black"
android:id="@+id/geventReadMoreButton"
android:background="@drawable/readtab"
android:paddingTop="10dp"
android:paddingLeft="20dp"
android:layout_gravity="bottom|right"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="20dp"
android:id="@+id/geventDescription"
android:text=""
android:textColor="@color/black"
android:layout_gravity="top|left"/>
<TextView
android:layout_width="300dp"
android:layout_height="wrap_content"
android:id="@+id/geventDetails"
android:text=""
android:textSize="20sp"
android:textColor="#FFF"
android:layout_gravity="bottom|left"
/>
</FrameLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/layoutBottom"
android:orientation="horizontal"
android:background="#9E9E9E"
android:padding="5dp"
android:layout_marginTop="5dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="100"
android:id="@+id/numberOfLikesGEvent"
android:textColor="@color/black"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/numberOfLikesGEvent"
android:id="@+id/likeTextViewGEvent"
android:layout_marginLeft="10dp"
android:textColor="@color/black"
android:text="Like"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/dislikeTextViewGEvent"
android:layout_marginRight="10dp"
android:id="@+id/numberOfDislikesGEvent"
android:textColor="@color/black"
android:text="200"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/dislikeTextViewGEvent"
android:text="Dislike"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="0dp"
android:textColor="@color/black"/>
</RelativeLayout>
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/profile_image"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/photo"
app:civ_border_width="2dp"
app:civ_border_color="#FFF"/>
そして、これは私のCustomAdapter
public class CustomAdapter extends ArrayAdapter<GEvent> {
boolean hasLiked;
boolean hasDisliked;
FirebaseUser fbUser;
ArrayList<String> uids;
ArrayList<String> dUids;
GEvent currentEvent;
private TextView likeTextView;
private TextView numberOfLikes;
private TextView dislikeTextView;
private TextView numberOfDislikes;
private TextView readMoreButton;
private TextView eventDetails;
private TextView titleGevent;
private ImageView eventPhoto;
private TextView profileName;
private TextView descriptionGevent;
private String description;
private String titleForGEvent;
private String detailsGEvent;
private boolean hasLoaded=true;
private de.hdodenhof.circleimageview.CircleImageView circleImageView;
private Context mContext;
private boolean wantsToReadMore=false;
String profilePhotoLink;
PopupWindow pw;
public CustomAdapter(Context context, GEvent[] resource) {
super(context, R.layout.custom_row , resource);
this.mContext = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
final View customView = inflater.inflate(R.layout.example,parent,false);
final GEvent gEvent = getItem(position);
currentEvent = gEvent;
firebaseAuth = FirebaseAuth.getInstance();
uid = firebaseAuth.getCurrentUser().getUid();
titleGevent = (TextView) customView.findViewById(R.id.geventTitle);
descriptionGevent = (TextView) customView.findViewById(R.id.geventDescription);
profileName = (TextView) customView.findViewById(R.id.geventProfileName);
likeTextView = (TextView) customView.findViewById(R.id.likeTextViewGEvent);
numberOfLikes = (TextView) customView.findViewById(R.id.numberOfLikesGEvent);
numberOfDislikes =(TextView) customView.findViewById(R.id.numberOfDislikesGEvent);
dislikeTextView = (TextView) customView.findViewById(R.id.dislikeTextViewGEvent);
readMoreButton = (TextView) customView.findViewById(R.id.geventReadMoreButton);
eventPhoto = (ImageView) customView.findViewById(R.id.geventEventPhoto);
eventDetails = (TextView)customView.findViewById(R.id.geventDetails);
circleImageView = (CircleImageView) customView.findViewById(R.id.profile_image);
description = gEvent.getDescription();
detailsGEvent = gEvent.getgEventDate()+"\n"+gEvent.getgEventTime()+"\n"+gEvent.getVenue1()+"\n"+gEvent.getVenue2()+"\n"+gEvent.getContactEmail()+"\n"+gEvent.getContactPhoneNumber();
titleForGEvent = gEvent.getTitle();
if(gEvent.isHasPhoto()){
hasLoaded= false;
readMoreButton.setText("Loading");
}
readMoreButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(hasLoaded) {
Log.i("CA","Entered hasLoaded Function");
readMoreFunction();
}
else
Toast.makeText(mContext,"Loading....",Toast.LENGTH_LONG).show();
}
});
titleGevent.setText(gEvent.getTitle());
String uuid = gEvent.getUuid();
Boolean hasPhoto = gEvent.isHasPhoto();
profileName.setText(gEvent.getProfileUsername());
if(hasPhoto) {
final long ONE_MEGABYTE = 1024 * 1024 * 5;
pathRef.getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
Bitmap picturel = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
ImageView imageView = (ImageView) customView.findViewById(R.id.geventEventPhoto);
Drawable d = new BitmapDrawable(mContext.getResources(),picturel);
imageView.setBackground(d);
Log.i("CA","Entered hasRetrieved Photo and update hasLoaded");
readMoreButton.setText("Read More");
hasLoaded=true;
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
Log.i("Error","Couldn't Download");
}
});
}
return customView;
}
ます。public void readMoreFunction(){
Log.i("CA","Entered readMoreFunction Function");
if(wantsToReadMore){ //wants to read less now
Log.i("CA","Entered Read Less Function");
eventPhoto.setAlpha(1f);
descriptionGevent.setText("");
eventDetails.setText("");
readMoreButton.setText("Read More");
titleGevent.setText(titleForGEvent);
wantsToReadMore=false;
}
else{ //Wants to read more
Log.i("CA","Entered Read More Function");
eventPhoto.setAlpha(0.5f);
descriptionGevent.setText(description);
eventDetails.setText(detailsGEvent);
titleGevent.setText("");
readMoreButton.setText("Read Less");
wantsToReadMore=true;
}
です
私の問題は、このCustomAdapterを呼び出すたびに、リストアイテムが1つしかないときは、それが完全に動作することです。ただし、複数のリストアイテムがあるときはいつも不思議な動作をします。たとえば、2つのアイテムがある場合はいつでも、最初のアイテムの「Read More」ボタンを押すと、それが動作しているデータが最初のアイテムそのものであることをログに表示しても、2番目のアイテムがトリガーされます。
2つ以上のアイテムがある場合、上記のパターンが観察されます。つまり、アイテムの[Read More]ボタンをクリックするたびに、リストビューの次のアイテムがトリガーされます。画像がロードされた後、または[Read More]ボタンが応答しなくても、「読み込み中」に固執することがあります。
私はバグを探して過去2日間頭を叩いています。皆さんが私を助けてくれることを願っています。アドバンスで感謝します。
PS: - "Load More"ボタンが外れています。 GEvent
クラスインサイド
あなたのモデルである「GEvent」内の各項目の状態を保存する必要があります。また 'readMoreFunction'メソッドは古いデータを使用しています。 'getView'の最後の反復の値を常に持ちます。 –
現在のデータを使用して、getViewの古いデータを防ぐ方法はありますか? – THGRoy