У меня есть слайдер AdapterViewFlipper во фрагменте, который я использовал для модификации данных в него. Проблема в том, что я не могу отправить JSON данные с помощью onclickListener для действия с деталями.
Вот мой фрагмент Home
public class Home extends Fragment implements View.OnClickListener {
private Button nx;
private Button prev;
private Bundle extras;
private OnFragmentInteractionListener mListener;
private AdapterViewFlipper adapterViewFlipper;
RetrofitService mService;
public Home() {
// Required empty public constructor
}
// TODO: Rename and change types and number of parameters
public static Home newInstance(String param1, String param2) {
Home fragment = new Home();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_home,container,false);
nx=(Button)view.findViewById(R.id.next);
prev=(Button)view.findViewById(R.id.prev);
nx.setOnClickListener(this);
prev.setOnClickListener(this);
adapterViewFlipper = (AdapterViewFlipper)view.findViewById(R.id.adapterViewFlipper);
adapterViewFlipper.setAutoStart(true);
adapterViewFlipper.setFlipInterval(3000);
mService = Common.getAnouncment();
getAllAnouncementList();
return view;
}
private String title;
private String body;
private String date;
private String imgUrl;
private void getAllAnouncementList() {
mService.getAllAnouncment().enqueue(new Callback<List<AnouncmentItem>>() {
@Override
public void onResponse(Call<List<AnouncmentItem>> call, Response<List<AnouncmentItem>> response) {
FlipperAdapter adapter = new FlipperAdapter(getActivity().getBaseContext(), response.body(), new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle bundle =getArguments();
if (bundle != null) {
title=getArguments().getString("date");
body = getArguments().getString("body");
date= getArguments().getString("date");
imgUrl = getArguments().getString("image");
}
Intent intent = new Intent(getContext(), Slider_details.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("title2", title);
intent.putExtra("date2", date);
intent.putExtra("body2", body);
intent.putExtra("image2", imgUrl);
startActivity(intent);
}
});
//adding it to adapterview flipper
adapterViewFlipper.setAdapter(adapter);
}
@Override
public void onFailure(Call<List<AnouncmentItem>> call, Throwable t) {
}
});
}
public void previousView(View v) {
adapterViewFlipper.showPrevious();
}
public void nextView(View v) {
adapterViewFlipper.showNext();
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.prev:
previousView(v);
break;
case R.id.next:
nextView(v);
break;
}
}
Мой адаптер Flipper
public class FlipperAdapter extends BaseAdapter{
private Context mCtx;
private List<AnouncmentItem> anouncements;
private View.OnClickListener listener;
public FlipperAdapter(Context mCtx, List<AnouncmentItem> anouncements, View.OnClickListener listener){
this.mCtx = mCtx;
this.anouncements =anouncements;
this.listener = listener;
}
@Override
public int getCount() {
return anouncements.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
AnouncmentItem item = anouncements.get(position);
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.flipper_anouncement, null);
TextView textView = (TextView) view.findViewById(R.id.title_slider);
textView.setText(item.getTitle());
view.setOnClickListener(listener);
Home myFrag = new Home();
Bundle bundle = new Bundle();
bundle.putString("title",item.getTitle());
bundle.putString("date",item.getField_date_published());
bundle.putString("body",item.getBody());
bundle.putString("image",item.getField_intro_image());
bundle.putString("files",item.getField_file_attachement());
bundle.putString("images",item.getField_image());
myFrag.setArguments(bundle);
return view;
}}
здесь я пытаюсь получить getString из адаптера флиппера для фрагмента дома, а затем отправить его в Slider Details
И моя активность sliderDetail
public class Slider_details extends AppCompatActivity {
private TextView title;
private TextView body;
private ImageView img;
private TextView date;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_slider_details);
Bundle extras=getIntent().getExtras();
title = (TextView) findViewById(R.id.txt_title2);
body = (TextView) findViewById(R.id.txt_body2);
img = (ImageView) findViewById(R.id.img_content2);
date = (TextView) findViewById(R.id.anouncemnet_date2);
if (extras != null) {
title.setText(extras.getString("title2"));
body.setText(extras.getString("body2"));
date.setText(extras.getString("date2"));
String imageUrl = extras.getString("image2");
Picasso.with(this).load(imageUrl).resize(500, 220).into(img);
}
}}
Is Есть ли способ сделать это так?