2016-12-12 12 views
0

CODE:の収集特定の情報[SQLiteの]

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/price/sizeではない)ect。また、Mealの名前だけを持ったrecyclerviewを得る前に、それを返そうとします。

私の目標はここにある:あなたが使用しているviewholderがDifferentRowAdapter内にあるのでhttps://img.exs.lv/l/a/lat-deels/fragm_1.png

+0

あなたは 'DiffRowAdapter'sのリストを持っていますそれに 'DetailedMeals'を入れようとします。犬のリストに車を置くことはできません* –

+0

レルムの使い方を学ぶ必要があります。私を信じて。データを簡単かつ迅速に保存できます。カーソルで読み取り値を作成する必要はなく、その多くがコンパイラー・エラーを引き起こすという面白い構文で表を宣言する必要はありません。 – Theo

+0

@Theo、提案ありがとう。それに見えるだろうが、私はこれを解決するのが好きだ。 –

答えて

0

変更あなたのDifferentRowAdapterがRecyclerView.Adapterに及びます。あなたは

だから同じリストやアダプタを持っている

public ArrayList<DetailedMeals> GetDetailedMeals(int ID1) 

public ArrayList<DifferentRowAdapter> GetDetailedMeals(int ID1) 

を変更DiffRowAdapterリストにDetailedMealsを入れているあなたのコード内で

0

list.add(cat); 。 次にput resultは何が起こるかを知るために聞きます。

更新:

それらを変更した後、あなたは自分のビューに名前を示しているので、mViewHolder価格または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); 
} 
+0

それはうまくいったが、元の投稿をもう一度チェックしてください - 今起こっていることを更新しました。私はDBから情報を返す私の方法は間違っていると思う。私は間違った方法で列を選択すると思います。 –

+0

カーソルの列が正しいかどうかを知るには、トーストのようなものを表示するか、ログを取得します....データが正しい場合は、カーソルが正常であることを確認してください... –

+0

mNameTextView = (TextView)itemView.findViewById(R.id.name); –

関連する問題