CODE:
private static final String CREATE_DETAILEDMEALS = "CREATE TABLE "
+ TABLE_DetailedMeals + "(" + KEY_ID + " INTEGER PRIMARY KEY,"
+ mealsName + " TEXT unique," + categoryId + " INTEGER,"
+ desc + " TEXT," + size + " TEXT,"
+ price + " TEXT" + ")";
public void addDetailedMeals(String name, int catId, String d, String s, String p){
ContentValues values = new ContentValues(1);
values.put(mealsName, name);
values.put(categoryId, catId);
values.put(desc, d);
values.put(size, s);
values.put(price, p);
getWritableDatabase().insert(TABLE_DetailedMeals, null, values);
}
public Cursor getMeals2(int id)
{
Cursor cursor = getReadableDatabase().rawQuery("SELECT Name, Description, Size, Price FROM " + TABLE_DetailedMeals + " WHERE " + categoryId + "=" + String.valueOf(id), null);
return cursor;
}
// ID1 = MEALS
public ArrayList<DetailedMeals> GetDetailedMeals(int ID1)
{
ArrayList<DetailedMeals> list = new ArrayList<DetailedMeals>();
Cursor cursor = getMeals2(ID1);
int i = 0; // Iterator for 'do-while'. While it gets Categories names, it shall catch all the url's from array as well.
if (cursor.moveToFirst())
{
do
{
DetailedMeals cat = new DetailedMeals();
cat.setName(cursor.getString(cursor.getColumnIndex(mealsName))); //mealsName
cat.setDescription(cursor.getString(cursor.getColumnIndex(mealsName)));
cat.setSize(cursor.getString(cursor.getColumnIndex(mealsName)));
cat.setPrice(cursor.getString(cursor.getColumnIndex(mealsName)));
switch (ID1) {
case 1: cat.setImage(PIZZA_image_urls[i]);
break;
case 2: cat.setImage(BEER_image_urls[i]);
break;
default:
break;
}
list.add(cat);
i++;
}
while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed())
{
cursor.close();
}
return list;
}
适配器:
public class DifferentRowAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
FragmentActivity c;
ArrayList<DetailedMeals> MealsList;
private final int MEAL = 0, DETAIL = 1;
public DifferentRowAdapter(FragmentActivity c, ArrayList<DetailedMeals> MealsList) {
this.c = c;
this.MealsList = MealsList;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
switch (viewType) {
case MEAL:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rec_item, parent, false);
return new mViewHolder(view);
case DETAIL:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.details_item, parent, false);
return new dViewHolder(view);
}
return null;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
DetailedMeals object = MealsList.get(position);
if (object != null) {
switch (object.getmType()) {
case MEAL:
((mViewHolder) holder).mNameTextView.setText(object.getName());
Glide.with(c)
.load(MealsList.get(position).getImage())
.diskCacheStrategy(DiskCacheStrategy.ALL) // Saves the media item after all transformations to cache and
// Saves just the original data to cache.
.placeholder(R.mipmap.ic_launcher)
.into(((mViewHolder) holder).img);
break;
case DETAIL:
((dViewHolder) holder).descTextView.setText(object.getDescription());
((dViewHolder) holder).sizeTextView.setText(object.getSize());
((dViewHolder) holder).priceTextView.setText(object.getPrice());
break;
}
}
}
@Override
public int getItemCount() {
if (MealsList == null)
return 0;
return MealsList.size();
}
@Override
public int getItemViewType(int position) {
if (MealsList != null) {
DetailedMeals object = MealsList.get(position);
if (object != null) {
return object.getmType();
}
}
return 0;
}
public class dViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView descTextView;
TextView sizeTextView;
TextView priceTextView;
detailsItemClickListener icl;
public dViewHolder(View itemView) {
super(itemView);
// Get references to desc, size, price
descTextView = (TextView) itemView.findViewById(R.id.desc);
sizeTextView = (TextView) itemView.findViewById(R.id.size);
priceTextView = (TextView) itemView.findViewById(R.id.price);
itemView.setOnClickListener(this);
}
public void setItemClickListener(detailsItemClickListener icl)
{
this.icl = icl;
}
@Override
public void onClick(View v){
this.icl.onItemClick(v, getPosition());
}
}
public class mViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView mNameTextView;
ImageView img;
mealsItemClickListener icl;
public mViewHolder(View itemView) {
super(itemView);
// Get references to image and name.
mNameTextView = (TextView) itemView.findViewById(R.id.name);
img = (ImageView) itemView.findViewById(R.id.category_image);
itemView.setOnClickListener(this);
}
public void setItemClickListener(mealsItemClickListener icl)
{
this.icl = icl;
}
@Override
public void onClick(View v){
this.icl.onItemClick(v, getPosition());
}
}
}
DetailedMeals.class
public class DetailedMeals {
private String name;
private String image;
private String description;
private String size;
private String price;
private int id;
private int mType;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public int getmType() {
return mType;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
现在,GetDetailedMeals(..)仅返回详细信息名称(不是名称/ desc /价格/大小)等。而且,在我只使用Meal名称获得recyclelerview之前,它会尝试返回它。
答案 0 :(得分:0)
将您的DifferentRowAdapter扩展为RecyclerView.Adapter,因为您使用的视图在DifferentRowAdapter中。
答案 1 :(得分:0)
list.add(cat);
将 DetailedMeals 放入 DiffRowAdapter 列表中
所以改变
public ArrayList<DifferentRowAdapter> GetDetailedMeals(int ID1)
到
public ArrayList<DetailedMeals> GetDetailedMeals(int ID1)
拥有相同的列表和适配器。 然后把结果听到知道会发生什么。
<强>更新强>
更改后,您只需在视图中显示名称,因此在mViewHolder中添加更多项目,如price或desc:
public mViewHolder(View itemView) {
super(itemView);
// Get references to image and name.
mNameTextView = (TextView) itemView.findViewById(R.id.name);
// Add them hear
mPriceTextView = (TextView) itemView.findViewById(R.id.price);
mDescTextView = (TextView) itemView.findViewById(R.id.desc);
img = (ImageView) itemView.findViewById(R.id.category_image);
itemView.setOnClickListener(this);
}