-1

I have two activity, first of which contains few buttons and the second one shows listview. For each button there is a particular array. Now i want to show for each onClick method in First activity, listview in 2nd activity should populate with that particular array. I can have separate Class for each Array. But for large numbers of array, there will be same no. of Classes. Any help will be appreciated. Below is my code : 1st Activity

public void onClick1 (View view){
     startActivity(new Intent ("com.questions"));

 }
 public void onClick2 (View view){
     startActivity(new Intent ("com.questions"));

 }

2nd Activity :

 public static class mSectionFragment extends ListFragment {



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.common_listview, container, false);
        final String[] sm =  getResources().getStringArray(R.array.first_array);

        setListAdapter(new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, sm));
        setListAdapter(getListAdapter());
        return rootView;
      }
    }

now i want to change R.array.first_array to second_array for onClick2

  • Pass some data to the second activity, to know which array you should load. Take a look at this http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android – Rami Apr 09 '15 at 07:40
  • @Rami ya i can pass some data, but can't figure out how to use if statement to choose particular set of array in the 2nd activity. Then took the approach given below by user4768078. I've set array1,array2 with different arrays in the first activity via onClick method and load only "key" in 2nd activity. It worked! Thank you guys. – Saumalya B. Apr 09 '15 at 20:17

1 Answers1

-1

Pass the specific data(array) using intent .

In onClick1:

Intent i =new Intent(this,SecondActivity.class);

I.putextra("key",array1);

startActivity(i);

In OnClick2:

Intent i =new Intent(this,SecondActivity.class);

I.putextra("key",array2);

startActivity(i);

In your SecondActivity u need to extract the data using bundle:-

In your onCreateView:

String str[]=savedInstanceState.getExtra("key");
  • Thanks for the tips. "First set then load" works good. and to load "key" in the 2nd activity i've used the code from the link provided by Rami ` Bundle extras = getIntent().getExtras(); if (extras != null) { sms = extras.getStringArray("key"); }` – Saumalya B. Apr 09 '15 at 20:21