RecyclerView适配器-页脚项打开了一个新的RecyclerView

时间:2019-03-06 06:06:59

标签: java android android-recyclerview recycler-adapter notifydatasetchanged

我正在创建一个应用程序,允许用户在日程表创建过程中将“活动卡”添加到日程表中。

  1. 我的footeritem在名为HFAdapter的RecyclerView适配器中。该适配器在我的“计划”类中。
  2. 当用户单击footeritem中的“添加活动卡”时,我希望CardLibrary类recyclerview以dialog形式打开。我的CardLibrary类本身是一个android活动,并且具有3列的名为'LibraryArrAdapter'的自己的recyclerview适配器。它显示SQLite数据库中的所有卡。我在HFAdapter类中创建了一个新的LibraryArrAdapter的原因是因为我想将列数更改为2。
  3. 一旦用户在dialog中选择了一张卡片,对话框就会关闭并且card appears in the schedule
  4. 我的LibraryArrAdapter运行良好,因为它可以为CardLibrary类提供所需的结果。
  5. 所有用户卡都存储在SQLite数据库中,其中包含标题,图像和activity_duration列。但是我只想在“计划”课程中显示标题和图像。
  6. 我的“ Schedule”课程仍然是一项新活动,但我没有做任何事情,因此在下面未包含其课程代码。

在HFAdapter类中,我已经有一个对象“卡”(CardListItem的实例),该对象存储用户在对话框中选择的卡的标题和图像,它位于MyViewHolder构造函数中。如何将这个卡片对象添加到ArrayList中,并使页脚始终保持为ArrayList中的最后一项?我可以在HFAdapter类中做到吗?

要在适配器中获得页脚项,我的方法和类主要基于https://demonuts.com/android-recyclerview-with-header-and-footer/

我真的希望得到一些帮助,因为我花了太多时间在此上。

HFAdapter类:

public class HFAdapter extends RecyclerView.Adapter<HFAdapter.MyViewHolder> {

RecyclerView recyclerView;

public static final int Normal = 1;
public static final int Footer = 2;

private LayoutInflater inflater;
private ArrayList<CardListItem> cardItemArrayList;
CardListItem card;

DatabaseHelper dbHelper;
SQLiteDatabase db;
Context context;
Cursor cursor;
byte[] image;

public HFAdapter(Context context, ArrayList<CardListItem> cardItemArrayList){
    inflater = LayoutInflater.from(context);
    this.cardItemArrayList = cardItemArrayList;
}

class MyViewHolder extends RecyclerView.ViewHolder
{
    TextView cardTitle;
    ImageView imageView;
    LinearLayout list_item,add_card;

    public MyViewHolder(View itemView, int viewType) {
        super(itemView);

        if(viewType == Normal) {
            cardTitle = itemView.findViewById(R.id.activity_title);
            imageView = itemView.findViewById(R.id.imgView);
            list_item = itemView.findViewById(R.id.schedule_list_item);
        }else if(viewType == Footer){
            add_card = itemView.findViewById(R.id.add_card);
            add_card.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    CardLibrary cardLib = new CardLibrary();
                    Dialog dialog = new Dialog(context);
                    dialog.setContentView(R.layout.activity_card_library);
                    recyclerView = dialog.findViewById(R.id.myRecyclerView);
                    int numberOfColumns = 2;
                    recyclerView.setLayoutManager(new GridLayoutManager(context, numberOfColumns));
                    LibraryArrAdapter adapter = new LibraryArrAdapter(context,cardLib.getAllCards());
                    recyclerView.setAdapter(adapter);
                    dialog.show();
                    final Dialog thisDialog = dialog;
                    LinearLayout a_card = recyclerView.findViewById(R.id.single_card);
                    dbHelper = new DatabaseHelper(context);
                    db = dbHelper.getReadableDatabase();
                    a_card.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            TextView txtvw_title = recyclerView.findViewById(R.id.item_title);
                            String title = txtvw_title.getText().toString();
                            cursor = db.rawQuery("SELECT image WHERE title=?",new String[]{title});
                            if(cursor.getCount()>0) {
                                cursor.moveToFirst();
                                image = cursor.getBlob(cursor.getColumnIndex(DatabaseHelper.COLUMN_IMAGE));
                            }
                            card = new CardListItem(title,image);
                            thisDialog.dismiss();
                        }
                    });
                }
            });
        }
    }
}

public void addCard(CardListItem card){
    cardItemArrayList.add(card);
    this.notifyDataSetChanged();
}



@Override
public int getItemViewType(int position) {
    if(cardItemArrayList.get(position).getViewType().equals("footer")){
        return Footer;
    }else {
        return Normal;
    }
}

@NonNull
@Override
public HFAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view ;
    MyViewHolder holder;

    switch (viewType){
        case Normal:
            view = inflater.inflate(R.layout.schedule_row_layout, parent, false);
            holder = new MyViewHolder(view , Normal);
            break;

        case Footer:
            view = inflater.inflate(R.layout.blank_row_layout, parent, false);
            holder = new MyViewHolder(view , Footer);
            break;

        default:
            view = inflater.inflate(R.layout.blank_row_layout, parent, false);
            holder = new MyViewHolder(view , Normal);
            break;
    }
    return holder;
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    if(cardItemArrayList.get(position).getViewType().equals("normal")) {
        holder.cardTitle.setText(card.getTitle());
        holder.imageView.setImageBitmap(card.getImage());
    }//cardItemArrayList.get(position)
}

@Override
public int getItemCount() {
    return cardItemArrayList.size();
}
}

CardListItem类:(用于HFAdapter Arraylist中的1个项目)

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class CardListItem {

String title;
byte[] image;
String viewType;

public CardListItem(String activityName, byte[] image){
    this.title = activityName;
    this.image = image;
}

public String getViewType(){
    return viewType;
}

public void setViewType(String viewType){
    this.viewType = viewType;
}

public String getTitle() {
    return title;
}

public void setTitle(String text) {
    this.title = text;
}

public Bitmap getImage(){
    Bitmap b = BitmapFactory.decodeByteArray(image,0,image.length);
    return b;
}

public void setImage(byte[] image) {
    this.image = image;
}
}

非常感谢您!

0 个答案:

没有答案