Reputation: 183
I have a fragment with a recycler view and an adaptor that accesses my string arrays, so that they are shown in the recycler view. My question is how can I transfer the data held in these arrays to a new activity, so when the user clicks on the selected item it then brings up more information held in these arrays. I am not sure how it can be done, I have tried putting the values into an array list but can't seem to get it to work, as I am new to this. Thank you in advance
This is my fragment
public class EventCalenderFragment extends Fragment {
RecyclerView recyclerView;
EventCalenderAdapter adapter;
public EventCalenderFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
adapter = new EventCalenderAdapter(getActivity());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.fragment_event_calender, container, false);
recyclerView = (RecyclerView) v.findViewById(R.id.recycler);
recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return v;
}
}
This is my adapter
public class EventCalenderAdapter extends RecyclerView.Adapter<EventCalenderAdapter.ViewHolder> {
String[] title;
String[] time_start;
String[] time_finish;
String[] date;
String[] description;
String[] loc_lat;
String[] loc_long;
static class ViewHolder extends RecyclerView.ViewHolder {
CardView cardView;
TextView titleView;
TextView auxView1;
public ViewHolder(CardView card) {
super(card);
cardView = card;
titleView = (TextView) card.findViewById(R.id.text1);
auxView1 = (TextView) card.findViewById(R.id.text2);
}
}
public EventCalenderAdapter (Context context) {
title = context.getResources().getStringArray(R.array.title);
time_start = context.getResources().getStringArray(R.array.time_start);
time_finish = context.getResources().getStringArray(R.array.time_finish);
date = context.getResources().getStringArray(R.array.date);
description = context.getResources().getStringArray(R.array.description);
loc_lat = context.getResources().getStringArray(R.array.loc_lat);
loc_long = context.getResources().getStringArray(R.array.loc_long);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
CardView v = (CardView) LayoutInflater.from(parent.getContext()).inflate(R.layout.event_task, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, final int i) {
final Context context = viewHolder.titleView.getContext();
viewHolder.titleView.setText(title[i]);
viewHolder.auxView1.setText("Date: "+ date[i] + " Start: " + time_start[i] + " Finish: " + time_finish[i]);
viewHolder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((OnEventView) context).eventView(i);
}
});
}
@Override
public int getItemCount() {
return title.length;
}
}
and this is the fragment I would like to access the data
public class EventViewFragment extends Fragment {
public static final String DEFAULT_FRAGMENT_TAG = "eventViewFragment";
static final String EVENT_ID = "eventId";
View rootView;
TextView title;
TextView description;
TextView date_time;
TextView location;
long eventId;
public static EventViewFragment newInstance(long id) {
EventViewFragment fragment = new EventViewFragment();
Bundle args = new Bundle();
args.putLong(EventViewActivity.VIEW_EVENTID, id);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle arguments = getArguments();
if (arguments != null) {
eventId = arguments.getLong(EventViewActivity.VIEW_EVENTID, 0L);
}
if (savedInstanceState != null) {
eventId = savedInstanceState.getLong(EVENT_ID);
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putLong(EVENT_ID, eventId);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_event_view, container, false);
rootView = v.getRootView();
title = (TextView) v.findViewById(R.id.title);
description = (TextView) v.findViewById(R.id.description);
date_time = (TextView) v.findViewById(R.id.time_date);
location = (TextView) v.findViewById(R.id.location);
date_time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
location.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
return v;
}
}
Upvotes: 0
Views: 232
Reputation: 134
use SharedPreferences class it may be helpfull to transfer a data any where in a application
Upvotes: 0
Reputation: 2757
Once try as follows In the first activity
Intent i=new Intent(getActivity(),NextActivity.class);
i.putStringArrayListExtra("title", (ArrayList) Arrays.asList(title));
i.putStringArrayListExtra("time_start",(ArrayList)Arrays.asList(time_start));
i.putStringArrayListExtra("time_finish",(ArrayList)Arrays.asList(time_finish));
i.putStringArrayListExtra("date",(ArrayList)Arrays.asList(date));
i.putStringArrayListExtra("description",(ArrayList)Arrays.asList(description));
i.putStringArrayListExtra("loc_lat",(ArrayList)Arrays.asList(loc_lat));
i.putStringArrayListExtra("loc_long",(ArrayList)Arrays.asList(loc_long));
And in the next activity
List<String> title1=getIntent().getStringArrayListExtra("title");
........................................
..........................................
Hope this will helps you.
Upvotes: 1