Метод getView не вызывается для всех элементов списка - PullRequest
0 голосов
/ 20 мая 2019

У меня есть просмотр списка в другом просмотре списка в 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);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...