gpt4 book ai didi

java - 数组适配器未在 onDataChange() 方法中更新

转载 作者:行者123 更新时间:2023-12-02 11:25:14 44 4
gpt4 key购买 nike

这是我的ArrayAdapter:

public class SackViewAdapter extends ArrayAdapter<PostInfo> {
private ArrayList<PostInfo> postInfo;
private Context context;
private LayoutInflater inflater;

public SackViewAdapter(@NonNull Context context, int resource,ArrayList<PostInfo> postInfo) {
super (context, resource);
this.context = context;
this.postInfo = postInfo;
inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
Log.e ("Size", String.valueOf (postInfo.size ()));
return postInfo.size ();
}

@Nullable
@Override
public PostInfo getItem(int i) {
return postInfo.get (i);
}

@NonNull
@Override
public View getView(int i, View view, @NonNull ViewGroup parent) {
view = inflater.inflate (R.layout.card_sack_view, parent,false);
SelectableRoundedImageView imageView = view.findViewById (R.id.image_view);
TextView name = view.findViewById (R.id.nameCards);
TextView username = view.findViewById (R.id.usernameCards);
imageView.setDrawingCacheEnabled (true);
name.setText (postInfo.get (i).name);
username.setText (postInfo.get (i).username);
try{
Glide.with (context).load (postInfo.get (i).Url).into (imageView);
}catch (Exception e){
Toast.makeText (context, e.getMessage (), Toast.LENGTH_SHORT).show ();
}
Log.e ("i", String.valueOf (i));
return view;
}
}

这是我的主要 Activity :

public class WallActivity extends AppCompatActivity {

FloatingActionButton newFloatingButton;
SackViewAdapter baseAdapter;
ArrayList<PostInfo> postInfos;
DatabaseReference reference = FirebaseDatabase.getInstance ().getReference ();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_wall);
postInfos = new ArrayList<PostInfo> ();
String isDirectly = getIntent ().getStringExtra ("directly");
if(isDirectly.equals ("yes")){
AuthAsyncTask authAsyncTask = new AuthAsyncTask (WallActivity.this);
authAsyncTask.execute ();
}
getData ();
CardStackView cardStackView = findViewById(R.id.cardView);
newFloatingButton = findViewById(R.id.newFloatingButton);
newFloatingButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(WallActivity.this, NewItemActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_up, R.anim.slide_down);
}
});
cardStackView.setCardEventListener(new CardStackView.CardEventListener() {
@Override
public void onCardDragging(float percentX, float percentY) {

}

@Override
public void onCardSwiped(SwipeDirection direction) {

}

@Override
public void onCardReversed() {

}

@Override
public void onCardMovedToOrigin() {

}

@Override
public void onCardClicked(int index) {
Toast.makeText(WallActivity.this, "Clicked", Toast.LENGTH_SHORT).show();
}
});
baseAdapter = new SackViewAdapter (WallActivity.this,android.R.layout.simple_list_item_1, postInfos);
cardStackView.setAdapter(baseAdapter);
}

public void getData(){
reference.addValueEventListener (new ValueEventListener () {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
DataSnapshot posts = dataSnapshot.child ("Posts");
for (DataSnapshot time: posts.getChildren ()){
DataSnapshot url = time.child ("Url");
DataSnapshot name = time.child ("Name");
DataSnapshot username = time.child ("Username");
DataSnapshot date = time.child ("Date");
PostInfo postInfo = new PostInfo (String.valueOf (url.getValue ()), String.valueOf (name.getValue ()), String.valueOf (username.getValue ()), String.valueOf (date.getValue ()));
postInfos.add (postInfo);
}
baseAdapter.notifyDataSetChanged ();
}

@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText (WallActivity.this, "Error 411: " + databaseError.getMessage (), Toast.LENGTH_SHORT).show ();
}
});
}
}

当我从 getData() 函数获取所有数据时,我更新了数组适配器,但更新后数组列表的实际大小为 7,但 getView函数仅采用 0 到 2 之间的 i 值。它没有添加 ArrayList 中的所有值,而是一次又一次地显示相同的值,而不是显示不同的值。这是屏幕加载时的日志,因为我在 中添加了 Log.e >getView:

 04-06 13:04:26.797 10246-10246/lifeline.learn.com.hotornot E/Value of i: 0
04-06 13:04:26.804 10246-10246/lifeline.learn.com.hotornot E/Value of i: 1
04-06 13:04:26.814 10246-10246/lifeline.learn.com.hotornot E/Value of i: 2

它不会超过 2。但是当我登录 getCount 时,它返回 7。

最佳答案

目前,您的adaptergetCount()返回 urls.size (); 的大小但您还传递了其他 2 个数组列表,这只会占用 urls 的大小数组列表。

如果您通过 Arraylist<UserObject> ,您可以将所有数据添加到该对象,然后返回大小为 userObjects.size();

所以更好的方法是创建一个对象,比如 UserObject并使用该对象创建一个数组列表,如 Arraylist<UserObject>

UserObject.java

public class UserObject {
String urls;
String names;
String usernames;
String dates;

public UserObject(String urls, String names, String usernames, String dates) {
this.urls = urls;
this.names = names;
this.usernames = usernames;
this.dates = dates;
}
}

声明一个数组列表

ArrayList<UserObject> userData=new ArrayList<>();

现在,像这样改变

public void onDataChange(DataSnapshot dataSnapshot) {
DataSnapshot posts = dataSnapshot.child ("Posts");
for (DataSnapshot time: posts.getChildren ()){
DataSnapshot url = time.child ("Url");
DataSnapshot name = time.child ("Name");
DataSnapshot username = time.child ("Username");
DataSnapshot date = time.child ("Date");
UserObject user=new UserObject(String.valueOf (url.getValue ()),String.valueOf (name.getValue ()),String.valueOf (username.getValue ()),String.valueOf (date.getValue ()));
userData.add(user);
}
baseAdapter.notifyDataSetChanged ();
}

你的适配器构造函数将是这样的

public SackViewAdapter(@NonNull Context context, int resource,ArrayList<UserObject> userObjects)

关于java - 数组适配器未在 onDataChange() 方法中更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49686484/

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