Search code examples
androidlistviewandroid-listviewandroid-fragmentsonclicklistener

setOnItemClickListener for ListView in a fragment


I'm having problems clicking on ListView items in the fragments. I wrote the code so the fragments point to layout files for everything and inside the 2 of the layouts are ListViews, which retrieve their entries from items in a string array in the strings.xml. I could not find a way to implement a setOnItemClickListener for each ListView item in my case due to the way I wrote the code.

MainActivity.java

package com.example.test_app;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.Menu;

public class MainActivity extends FragmentActivity{
    ViewPager ViewPager = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ViewPager= (ViewPager) findViewById(R.id.pager);
        FragmentManager fragmentManager=getSupportFragmentManager();
        ViewPager.setAdapter(new AdapterActivity(fragmentManager));
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }
}

AdapterActivity.java

package com.example.test_app;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class AdapterActivity extends FragmentStatePagerAdapter{
    public AdapterActivity(FragmentManager fm){
        super(fm);
    }
    @Override
    public Fragment getItem(int i){
        Fragment fragment=null;
        if(i==0){
            fragment=new HomeFragment();
        }
        if(i==1){
            fragment=new CommunityFragment();
        }
        if(i==2){
            fragment=new ResourcesFragment();
        }
        return fragment;
    }

    @Override
    public int getCount(){
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position){
        if(position==0){
            return "Home Page";
        }
        if(position==1){
            return "Community";
        }
        if(position==2){
            return "Resources";
        }
        return null;
    }
}
class HomeFragment extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        return inflater.inflate(R.layout.home_fragment, container, false);
    }
}
class CommunityFragment extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        return inflater.inflate(R.layout.community_fragment, container, false);
    }
}
class ResourcesFragment extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        return inflater.inflate(R.layout.resources_fragment, container, false);
    }
}

Then in the resources_fragment.xml layout, there is a ListView

<ListView
        android:id="@+id/resourcesListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/categories" >
</ListView>

It grabs entries from strings.xml

<string-array name="categories">
        <item>Category 1</item>
        <item>Category 2</item>
        <item>Category 3</item>
        <item>Category 4</item>
        <item>Category 5</item>
        <item>Category 6</item>
        <item>Category 7</item>
        <item>Category 8</item>
</string-array>

I used this link to create my ListView, but the setOnItemClickListener won't work for me. How else can I connect each listview item other activities?


Solution

  • The place to add your ListView.SetOnItemClickListener is in your fragment creation code, after you inlfate the layouts. Use findViewById to find your ListView, and add your setOnItemClick listener.

    class ResourcesFragment extends Fragment{
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    
            View view = inflater.inflate(R.layout.resources_fragment, container, false);
            ListView lv = (ListView) view.findViewById(R.id.mylistviewid);
    
            lv.setOnItemClickListener(new OnItemClickListener() {
    
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int position, long arg3) {
                    // TODO Auto-generated method stub
    
                            String category = categories[position];
                            Class activityClass = lookupActivityClass_byName(category);
                            //You could lookup by position, but "name" is more general
    
                            Intent intent = new Intent(getActivity(), activityClass);
                            startActivity(intent);
    
    
                }
            });
    
            return view;
    
        }