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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

9 Comments

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?
I edited my answer, so you can understand the code. Don't forget to import android.support.v4.app.Fragment.
Thanks Man :) it worked :) i wasn't implementing method inside fragment in the right way, thank u so very much\
I'm glad that I can help :)
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
|
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;
}

3 Comments

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?
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[stackoverflow.com/questions/17436298/… to understand how to pass arguments to fragment.
can u please give me an example? I have read everything on the google and here and still unable to solve it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.