我正在创建一个应用程序。在这个应用程序中,我有一个recyclerview。我的单行Recyclerview有一些按钮。当用户点击该按钮时,我需要启动一个actvity(或执行weservice调用)。我需要通过此适配器将包含回收器视图的片段中的一些数据传递给新活动。按钮单击在适配器中发生。我陷入了这个问题并发现很难解决这个问题,任何人都可以帮助我如何解决这个问题。欢迎提出所有建议。
我的片段代码:
/**
* A simple {@link Fragment} subclass.
*/
public class Toadline extends Fragment implements ToadlineAdapter.ClickListener{
public Toadline() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_toadline, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.recyclerViewTimeline);
context = getContext();
postHeader = getArguments().getStringArrayList("PostHeader");
ToadlineAdapter adapter = new ToadlineAdapter(context, getData1());
recyclerView.setAdapter(adapter);
adapter.setClickListener(this);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
});
return view;
}
@Override
public void itemClicked (View view,int position){
}
public static List<TimelineDataStore> getData1() {
List<TimelineDataStore> data = new ArrayList<>();
int[] icons = {R.mipmap.human_image, R.mipmap.human_image, R.mipmap.human_image, R.mipmap.human_image, R.mipmap.human_image, R.mipmap.human_image, R.mipmap.human_image};
Bitmap[] images = profileImageAfterDownload;
ArrayList<String> titles = postHeader;
for (int i = 0; i < titles.size() && i < icons.length && i < noOfDays.size() && i < postTitle.size(); i++) {
TimelineDataStore current = new TimelineDataStore();
current.images = images[i];
//current.iconId = icons[i];
current.title = titles.get(i);
data.add(current);
}
return data;
}
}
我的适配器代码:
import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Collections;
import java.util.List;
import toadways.ways.toad.toadways.R;
/**
* Created by Toadways Admin on 19-11-2015.
*/
public class ToadlineAdapter extends RecyclerView.Adapter<ToadlineAdapter.MyViewHolder> {
private ClickListener clickListener;
private SwipeRefreshLayout.OnRefreshListener clickListener1;
private LayoutInflater inflater;
List<TimelineDataStore> data = Collections.EMPTY_LIST;
public ToadlineAdapter(Context context, List<TimelineDataStore> data) {
inflater = LayoutInflater.from(context);
this.data = data;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.single_card_row, parent, false);
MyViewHolder viewHolder = new MyViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
TimelineDataStore current = data.get(position);
holder.title.setText(current.title);
holder.images.setImageBitmap(current.images);
}
@Override
public int getItemCount() {
return data.size();
}
public void setClickListener(ClickListener clickListener) {
this.clickListener = clickListener;
}
public void setClickListener1(SwipeRefreshLayout.OnRefreshListener clickListener1) {
this.clickListener1 = clickListener1;
}
// View Holder object for Recycler View
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener {
TextView title, noOfDays, postTitle, agreeCount, disAgreeCount, neutralCount, conversationCount, postDescription;
ImageView icon, postImage, images;
CardView mCardView;
Button mShowMore, mShowLess;
public MyViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener((View.OnClickListener) this);
title = (TextView) itemView.findViewById(R.id.textViewTitle);
images = (ImageView) itemView.findViewById(R.id.imageViewUser);
noOfDays = (TextView) itemView.findViewById(R.id.textViewNoOfDays);
postTitle = (TextView) itemView.findViewById(R.id.textViewPostDescription);
postImage = (ImageView) itemView.findViewById(R.id.postImage);
agreeCount = (TextView) itemView.findViewById(R.id.textViewAgreeCount);
disAgreeCount = (TextView) itemView.findViewById(R.id.textViewDisAgreeCount);
neutralCount = (TextView) itemView.findViewById(R.id.textViewNeutralCount);
conversationCount = (TextView) itemView.findViewById(R.id.textViewConversationCount);
postDescription = (TextView) itemView.findViewById(R.id.textViewPostContentDescription);
mCardView = (CardView) itemView.findViewById(R.id.card_view);
mShowMore = (Button) itemView.findViewById(R.id.buttonSeeMore);
mShowLess = (Button) itemView.findViewById(R.id.buttonSeeLess);
}
mShowLess.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mContext.startActivity(new Intent(mContext, newActivity.class));
}
});
@Override
public void onClick(View view) {
if (clickListener != null) {
clickListener.itemClicked(view, getPosition());
}
}
}
public interface ClickListener {
public void itemClicked(View view, int position);
}
}
我需要将片段中的数组列表从适配器中的mShowLess.setOnClickListener方法传递给新活动。任何人都可以让我知道我该怎么做或有更好的方法来做到这一点。
答案 0 :(得分:2)
我是初学者,但我觉得这有帮助:
如果您想传递ArrayList
到newActivty
,请尝试使用:
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, newActivity.class);
intent.putExtra("arrayList", yourArrayList);
mContext.startActivity(intent);
}
});
在newActivity中:
protected void onCreate(Bundle savedInstanceState) {
...
ArrayList<String> arrayList = getIntent().getExtras().getStringArrayList("arrayList");
}
答案 1 :(得分:0)
在Fragment中将数据设置为适配器
recyclerView.setAdapter(new ToadlineAdapter( context,getData1()));
在构造函数内的适配器中分配数据
private Context context;
private ArrayList<ProductRequestObject> data;
public ToadlineAdapter(Context context, List<TimelineDataStore> data) {
data = data;
this.context = context;
}
然后覆盖适配器中的onclick方法
@Override
public void onClick(View v) {
// Here You will get data of the selected position
Bundle bumdle;
bumdle = new Bundle();
bumdle.putSerializable("selectedPosition ", data.get(getAdapterPosition()));
}