manuelBetancurt
manuelBetancurt

Reputation: 16168

OnItemSelectedListener in a Fragment that uses a custom Adapter not responding

I have a list in a fragment with custom cells adapter,

The problem is that the onItemSelected is not responding,

how to fix this please?

public class PhoneMenuList extends SherlockFragment implements OnItemSelectedListener {

Fragment newContent = null;
ListView productList;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ 
    View mView = inflater.inflate(R.layout.list, container, false);
    return mView; 
}

@Override
public void onActivityCreated(Bundle savedInstanceState){
    super.onActivityCreated(savedInstanceState);

    //SET THE LIST ADAPTER!
    Hamburger hambu_data[] = new Hamburger[] {
                new Hamburger(R.drawable.icon_hambu_folder, "My Documents"),
                new Hamburger(R.drawable.icon_hambu_favs, "Top 10 viewed"),
                new Hamburger(R.drawable.icon_hambu_validate, "Validate Document"),
                new Hamburger(R.drawable.icon_hambu_how, "How to use"),
                new Hamburger(R.drawable.icon_hambu_about, "About")
            };              

      productList= (ListView) getActivity().findViewById(R.id.listView1);

      HamburgerAdapter adapter = new HamburgerAdapter(getActivity(), R.layout.hamburger_item_row, hambu_data);          

        productList= (ListView) getActivity().findViewById(R.id.listView1);  
        View header = (View)getLayoutInflater(savedInstanceState).inflate(R.layout.hamburger_item_row, null);
        productList.addHeaderView(header);
        productList.setAdapter(adapter);

        //listener          
        productList.setOnItemSelectedListener(this);
}

 //@Override
 public void onListItemClick(ListView l, View v, int position, long id) {
    Log.d("mensa", "chapuzea");
 }

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    // TODO Auto-generated method stub
    Log.d("mensa", "abacus");
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub
    Log.d("mensa", "semper");
}

}

Upvotes: 1

Views: 990

Answers (3)

SilentKiller
SilentKiller

Reputation: 6942

You can override onListItemClick method of SherlockListFragment.

Try this Code.

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
   super.onListItemClick(l, v, position, id);
   System.out.println("onListItemClick");
}

or instead of using OnItemSelectedListener use OnItemClickedListener.

Upvotes: 0

Ketan Ahir
Ketan Ahir

Reputation: 6738

why are you initializing listview twice ?

productList= (ListView) getActivity().findViewById(R.id.listView1);

and use setOnItemClick() instead of setOnItemSelected()

remove productList.setOnItemSelectedListener(this);

use productList.setOnItemClickListener(this);

Upvotes: 1

pumpkee
pumpkee

Reputation: 3357

You have to set a choice mode to your ListView. setChoiceMode

Upvotes: 0

Related Questions