0

I have two fragments in an activity and my first fragment is extending ListFragment class and there is one ListView widget whose id is 'list' in its xml layout. I wrote a custom adapter for this List and works fine but I need to click an item to pass some values to another fragment. For itemClick event I guess, I must use onListItemClick() method of ListFragment class.But when I run the app and click an item in list nothing happened.

public class List extends ListFragment {

    private Context context;
    ListAdapter listAdapter;
    OnHeadlineSelectedListener mCallback;

    public interface OnHeadlineSelectedListener {
        public void onArticleSelected(int position);
    }


    @Override
    public void onCreate (Bundle savedInstanceState){

        super.onCreate(savedInstanceState);

        KnlContainer knlModel = new KnlContainer();
        listAdapter = new ListAdapter((Activity)context,knlModel);

    }

    @Override
    public void onAttach(Activity activity){

        super.onAttach(activity);
        context = activity;

      //I need to call this instance of interface to comminacation with Activity
        try {
            mCallback = (OnHeadlineSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnHeadlineSelectedListener");
        }

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

      // Send the event to the host activity

       //for comminacation with activity

        mCallback.onArticleSelected(position);
            Log.e("STAT","OK");
        }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

        LinearLayout rootView = (LinearLayout)inflater.inflate(R.layout.knl_list_fragment, null);

        ListView knllrList = (ListView)rootView.findViewById(android.R.id.list);
        knllrList.setAdapter(listAdapter);


        return rootView;

    }
}

I must define clicklistener in adapter ?

Marcus
  • 6,697
  • 11
  • 46
  • 89
Cem
  • 361
  • 6
  • 17

2 Answers2

0

The adapter is purely for handling the data and generating the view for each row in the listview.

According to Android ListFragment API you must use ListFragment.setListAdapter().

so....

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

    KnlContainer knlModel = new KnlContainer();

    setListAdapter(new ListAdapter((Activity)context, knlModel));
 }
zec
  • 294
  • 1
  • 9
  • I fetched the data from adapter as you said but I want to add clicklistener to each item in listview becuase I need to show the results in another fragment by clicking these different items. When button click, it must call public void onListItemClick but It doesnt work – Cem Aug 13 '15 at 17:08
  • If I am understanding you, you have a button for each item (row) in your ListView? – zec Aug 13 '15 at 17:20
  • Yes, There is button in my custom row view but I dont know how use clicklistener. I must add clicklistener in my custom adapter? or I need to use onListItemClick() method of ListFragment ? – Cem Aug 13 '15 at 17:30
  • The button is unnecessary as the entire row view is clickable. – zec Aug 13 '15 at 17:50
0

If you're list contains some other clickable element like a checkbox, you'll have to set it's focusable attribute to false before onListItemClick will work.

See this answer: ListFragment OnListItemClick not being called


If that doesn't work, another potential solution is to ensure the definition fo yourListView's id is android:id="android:id/list" and not android:id="@+id/list.

Community
  • 1
  • 1
Alex Crist
  • 1,059
  • 2
  • 12
  • 22