Hitesh Matnani
Hitesh Matnani

Reputation: 553

How to send the array data from one activity to fragment in android?

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

Upvotes: 0

Views: 12072

Answers (4)

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.

Upvotes: 0

Krunal Indrodiya
Krunal Indrodiya

Reputation: 792

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

Upvotes: 0

localhost
localhost

Reputation: 5598

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

Upvotes: 1

Rohail Ahmed
Rohail Ahmed

Reputation: 111

Use

intent.putCharSequenceArrayListExtra(tag, arraylist);

and

getIntent().getExtras().getCharSequenceArrayList(tag);

Upvotes: 0

Related Questions