Anders Anders - 7 months ago 15
Java Question

How to Implement onItemClick() for multiple ListViews separately?

I am developing an app for a book, which have 13 units, each unit contain many exercises and each exercise contains many scanned images.

Now I have a Main Menu Activity, and 2 fragments, fragmentA for showing the exercises in ListView and fragmentB for showing the scanned images of exercises.

My Main Menu Activity contain 13 buttons, each button for every unit, to load that unit's exercises.

I have created ArrayLists of exercises for each unit separately, to load them into fragmentA when user tap on a specific unit.

I have created a Switch Case in the Main Menu Activity for button clicks of each unit, which loads the ArrayList of that specific unit in fragmentA in a listview.

Onbackpress it takes user back from fragmentA to Main Menu.

Now I am trying to implement OnItemClick() for clicks on ListViews which are loaded into fragmentA for each exercise of each unit.

If I set toast inside fragmentA OncreatView it assign them all of the ListViews of all the units.
But I want to do separately for each exercise of each unit i.e for all the entries in my all ArrayLists.

As am using bundle to pass the data from ArayLists into fragmentA, like this

@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button1:
bundle = new Bundle();
bundle.putStringArrayList(LISTVIEW_DATA, unit1);
break;
case R.id.button2:
bundle = new Bundle();
bundle.putStringArrayList(LISTVIEW_DATA, unit2);
break;
case R.id.button3:
bundle = new Bundle();
bundle.putStringArrayList(LISTVIEW_DATA, unit3);
break;
default:
break;
}

setListFragment(bundle);
}
private void setListFragment(Bundle arguments) {
Fragment fragment = new ExFragment();

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

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.container, fragment);
ft.addToBackStack("");
ft.commit();
}


This is my onItemClick in fragmentA.

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)


I know its pretty basic question but am on it for hours.

Thanks

I have one fragmentA for 13 ArrayLists, showing only one at a time, and am looking for a way to assign onitemclick to all the items in 13 listviews.

Answer

For anyone who is going through the same situation as I was, I assigned a tag named DATA_TYPE to each case and made switch case for each tag in on create view of fragment and solved the issue

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

while MainActivityFragment is the name of fragment class. Hope it helps to someone