0

I am building an android application where an user select their favorite stuff.

The name of stuff is added in an array when user clicks on the stuff's image.

Now I want to know how can I parse the value of that array to any fragment and show it in my spinner list.

For example: user select Mobile and tablet by clicking on respective images then these values added in to an array name 'stuffarray' now I want to pass this array to my fragment on an 'submitted' button and when I click on an spinner of my fragment it Should have mobile and tablet value in there list.

Here is my code for stuff selection :

submite = (ImageButton) findViewById(R.id.nextscreen);      
next.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

        Intent innext = new Intent(getApplicationContext(), MainActivitytabnew.class);

        startActivity(innext);              

});
img1 = (ImageButton) findViewById(R.id.imageButton1);


img1.setBackgroundResource(R.drawable.mobile);   
img1.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

         isClicked1=!isClicked1;
            if (isClicked1) {
                img1.setImageResource(R.drawable.mobile);
                start();
                stuff1 = "mobile";

                   myList.add(stuff1);



            }else {
                img1.setImageResource(R.drawable.mobile);
                myList.remove(sport1);
                //sport1 = "";  
                txt1.setText("");
            }
    }
});

img2 = (ImageButton) findViewById(R.id.imageButton2);
img2.setBackgroundResource(R.drawable.tablet);
img2.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        isClicked2=!isClicked2;
        if (isClicked2) {
            img2.setImageResource(R.drawable.tablet);
            start();
            stuff2 = "tablet";
           myList.add(stuff2);
        }else {
            img2.setImageResource(R.drawable.tablet);
           // sport2 = "";
            myList.remove(sport2);
        }
    }
});
Prachi
  • 2,559
  • 4
  • 25
  • 37
Hitesh Matnani
  • 533
  • 2
  • 8
  • 26

4 Answers4

1

You have to use Fragment arguments here.

From Activity or where you instantiate your fragments or make a transaction:

 private void putList(List<String> list) {
        Bundle bundle = new Bundle();
        bundle.putStringArrayList("myList", new ArrayList<>(list));

        Fragment fragment = new MyFragment();
        fragment.setArguments(bundle);
    }

Inside each fragment:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    List<String> list = getArguments().getStringArrayList("myList");
}
localhost
  • 5,568
  • 1
  • 33
  • 53
  • My application is unfortunate stopped error coming on implementation of List list = getArguments().getStringArrayList("myList"); – Hitesh Matnani Nov 11 '14 at 09:46
  • @HiteshMatnani, post your error then. Code is fine. – localhost Nov 11 '14 at 09:50
  • When running application it is unfortunate stopped – Hitesh Matnani Nov 11 '14 at 09:56
  • I am coding like this -next.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) {{ Intent innext = new Intent(getApplicationContext(), MainActivitytabnew.class); Bundle bundle = new Bundle();bundle.putStringArrayList("my", myList); FragmentPlayo fragment = new FragmentPlayo(); fragment.setArguments(bundle); startActivity(innext);}}); – Hitesh Matnani Nov 11 '14 at 09:58
  • @HiteshMatnani, because you do it wrong. The question is asking how to get data passed from Activity to a Fragment. But you start another Activity. Attach more sources. – localhost Nov 11 '14 at 10:11
  • why you need my more code it simple on click next button parse the value from Activity to fragment and let have an toast on fragment so it be tested – Hitesh Matnani Nov 11 '14 at 11:00
0

Use

intent.putCharSequenceArrayListExtra(tag, arraylist);

and

getIntent().getExtras().getCharSequenceArrayList(tag);
Rohail Ahmed
  • 111
  • 3
0

You can do this,

In fragment. Access Activity method by fragment.

ActivityName activityName=(ActivityName)getActivity();
if(activityName!=null){
    activityName.MethodOfActivityName();
}

In activity, Access Fragment method by Activity.

fragmentObject.MethodName();
Krunal Indrodiya
  • 782
  • 2
  • 7
  • 19
0

In my case , I just used intent to share String Array from activity to fragment. That worked for me.

In Activity

 Intent send=new Intent(getApplicationContext(),FragmentActivity.class);
                send.putExtra("array",TouristPlaces);

In Fragment under onCreateView..

Intent in=getActivity().getIntent();
    String [] ReceivedTouristPlaces=in.getStringArrayExtra("array");

Note: In my activity I used list view for my requirement, on click it sends data to fragment, which has a tab.