将数据从字符串数组传输到另一个活动

时间:2015-04-25 12:41:15

标签: android

我有一个带有回收器视图的片段和一个访问我的字符串数组的适配器,以便它们显示在recycler视图中。我的问题是如何将这些数组中保存的数据传输到新活动,因此当用户单击所选项时,它会显示这些数组中保存的更多信息。我不确定它是如何完成的,我已经尝试将值放入数组列表但似乎无法使它工作,因为我是新手。提前谢谢

这是我的片段

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;
}


}

这是我的适配器

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;
}


}

这是我想要访问数据的片段

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;
}
}

2 个答案:

答案 0 :(得分:1)

尝试如下 在第一个活动中

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));

在下一个活动中

 List<String> title1=getIntent().getStringArrayListExtra("title");
 ........................................

..........................................

希望这会对你有所帮助。

答案 1 :(得分:0)

使用SharedPreferences类可能有助于在应用程序的任何位置传输数据