Giuseppe Terribilio
Giuseppe Terribilio

Reputation: 49

Android - setOnItemClickListener event of ListView not working in Fragment

Good morning,

I have a problem with the setOnItemClickListener event of a ListView inside a Fragment which is never fired.

Here is the code of the item in listView:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="80dip" >

<ImageView
  android:id="@+id/url_foto"
  android:layout_width="100dip"
  android:layout_height="100dip" 
  android:src="@drawable/stub" 
  android:scaleType="centerCrop"/>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_toRightOf="@id/url_foto"
    android:orientation="vertical"
    android:paddingLeft="10sp" >

    <TextView
        android:id="@+id/nome"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textIsSelectable="true" />

    <TextView
        android:id="@+id/cognome"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textIsSelectable="true" />

Here is the xml code of the list (that also includes a edittext with some listeners):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<EditText
    android:id="@+id/cerca_il_prof_edit_search"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="search" />

<ListView
    android:id="@+id/cerca_il_prof_list_result"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:cacheColorHint="#00000000"
    android:drawSelectorOnTop="true"
    android:focusable="true" />

and finally the fragment that contains the event which is not fired:

public class CercaIlProfFragment extends Fragment {
private DbAdapter dbHelper;
private Cursor cursor;
private Context ctx;
List<Professore> listProf;

private static final String TAG = "CercaIlProf - ";

private EditText string_search;
private ListView listViewProf;

int textlength = 0;

/**
 * The fragment argument representing the section number for this fragment.
 */
public static final String ARG_SECTION_NUMBER = "section_number";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.cerca_il_prof, container, false);

    ctx = getActivity();
    listProf = new ArrayList<Professore>();

    string_search = (EditText) rootView.findViewById(R.id.cerca_il_prof_edit_search);

    Date currDate = new Date();
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(currDate);
    calendar.add(Calendar.MONTH, 1);

    // get all prof
    getAllProf();

    listViewProf = (ListView) rootView.findViewById(R.id.cerca_il_prof_list_result);
    ProfessoreListAdapterWithCache professoreListAdapterWithCache = new ProfessoreListAdapterWithCache(ctx,
            R.layout.cerca_il_prof_list_result_item, listProf, this.getActivity());
    listViewProf.setAdapter(professoreListAdapterWithCache);

    listViewProf.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getActivity(), "Click ListItem Number " + position, Toast.LENGTH_LONG).show();
        }
    });

    listViewProf.setEnabled(true);

    string_search.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
            Toast.makeText(getActivity(), "Click ListItem Number ", Toast.LENGTH_LONG).show();
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            //do stuff
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //do stuff
    });

    return rootView;

}

Can someone help me please??

Thanks in advance.

Regards.

Giuseppe

Upvotes: 0

Views: 6337

Answers (5)

Amitabh Sarkar
Amitabh Sarkar

Reputation: 1311

I found the solution. If you change the list item's (layout which you are setting in the listview adapter)layout child views to

 android:clickable="false"

and

 android:focusable="false"

it works.

Upvotes: 0

qix
qix

Reputation: 7902

To expand upon @Ashwin's answer, note that the ListView blocks clicks of an item that contains at least one focusable descendant but it doesn’t make the content focus-reachable calling setItemsCanFocus(true). One workaround is by disabling focusability of descendants, using

android:descendantFocusability="blocksDescendants"

in the layout defining your list item, although you can just disable children that can take focus as he mentioned earlier. (First learned of this myself from http://cyrilmottier.com/2011/11/23/listview-tips-tricks-4-add-several-clickable-areas/, but a few other SO posts also point this out.)

Upvotes: 2

somnath mule
somnath mule

Reputation: 1

you will try this it will help you lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(android.widget.AdapterView<?> arg0,
                    View arg1, int arg2, long arg3) {
                // TODO Auto-generated method stub


            }
        });

Upvotes: 0

Ashwin S Ashok
Ashwin S Ashok

Reputation: 3663

Just put android:focusable="false" android:clickable="false" in layout. For all textviews,buttons etc. Works Fine.

Upvotes: 2

Basim Sherif
Basim Sherif

Reputation: 5440

Declare your listview and itemclick listener inside onViewCreated Like,

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    final ListView listViewProf=(ListView)getActivity().findViewById(R.id.cerca_il_prof_list_result);
    listViewProf.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        }
    });
}

Upvotes: 1

Related Questions