accessing an adapter's data via another adapte

2019-08-09 07:37发布

问题:

Is it all possible to access an adapter's data via another adapter? I want to start an activity and pass data from an adapter to a fragment which is used in TabLayout as one of three fragments, I have two adapters and a button which is clicked to start an activity, its Java code is in my first adapter and I need to pass second adapter's data via second adapter itself here is my codes:

my first adapter:

public class RecyclerViewDataAdapter extends RecyclerView.Adapter<RecyclerViewDataAdapter.ItemRowHolder>{

    private ArrayList<SectionDataModel> dataList;
    private Context mContext;
    private RecyclerView.RecycledViewPool recycledViewPool;
    private SnapHelper snapHelper;

    public RecyclerViewDataAdapter(ArrayList<SectionDataModel> dataList, Context mContext) {
        this.dataList = dataList;
        this.mContext = mContext;
        recycledViewPool = new RecyclerView.RecycledViewPool();
    }

    @Override
    public ItemRowHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, null);
        ItemRowHolder rowHolder = new ItemRowHolder(v);
        snapHelper = new GravitySnapHelper(Gravity.START);
        return rowHolder;
    }

    @Override
    public void onBindViewHolder(ItemRowHolder holder, int position) {

        ArrayList singleSectionItems = dataList.get(position).getAllItemInSection();

        final String sectionName = dataList.get(position).getHeaderTitle();
        holder.itemTitle.setText(sectionName);

        SectionDataAdapter adapter = new SectionDataAdapter(singleSectionItems, mContext);
        holder.recyclerView.setHasFixedSize(true);
        holder.recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        holder.recyclerView.setAdapter(adapter);
        holder.recyclerView.setRecycledViewPool(recycledViewPool);
        snapHelper.attachToRecyclerView(holder.recyclerView);


        holder.btnMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//here i can start the activity but..(second adapter)
                Toast.makeText(view.getContext(), sectionName, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return (null != dataList ? dataList.size() : 0);
    }

    public class ItemRowHolder extends RecyclerView.ViewHolder {
        protected ImageView mitemImage;
        protected TextView mitemtext;
        protected TextView itemTitle;
        protected RecyclerView recyclerView;
        protected Button btnMore;

        public ItemRowHolder(View itemView) {
            super(itemView);
            this.mitemImage = itemView.findViewById(R.id.itemImage);
            this.mitemtext = itemView.findViewById(R.id.tvTitle);
            this.itemTitle = itemView.findViewById(R.id.itemTitle);
            this.recyclerView = itemView.findViewById(R.id.recycler_view_list);
            this.btnMore = itemView.findViewById(R.id.btnMore);
        }
    }
} '

and my second adapter:

import java.net.PortUnreachableException;
import java.util.ArrayList;

public class SectionDataAdapter extends RecyclerView.Adapter<SectionDataAdapter.SssingleItemRowHolder>{

    private ArrayList<SingleItemModel> itemModels;
    private Context mContext;

    public SectionDataAdapter(ArrayList<SingleItemModel> itemModels, Context mContext) {
        this.itemModels = itemModels;
        this.mContext = mContext;
    }

@Override
public SingleItemRowHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_single_card, null);
    SingleItemRowHolder singleItemRowHolder = new SingleItemRowHolder(v);
    return singleItemRowHolder;
}

@Override
public void onBindViewHolder(SingleItemRowHolder holder, int position) {
    SingleItemModel itemModel = itemModels.get(position);
    holder.tvTitle.setText(itemModel.getName());
    holder.mitemImage.setImageResource(itemModel.getImage());

}


@Override
public int getItemCount() {
    return (null != itemModels ? itemModels.size() : 0);
}

public class SingleItemRowHolder extends RecyclerView.ViewHolder {

    protected TextView tvTitle;
    protected ImageView mitemImage;


    public SingleItemRowHolder(View itemView) {
        super(itemView);

        final Intent intent = new Intent(mContext,MainActivity.class);
        this.mitemImage = itemView.findViewById(R.id.itemImage);
        this.tvTitle = itemView.findViewById(R.id.tvTitle);
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//... need to start the activity from here
                Toast.makeText(view.getContext(), tvTitle.getText(), Toast.LENGTH_SHORT).show();
            }
        });

    }
}

回答1:

Like @MilaDroid said you simply need a getter that returns the another Adapter's ArrayList<SingleItemModel> but the problem you will face is that you need to have the same instance of the Adapter from the Activity in order to get the populated ArrayList<SingleItemModel>.

A good workaround is to used Bill Pugh's Singleton in the Adapter

public class Adapter {

private ArrayList<SingleItemModel> list;

private Adapter() {}

public static Adapter getInstance() {
    return InstInit.INSTANCE;
}

// Don't forget to set the list (or NPE)
// because we can't argue with a Singleton
public void setList(ArrayList<SingleItemModel> list) {
    this.list = list;
}

// You can now get the ArrayList
public ArrayList<SingleItemModel> getList() {
    return list;
}

private static class InstInit {
    private static final Adapter INSTANCE = new Adapter();
}

// Overrided RecyclerView.Adapter Methods
.................

}

Retrieving the ArrayList assuming that the following Adapters are Singleton

AdapterOne a1 = AdapterOne.getInstance();
AdapterTwo a2 = AdapterTwo.getInstance();

ArrayList<SingleItemModel> a1RetrievedList = a1.getList();
// You don't need to create a new instance
// creating a new instance doesn't make sense
// because you need to repopulate the list
// for the new instance.

ArrayList<SingleItemModel> a2RetrievedList = a2.getList();
// You can also retrieve from AdapterTwo