11

I have a popup in my page which has a listview. I have created design of popup in a seperate xml and loading it on some button click in my main page. The popup has a listview with each row having a image and a textview. I am not able to get row selection in listview in android 4.1 but it is working in 5.0. Can anyone please suggest me the solution? Listview:

<ListView android:id="@+id/lstSites"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fastScrollEnabled="true"
        android:layout_margin="5dp"
        android:descendantFocusability="blocksDescendants"></ListView>
</LinearLayout>

Listview Item:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
android:focusable="false"
android:focusableInTouchMode="false">
<ImageView
    android:id="@+id/thumbImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="10dp"
    android:focusable="false"
    android:focusableInTouchMode="false"/>
<TextView
    android:id="@+id/tvSite"
    android:textSize="20dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:text="Name"
    android:focusable="false"
    android:focusableInTouchMode="false"/>

adding row click listner:

lstSiteMenu = (ListView) popupView.findViewById(R.id.lstSites);

            adapter = new MenuItemsAdapter(SiteActivity.this, arrMenu);
            // Attach the adapter to a ListView
            lstSiteMenu.setAdapter(adapter);
            lstSiteMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        final int position, long id) {
            }
pankaj
  • 7,878
  • 16
  • 69
  • 115
  • any error in logcat? when you say row selected you meant onItemClick not working or you are not getting position? – virendrao Dec 22 '15 at 09:50
  • 1
    Remove this `android:descendantFocusability="blocksDescendants"` from `ListView`. – Piyush Dec 22 '15 at 10:02

4 Answers4

1

remove this property from listview , android:descendantFocusability="blocksDescendants" and

android:focusable="false"
android:focusableInTouchMode="false"

from text and imageview.

And try This is sample example,

String names[] ={"A","B","C","D"};
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
        LayoutInflater inflater = getLayoutInflater();
        View convertView = (View) inflater.inflate(R.layout.row_file, null);
        alertDialog.setView(convertView);
        alertDialog.setTitle("List");
        ListView lv = (ListView) convertView.findViewById(R.id.listView);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,names);
        lv.setAdapter(adapter);
        alertDialog.show();

otherwise your code is fine. it works.

go through this link for more details.

Another official link for the same.

Community
  • 1
  • 1
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
1

Try all of solutions:


popupwindow.setFocusale(true);
listView xml : "android:focusable="true"

do not use lstSiteMenu.setOnItemClickListener
instead go to your Adapters getView and add

convertView.setOnClickListener

Hope it will help you!

Volodymyr Kulyk
  • 6,455
  • 3
  • 36
  • 63
0

You need to remove android:descendantFocusability="blocksDescendants" from your ListView.

Also remove android:focusable="false" and android:focusableInTouchMode="false" from your row layout in the ImageView and the TextView.

The list item is supposed to be clickable, there is disable it obtaining focus

Ed Holloway-George
  • 5,092
  • 2
  • 37
  • 66
Mina Wissa
  • 10,923
  • 13
  • 90
  • 158
-1

Set row onclick listener in adapter class

public class MenuItemsAdapter extends Base Adapter{
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
             /*
                          Paste your adapter code
             */
              customView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.v("log", "You clicked at pos "+position);
                }
            });
        }
}

This code worked in all sdk versions.

Also remove these properties from xml files

android:descendantFocusability="blocksDescendants"
android:focusable="false"
android:focusableInTouchMode="false"
Ijas Ahamed N
  • 5,632
  • 5
  • 31
  • 53