并非为列表的所有元素调用getView方法

时间:2019-05-20 07:55:16

标签: java android android-listview

我在Android的另一个列表视图中有一个列表视图。但是内层的getView方法只旋转了一圈。

尽管getCount方法返回5。

  

这是我的适配器类:

public class ProductListAdapter extends BaseAdapter {
    private ArrayList<Product> productList;
    private Context context;

    public ProductListAdapter(Context context, int orderListId) {
        super();
        this.context = context;
        productList = new ArrayList<Product>();

        DatabaseAccess dbAccess = new DatabaseAccess(context);
        Cursor c = dbAccess.getDb().rawQuery("SELECT * FROM orderList WHERE id = ?", new String[]{Integer.toString(orderListId)});
        c.moveToFirst();
        while (!c.isAfterLast()) {
            for (int i = 2; i < 7; i++) {
                Cursor c1 = dbAccess.getDb().rawQuery("SELECT * FROM products WHERE id = ?", new String[]{c.getString(i)});
                c1.moveToFirst();
                Product product = new Product(c1.getInt(0), c1.getString(2), c1.getInt(3), c1.getInt(4), c1.getInt(5));
                productList.add(product);
            }
            c.moveToNext();
        }
        dbAccess.close();
    }

    @Override
    public int getCount() {
        return productList.size();
    }

    @Override
    public Object getItem(int position) {
        return productList.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.product_list_layout, parent, false);
        }
        TextView nameTxt = convertView.findViewById(R.id.product_name);
        TextView priceTxt = convertView.findViewById(R.id.product_price);
        TextView discountTxt = convertView.findViewById(R.id.product_discount);

        nameTxt.setText(productList.get(position).getName());
        priceTxt.setText(Integer.toString(productList.get(position).getPrice()));
        discountTxt.setText(Integer.toString(productList.get(position).getOffer()) + "%");
        return convertView;
    }
}

然后我在外部列表中称呼适配器:

ProductListAdapter productListsAdapter = new ProductListAdapter(context, orderLists.get(position).getId());
productsLst.setAdapter(productListsAdapter);

0 个答案:

没有答案