Android: listview is not displying

86 views Asked by At

I am creating an list view inside fragment but it is not displaying i have stuck.I am passing data from mainActivity.java to OrderDetailsAdapter.java but getview() function is not getting called and getcount returning nonzero Here is my code

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

HELP PLZ

1

There are 1 answers

4
Dipesh Dhakal On BEST ANSWER

do this public OrderDetailsAdapter(Context context, int resource,List<OrderDetails> orderList) { super(context, resource); this.context = context; this.resource = resource; this.orderList = orderList; } and this OrderDetailsAdapter orderDetailsAdapter = new OrderDetailsAdapter(this,R.layout.order_list_view_items,orderList);