gpt4 book ai didi

java - Android: ListView 未显示

转载 作者:行者123 更新时间:2023-12-01 11:26:21 24 4
gpt4 key购买 nike

我正在 fragment 内创建一个 ListView ,但它没有显示我卡住了。我正在将数据从 mainActivity.java 传递到 OrderDetailsAdapter.java 但 getview() 函数没有被调用并且 getcount 返回非零这是我的代码

OrderDetailsAdapter.java

public class OrderDetailsAdapter extends ArrayAdapter<OrderDetails> {
Context context;
int resource;
private List<OrderDetails> orderList = new ArrayList<OrderDetails>();

public OrderDetailsAdapter(Context context, int resource) {
super(context, resource);
this.context = context;
this.resource = resource;
}

@Override
public void add(OrderDetails object) {
orderList.add(object);
Log.v("getcount", "addItems " + getCount());
super.add(object);
}

@Override
public int getCount() {
int size = orderList.size();
Log.v("getcount","getcount "+ size);
return orderList.size();
}

@Override
public OrderDetails getItem(int index) {
return orderList.get(index);
}


@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Log.v("getcount","I am getting called");
View row = convertView;
OrderDetailsHolder holder = null;
if(row == null)
{
LayoutInflater layoutInflater = LayoutInflater.from(context);

row = layoutInflater.inflate(resource,parent,false);

holder = new OrderDetailsHolder();
holder.patientName = (TextView)row.findViewById(R.id.order_list_view_patient_name);
holder.price = (TextView)row.findViewById(R.id.order_list_view_price);
holder.medicineList = (TextView)row.findViewById(R.id.order_list_view_medicine_list);
holder.expirationTime = (TextView)row.findViewById(R.id.order_list_view_expiration_time);
holder.expirationText = (TextView)row.findViewById(R.id.order_list_view_order_expire_text);
holder.cancelOrder = (TextView)row.findViewById(R.id.order_list_view_cancel_order);
holder.openOrder = (TextView)row.findViewById(R.id.order_list_view_open_order);

//Assigning custom fonts
Typeface typeface = Typeface.createFromAsset(context.getAssets(),"fonts/gothic.ttf");
holder.patientName.setTypeface(typeface);
holder.price.setTypeface(typeface);
holder.expirationTime.setTypeface(typeface);
holder.medicineList.setTypeface(typeface);
holder.expirationText.setTypeface(typeface);
holder.cancelOrder.setTypeface(typeface);
holder.openOrder.setTypeface(typeface);
row.setTag(holder);
}
else {
holder = (OrderDetailsHolder)row.getTag();
}

final OrderDetails details = getItem(position);
Log.v("AAAA",details.toString());
// OrderDetails orderDetails1 = orderList[position];
holder.patientName.setText(details.getPatientName());
holder.price.setText(String.valueOf(details.getPrice()) +" /-");
holder.medicineList.setText(Arrays.toString(details.getMedicineList()));
holder.expirationTime.setText(String.valueOf(details.expirationTime)+" mins");
return row;
}

static class OrderDetailsHolder {
TextView patientName;
TextView price;
TextView medicineList;
TextView expirationTime;
TextView expirationText;
TextView openOrder;
TextView cancelOrder;
}}

订单详细信息.java

public class OrderDetails {
public String patientName;
public float price;
public String medicineList[];
public int expirationTime;

public OrderDetails()
{
super();
}

public OrderDetails(String patientName,float price,String[] medicineList,int expirationTime)
{
this.patientName = patientName;
this.price = price;
this.medicineList = medicineList;
this.expirationTime = expirationTime;
}

public String getPatientName(){
return patientName;
}

public float getPrice() { return price; }

public String[] getMedicineList() { return medicineList; }

public int getExpirationTime() { return expirationTime; }}

MainActivity.java

 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.mipmap.ic_icon_home);
TextView userAddress = (TextView) findViewById(R.id.navigationDrawerHeaderTextView2);
userAddress.setText("52N 12, Pratap Nagar");
replaceFragment(R.layout.fragment_order_list, null);


//Set adapter for Order List
String medicineList[] = {"Paracetamol","Crocin","Azithomycin","Strepsils"};
orderDetails = new OrderDetails("Ravi Gupta",220,medicineList,20);
OrderDetailsAdapter orderDetailsAdapter = new OrderDetailsAdapter(this,R.layout.order_list_view_items);

orderDetailsAdapter.add(orderDetails);
orderDetailsAdapter.add(orderDetails);
orderDetailsAdapter.add(orderDetails);
orderDetailsAdapter.add(orderDetails); }

请帮忙

最佳答案

这样做public OrderDetailsAdapter(Context context, int resource,List<OrderDetails> orderList) {
super(context, resource);
this.context = context;
this.resource = resource;
this.orderList = orderList;
}
还有这个OrderDetailsAdapter orderDetailsAdapter = new OrderDetailsAdapter(this,R.layout.order_list_view_items,orderList);

关于java - Android: ListView 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30773901/

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