ListFragment onListItemClick not working

I'm using tab layout with swipeable example, On of the tab is list view, and when clicked on list item new fragment opens up. My issue is when i click on list item, nothing is happening. i'm using following example and adding my code androidhive.info/category/tab-view

AddingList

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class AddingList extends ListFragment {
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    String[] values = new String[] { "List", "iPhone", "WindowsMobile",
        "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
        "Linux", "OS/2" };
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
        android.R.layout.simple_list_item_1, values);
    setListAdapter(adapter);

  }

  @Override
  public void onListItemClick(ListView l, View v, int position, long id) {
      FragmentTransaction ft = getFragmentManager().beginTransaction();
         ft.replace(R.id.pager, new List()).commit();
  }
} 

Answers


Move your code in onCreateView() method from onActivityCreated() method

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    String[] values = new String[] { "List", "iPhone", "WindowsMobile",
    "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
    "Linux", "OS/2" };

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
    android.R.layout.simple_list_item_1, values);

    setListAdapter(adapter);

    return super.onCreateView(inflater, container, savedInstanceState);
 }

UPDATE:

  @Override
  public void onListItemClick(ListView l, View v, int position, long id) {


      FragmentManager manager = getFragmentManager();
      FragmentTransaction ft = manager.beginTransaction();
      ft.replace(R.id.pager, new Home());
      ft.commit();

  }

Implement this method in your Fragment.

 @Override
 public void setUserVisibleHint(boolean isVisibleToUser) {
  super.setUserVisibleHint(isVisibleToUser);

  // Make sure that we are currently visible
  if (this.isVisible()) {
    // Do your stuff here
    if (!isVisibleToUser) {
        Log.d("MyFragment", "Not visible");

    }
  }
  }

Try this

 ListView list = (ListView) findViewById(R.id.pager1);

 list.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View view, int arg2,
                        long arg3) {
                    // TODO Auto-generated method stub
                                     //Your action


                }
            });

Try this . need to set adapter

        ListView mListContainer = (ListView) findViewById(R.id.pager1);


        //YOURADAPTER NAME
        mInformedListAdapter = new InformedListAdapter(mContext, mStatistics,this);

        //mStatistics is the my  private Map<String, Map<String, String>> mStatistics = new HashMap<String, Map<String, String>>();

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

         //DO YOURS



            }
        });

You guys have to call super.onItemClickListener method in overrided method


Need Your Help

S3 to RDS file management system

amazon-web-services amazon-s3 amazon-rds

I'm new to AWS and have a feasibility question for a file management system I'm trying to build. I would like to set up a system where people will use the Amazon S3 browser and drop either a csv or...

Handling uncaught exceptions in C# console application

c# .net exception mono appdomain

I'm currently writing a server that hosts several modules. The server runs each module in a separate AppDomain. What I want to achieve is exception isolation, i.e. when one module throws an excepti...