gpt4 book ai didi

java - 当所有数据结构都具有有效值时,数组列表和三个数组的客户适配器不会填充 ListView

转载 作者:行者123 更新时间:2023-12-01 09:15:07 27 4
gpt4 key购买 nike

我有一个数组列表和 3 个数组,它们都包含有效值。我试图将它们显示在数组列表中,但没有显示任何内容。没有任何错误。

这是我的适配器代码:

public class CustomAdapter extends BaseAdapter {
Context context;
ArrayList<String> brands;
int[] ard,chmd,brandTotal;
LayoutInflater inflater;
public CustomAdapter(Context context,ArrayList<String> brands,int[] ard, int[] chmd,int[] brandTotal){
this.context=context;
this.brands=brands;
this.ard=ard;
this.chmd=chmd;
this.brandTotal=brandTotal;
inflater = (LayoutInflater)context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return brands.size();
}

@Override
public Object getItem(int position) {
return position;
}
public class Holder
{
TextView brand;
TextView ard;
TextView chmd;
TextView total;

}

@Override
public long getItemId(int position) {
return position;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.reportview, parent);
holder.brand=(TextView) rowView.findViewById(R.id.brandText);
holder.ard=(TextView) rowView.findViewById(R.id.ardCount);
holder.chmd=(TextView) rowView.findViewById(R.id.chmdCount);
holder.total=(TextView) rowView.findViewById(R.id.totalCount);
holder.brand.setText(brands.get(position));
holder.ard.setText(String.valueOf(ard[position]));
holder.chmd.setText(String.valueOf(chmd[position]));
holder.total.setText(String.valueOf(brandTotal[position]));
return rowView;
}
}

这是我调用适配器的代码:

public class ReportFragment extends Fragment {


DatabaseReference db,mdb;
ArrayList<Item> items=new ArrayList<Item>();
ArrayList<String> brands =new ArrayList<>();
Query query;
ListView listView;
TextView t1,t2,t3;
public ReportFragment() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_report, container, false);
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
listView=(ListView)getView().findViewById(R.id.reportList);
t1=(TextView)getView().findViewById(R.id.ardTotal);
t2=(TextView)getView().findViewById(R.id.chmdTotal);
t3=(TextView)getView().findViewById(R.id.total);
db= FirebaseDatabase.getInstance().getReferenceFromUrl("https://stockmanager-142503.firebaseio.com/Sold");
mdb=FirebaseDatabase.getInstance().getReferenceFromUrl("https://stockmanager-142503.firebaseio.com/Brands");
query=db.orderByChild("date");
query.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Item i=dataSnapshot.getValue(Item.class);
items.add(i);
}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {

}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});
Query q=mdb.orderByChild("brandName");
q.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
brands.add(dataSnapshot.getValue(Brand.class).getBrandName());

}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {

}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});

}


t1.setText(String.valueOf(totalArd));
t2.setText(String.valueOf(totalChmd));
t3.setText(String.valueOf(itemCount));
listView.setAdapter(new CustomAdapter(getActivity(),brands,ard,chmd,brandTotal));

}
}

我的所有数组和数组列表中都有有效数据。我已经通过将它们全部打印出来进行了检查。无法弄清楚适配器为什么不工作。我也没有收到任何错误。请帮帮我。谢谢

最佳答案

您正在将 ListView 高度参数设置为wrap_content,将其更改为match_parent或为其指定固定大小(以dp为单位)

关于java - 当所有数据结构都具有有效值时,数组列表和三个数组的客户适配器不会填充 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40608113/

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