4

This is the array in main activity

String[] items = {
            "Unit 1", "Unit 2", "Unit 3", "Unit 4", "Unit 5", "Unit 6", "Unit 7",
            "Unit 8", "Unit 9", "Unit10", "Unit 11", "Unit 12", "Unit 13"};
    ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, items);

this is the onclickmethod from which I want to load that list into fragment.

   bt2.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.container, fragmentB);
            fragmentTransaction.addToBackStack("");
            fragmentTransaction.commit(); 

I have many arrays of strings and want to assign one to each button in menu. Now where should i put this code?

ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
Atiq
  • 14,435
  • 6
  • 54
  • 69

2 Answers2

1

The listview must be inside the fragment, if you want to pass data to the fragment by clicking on different buttons you should do:

EDITED

IMPORTANT!

This class is only for example (it does't follow the best practices in android development).

public class MainActivity extends FragmentActivity implements View.OnClickListener{

public static final String LISTVIEW_DATA = "myData";
public static final String DATA_TYPE1 = "DataType1";
public static final String DATA_TYPE2 = "DataType2";
public static final String DATA_TYPE3 = "DataType3";

Bundle bundle = null;

ArrayList<String> items1 = new ArrayList<String>();
ArrayList<String> items2 = new ArrayList<String>();
ArrayList<String> items3 = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //set some data to the items
    items1 = generateStringData("Unit 1");
    items2 = generateStringData("Unit 2");
    items3 = generateStringData("Unit 3");

    Button btn1 = (Button) findViewById(R.id.button1);
    Button btn2 = (Button) findViewById(R.id.button2);
    Button btn3 = (Button) findViewById(R.id.button3);

    btn1.setOnClickListener(this);
    btn2.setOnClickListener(this);
    btn3.setOnClickListener(this);
}

private ArrayList<String> generateStringData(String prefix){
    ArrayList<String> temp = new ArrayList<String>();

    for (int i = 0; i<10; i++){
        temp.add(prefix + i);
    }

    return temp;
}

@Override
public void onClick(View view) {
    switch (view.getId()){
        case R.id.button1:
            bundle = new Bundle();
            bundle.putString(MainActivityFragment.DATA_TYPE, DATA_TYPE1);
            bundle.putStringArrayList(LISTVIEW_DATA, items1);
            break;
        case R.id.button2:
            bundle = new Bundle();
            bundle.putString(MainActivityFragment.DATA_TYPE, DATA_TYPE2);
            bundle.putStringArrayList(LISTVIEW_DATA, items2);
            break;
        case R.id.button3:
            bundle = new Bundle();
            bundle.putString(MainActivityFragment.DATA_TYPE, DATA_TYPE3);
            bundle.putStringArrayList(LISTVIEW_DATA, items3);
            break;
        default:
            break;
    }

    setListFragment(bundle);
}

private void setListFragment(Bundle arguments){
    Fragment fragment = new MainActivityFragment();

    if (arguments != null){
        fragment.setArguments(arguments);
    }

    FragmentManager fragmentManager = getSupportFragmentManager();
    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.container, fragment);
    fragmentTransaction.addToBackStack("");
    fragmentTransaction.commit();
}

When you click over button this code will put fragment over fragment over fragmen as you can see by tapping the back button, this is not good idea. You should refartor the code by your needs.

After setting the arguments to the fragment you must receive the data inside the fragment:

public class MainActivityFragment extends Fragment {

public final static String DATA_TYPE = "DataType";

private ArrayList<String> data = new ArrayList<>();
private String dataTag = "";

public MainActivityFragment() {
}

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

    if (getArguments() != null){
        data = getArguments().getStringArrayList(MainActivity.LISTVIEW_DATA);
        dataTag = getArguments().getString(DATA_TYPE);
    }

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);

    ListView listView = (ListView) rootView.findViewById(R.id.listView1);

    ArrayAdapter adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, data);

    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int   i, long l) {

            switch (dataTag) {
                case MainActivity.DATA_TYPE1:
                    //here is where you can implement your logic for type 1
                    break;
                case MainActivity.DATA_TYPE2:
                    //here is where you can implement your logic for type 2
                    break;
                case MainActivity.DATA_TYPE3:
                    //here is where you can implement your logic for type 3
                    break;
                default:
                    //do something else
                    break;
            }

        }
    });

    return rootView;
}

Hope it helps.

Vladimir Dimov
  • 1,213
  • 2
  • 10
  • 7
  • by applying ur method nothing comes on screen not any exception and if i double tap the button it force closes the app and in logcat it says Fragment already active? – Atiq Jan 07 '16 at 09:55
  • I edited my answer, so you can understand the code. Don't forget to import android.support.v4.app.Fragment. – Vladimir Dimov Jan 07 '16 at 12:06
  • Thanks Man :) it worked :) i wasn't implementing method inside fragment in the right way, thank u so very much\ – Atiq Jan 07 '16 at 12:37
  • I'm glad that I can help :) – Vladimir Dimov Jan 07 '16 at 12:47
  • I have got one more Question if u can answer it please, how can i assign onitemclick to all the items of these arraylists separately? right now if i assign some action to exercise 1 of unit 1, it gets assigned to exercise of all units i.e unit 1- exercise 1, unit 2 exercise 1 so on – Atiq Jan 10 '16 at 13:29
  • I edited my answer again. Look at the follow the DataType logic. – Vladimir Dimov Jan 11 '16 at 07:03
  • Thanks Man its perfect :) I really appreciate it, i was trying to figure it out from two days and can't believe it was that simple :) – Atiq Jan 11 '16 at 07:46
  • Please see [this](http://stackoverflow.com/questions/34754043/how-do-i-create-an-arraylist-of-imges-from-drawable-and-display-it-in-fragment-l/34754504?noredirect=1#comment57256357_34754504)? – Atiq Jan 13 '16 at 06:13
  • I have lil problem, When i make nested switch case for lets say MainActivity.DATA_TYPE1, it gives error asking for `android.permission.INTERACT_ACROSS_USERS_FULL` even if i gave the permission? though it doesn't need such permissions. I am unable to handle click on specific excercise of each DATA_TYPE. – Atiq Jan 14 '16 at 10:43
0

firstly the listview must be a part of fragment layout then you can write this in your onCreateView() method of fragment.

onCreateView(LayoutInflater inflater, ViewGroup group, Bundle savedInstance){
View v = inflater.inflate(R.layout.fragment_layout, container, false);
String[] items = {"Unit 1", "Unit 2", "Unit 3", "Unit 4", "Unit 5", "Unit 6", "Unit 7", "Unit 8", "Unit 9", "Unit10", "Unit 11", "Unit 12", "Unit 13"};
ArrayAdapter adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, items);

ListView listView = (ListView) v.findViewById(R.id.listview);
listView.setAdapter(adapter);
return v;
}
Akash Raghav
  • 1,073
  • 9
  • 19
  • listview is part of fragment layout bt the thing is i want to load diff arraylist on each button press, so i don't want to write it in oncreateview, is there anyway i could assign this to button so fragment loads diff Arraylist for each button press? – Atiq Jan 07 '16 at 07:37
  • you can pass the `array` into fragment after the fragment object creation which can be obtained directly in `onCreateView()` and added to `ArrayAdapter` object. Read this[http://stackoverflow.com/questions/17436298/how-to-pass-a-variable-from-activity-to-fragment-and-pass-it-back] to understand how to pass arguments to fragment. – Akash Raghav Jan 07 '16 at 08:26
  • can u please give me an example? I have read everything on the google and here and still unable to solve it – Atiq Jan 07 '16 at 12:10