Android:listview没有意外

时间:2015-06-11 07:07:14

标签: java android listview android-listview android-arrayadapter

我在片段内创建一个列表视图,但它没有显示我卡住了。我正在将数据从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;
    }}

OrderDetails.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);  }

帮助PLZ

1 个答案:

答案 0 :(得分:1)

执行此操作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);