Пользовательский ListView внутри пользовательского списка, имеющий повторяющиеся данные
Я использую пользовательский просмотр списка внутри пользовательского просмотра списка. Для этих двух списков у меня есть два класса модели. Первый просмотр списка предназначен для магазина, а внутренний просмотр списка предназначен для товаров магазина. Поэтому для этого я создаю два класса моделей (Store, Item). I Класс StoreModel Я взял объект класса ItemModel и назначил его в цикле хранения.
Ниже мой код
ArrayList<CartStoreModel> cartStoreArray = new ArrayList<>();
for (int storeCount = 0; storeCount < jsonStoreArray.length(); storeCount++) {
JSONObject jsonStoreObj = jsonStoreArray.getJSONObject(storeCount);
CartStoreModel storeModel = new CartStoreModel();
JSONArray jsonProArray = jsonStoreObj.getJSONArray("products");
for (int itemCount = 0; itemCount < jsonProArray.length(); itemCount++) {
JSONObject jsonproObj = jsonProArray.getJSONObject(itemCount);
CartItemModel itemModel = new CartItemModel();
itemModel.setItemId(Integer.parseInt(jsonproObj.get("proId").toString()));
storeModel.setItemModel(itemModel);
}
storeModel.setProductCount(jsonProArray.length());
cartStoreArray.add(storeModel);
}
listView.setAdapter(new CartStoreAdapter(context, cartStoreArray));
Класс CartStoreAdapter находится ниже
class CartStoreAdapter extends BaseAdapter {
Context context;
ArrayList<CartStoreModel> storeArrayList = new ArrayList<>();
ArrayList<CartItemModel> itemModelArrayList = new ArrayList<>();
public CartStoreAdapter(Context context, ArrayList<CartStoreModel> storeArrayList) {
this.context = context;
this.storeArrayList = storeArrayList;
}
@Override
public int getCount() {
return storeArrayList.size();
}
@Override
public CartStoreModel getItem(int position) {
if (storeArrayList != null) {
return storeArrayList.get(position);
} else {
return null;
}
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
convertView = inflater.inflate(R.layout.cart_store_rowlist, parent, false);
}
ListView listView = convertView.findViewById(R.id.cartStoreItemListview);
CartStoreModel storeModel = getItem(position);
for (int i = 0; i < storeModel.getProductCount(); i++) {
CartItemModel cartItemModel = storeModel.getItemModel();
itemModelArrayList.add(cartItemModel);
listView.setAdapter(new CartStoreItemAdapter(context, itemModelArrayList));
}
return convertView;
}
}
Код CartStoreItemAdapter находится ниже
class CartStoreItemAdapter extends BaseAdapter {
ArrayList<CartItemModel> itemModel = new ArrayList<>();
public CartStoreItemAdapter(Context context, ArrayList<CartItemModel> itemModel) {
this.itemModel = itemModel;
}
@Override
public int getCount() {
return itemModel.size();
}
@Override
public CartItemModel getItem(int position) {
if (itemModel != null) {
return itemModel.get(position);
} else {
return null;
}
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
convertView = inflater.inflate(R.layout.cart_store_item_rowlist, parent, false);
}
TextView itemName = convertView.findViewById(R.id.itemName);
CartItemModel itemModel = getItem(position);
itemName.setText(itemModel.getItemName());
return convertView;
}
}
Предметы магазина одинаковы во внутреннем списке.