title: setOnItemClickListener for ListView in a fragment
body: <p>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.</p>
  
  <p>MainActivity.java</p>
  
  <pre><code>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;
      }
  }
  </code></pre>
  
  <p>AdapterActivity.java</p>
  
  <pre><code>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);
      }
  }
  </code></pre>
  
  <p>Then in the resources_fragment.xml layout, there is a ListView</p>
  
  <pre><code>&lt;ListView
          android:id="@+id/resourcesListView"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:entries="@array/categories" &gt;
  &lt;/ListView&gt;
  </code></pre>
  
  <p>It grabs entries from strings.xml</p>
  
  <pre><code>&lt;string-array name="categories"&gt;
          &lt;item&gt;Category 1&lt;/item&gt;
          &lt;item&gt;Category 2&lt;/item&gt;
          &lt;item&gt;Category 3&lt;/item&gt;
          &lt;item&gt;Category 4&lt;/item&gt;
          &lt;item&gt;Category 5&lt;/item&gt;
          &lt;item&gt;Category 6&lt;/item&gt;
          &lt;item&gt;Category 7&lt;/item&gt;
          &lt;item&gt;Category 8&lt;/item&gt;
  &lt;/string-array&gt;
  </code></pre>
  
  <p>I used <a href="http://theopentutorials.com/tutorials/android/listview/android-creating-and-populating-listview-items-in-xml/" rel="nofollow">this link to create my ListView</a>, but the setOnItemClickListener won't work for me. How else can I connect each listview item other activities?</p>