gpt4 book ai didi

java - Android:自定义ListView不会立即填充

转载 作者:行者123 更新时间:2023-12-02 08:46:37 25 4
gpt4 key购买 nike

我想创建一个由自定义 ArrayAdapter 填充的自定义 ListView。代码工作正常,但 ListView 没有立即显示数据。为了显示数据,我需要单击同一页面的随机 EditText,关闭键盘,ListView 会神奇地显示数据。我不知道这是否会产生影响,但适配器在 fragment 中使用,该 fragment 在 OnCreate 方法期间从 Firebase 检索数据。在我的代码下面,我删除了本主题不需要的所有内容并简化了数组。

这是我的数组内的元素类:

public class ListItem {

private String Description;
private String Price;

public ListItem (String Description, String Price) {
this.Description = Description;
this.Price = Price;
}

public String getDescription() { return Description; }

public String getPrice() {
return Price;
}

public void setDescription(String description) {
Description = description;
}

public void setPrice(String price) {
Price = price;
}

}

适配器如下:

public class CustomListAdapter extends ArrayAdapter<ListItem> {

private Context mContext;
private Integer mResource;

private static class ViewHolder {
TextView txt_description_item;
TextView txt_price_item;
}

public CustomListAdapter(@NonNull Context context, int resource, @NonNull ArrayList<ListItem> objects) {
super(context, resource, objects);
mContext = context;
mResource = resource;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

String Description = getItem(position).getDescription();
String Price = getItem(position).getPrice();

LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(mResource, parent, false);

ViewHolder viewHolder = new ViewHolder();

viewHolder.txt_description_item = convertView.findViewById(R.id.txt_description_item);
viewHolder.txt_price_item = convertView.findViewById(R.id.txt_price_item);

viewHolder.txt_description_item.setText(Description);
viewHolder.txt_price_item.setText(Price);

return convertView;
}

}

最后这是 fragment 代码:

public class MyFragment extends Fragment {

private ListView view_list_items;
private FirebaseDatabase database;
private DatabaseReference refDatabase;
private static final String USER = "user";

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.fragment_myprofile, container, false);
view_list_items = v.findViewById(R.id.list_items);

database = FirebaseDatabase.getInstance();
refDatabase = database.getReference(USER);

refDatabase.child("items").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

final ArrayList<ListItem> arr_items = new ArrayList<>();

for (DataSnapshot ds : dataSnapshot.child("itemlist1").getChildren()) {
final String description = ds.getKey();
final String price = ds.getValue(String.class);

ListItem listItem = new ListItem(description,price);
arr_items.add(listItem);
}

CustomListAdapter adapter = new CustomListAdapter(getContext(),R.layout.layout_myItemList, arr_items);
view_list_items.setAdapter(adapter);
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});



}

编辑:我通过将适配器设置在“for”循环内解决了这个问题。也许这不是最优雅的解决方案,但效果很好

for (DataSnapshot ds : dataSnapshot.child("itemlist1").getChildren()) {
final String description = ds.getKey();
final String price = ds.getValue(String.class);

ListItem listItem = new ListItem(description,price);
arr_items.add(listItem);

CustomListAdapter adapter = new
CustomListAdapter(getContext(),R.layout.layout_myItemList, arr_items);
view_list_items.setAdapter(adapter);
}

最佳答案

Firebase 数据库是异步的,即编译器不会等待 onDataChange() 或 onCancelled() 内的代码返回值。它将立即返回除这 2 个方法内的值之外的所有值,而对于这 2 个方法,它将在数据库中提供该值时返回该值。下面的代码可能会帮助您更好地理解

 refDatabase.child("items").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

System.out.println("Inside onDataChange method");
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});
System.out.println ("Outside onDataChange method");

输出如下:

Outside onDataChange method
Inside onDataChange method

请注意,首先出现在onDataChange 方法之外。这就是您的 listView 不立即显示数据的原因。为了保持用户的参与度,您可以在 View 中添加一个微调器,该微调器在从数据库中获取数据之前一直可见。为此,请尝试此代码。

        database = FirebaseDatabase.getInstance();
refDatabase = database.getReference(USER);
spinner.setVisibility(View.VISIBLE);

refDatabase.child("items").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

spinner.setVisibility(View.INVISIBLE);
//Your code goes here
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
//Error code goes here
}
});

关于java - Android:自定义ListView不会立即填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61041638/

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