4

Update : I found the solution by removing

v.setFocusable(true); v.setClickable(true); in the code

and only add

v.setEnabled(true);

and in my xml (ListView) add

android:drawSelectorOnTop="true" android:focusable="true"


When I click it nothing happens , even the list view doesn't focus.

I have tried to add all this:

           v.setClickable(true);
           v.setEnabled(true); 
           v.setFocusable(true);

Only this will work is i add the following code:

But this doesn't determine what item is being clicked

How to handle ListView click in Android

and the result still the same.

Here's is the code :

public class AppsInspectorActivity extends Activity {
     /** Called when the activity is first created. */
     @Override
        public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
//[...]
    ListView app_listView = (ListView)findViewById(R.id.listview);

// I try setOnItemClickListene here - 1
    app_listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            Toast.makeText(getApplicationContext(), "I Clicked on Row " + position + ".", Toast.LENGTH_SHORT).show();

        }
    });
}

Adapter

 public class AppAdapter extends BaseAdapter {

            Context context;
            ArrayList<AppInfo> dataList=new ArrayList<AppInfo>();
            public AppAdapter(Context context,ArrayList<AppInfo> inputDataList)
            {

                this.context = context;
                dataList.clear();
                for(int i=0;i<inputDataList.size();i++)
                {
                    dataList.add(inputDataList.get(i));
                }
            }
            public int getCount() {
                // TODO Auto-generated method stub
                return dataList.size();
            }

            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return dataList.get(position);
            }

            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return position;
            }

            public View getView(int position, View convertView, ViewGroup parent) {

                View v=convertView;
                final AppInfo appUnit = dataList.get(position);

                ListView app_listView = (ListView)findViewById(R.id.listview);

                /** Remove this , i just try to add to see setOnItemClickListener will work at here or not **/
                 app_listView.setOnItemClickListener(new OnItemClickListener() {
                    public void onItemClick(AdapterView<?> parent, View view,
                      int position, long id) {
                    Toast.makeText(getApplicationContext(), "I Clicked on Row " + position + ".", Toast.LENGTH_SHORT).show();
                  }
                });

                if(v==null)
                {
                    LayoutInflater vi=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v=vi.inflate(R.layout.app_row, null);
                    v.setClickable(true);
                    v.setEnabled(true); 
                    v.setFocusable(true);           
                }


                TextView appName=(TextView)v.findViewById(R.id.appName);
                ImageView appIcon=(ImageView)v.findViewById(R.id.AppIcon);

                if(appName!=null)
                    appName.setText(appUnit.appName);
                if(appIcon!=null)
                    appIcon.setImageDrawable(appUnit.appIcon);
                return v;
            }
         }
 }
}
Community
  • 1
  • 1
xAnGz
  • 127
  • 4
  • 11

6 Answers6

1

Can you please post the main.xml layout. Seems to me that you have another view on top of the listview that is consuming the touch events.

1

I found the solution by removing

v.setFocusable(true); v.setClickable(true); in the code

and only add

v.setEnabled(true);

and in my xml (ListView) add

android:drawSelectorOnTop="true" android:focusable="true"

xAnGz
  • 127
  • 4
  • 11
0

Hi remove the setOnItemClickListener in the getView of the Adapter.Refer this listView

u.jegan
  • 833
  • 6
  • 15
0

use AppsInspectorActivity.this instead of getApplicationContext()

  public class AppsInspectorActivity extends Activity {
     /** Called when the activity is first created. */
     @Override
        public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
//[...]
    ListView app_listView = (ListView)findViewById(R.id.listview);

// I try setOnItemClickListene here - 1
    app_listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            Toast.makeText(AppsInspectorActivity.this, "I Clicked on Row " + position + ".", Toast.LENGTH_SHORT).show();

        }
    });
}

and remove the setOnItemClickListener from the getView() method in your adapter

confucius
  • 13,127
  • 10
  • 47
  • 66
0

Change your getView() to below one and tell if it works

public View getView(int position, View convertView, ViewGroup parent) {

            View v=convertView;
            final AppInfo appUnit = dataList.get(position);
            if(v==null)
            {
                LayoutInflater vi=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v=vi.inflate(R.layout.app_row, null);
                v.setClickable(true);
                v.setEnabled(true); 
                v.setFocusable(true);           
            }


            TextView appName=(TextView)v.findViewById(R.id.appName);
            ImageView appIcon=(ImageView)v.findViewById(R.id.AppIcon);

            if(appName!=null)
                appName.setText(appUnit.appName);
            if(appIcon!=null)
                appIcon.setImageDrawable(appUnit.appIcon);
            return v;
        }
ingsaurabh
  • 15,249
  • 7
  • 52
  • 81
0

You have to set adapter to listview before setting its onItemClick.Here you just get the listview but with no items.so when you click on listview,its onItemClick is never called.

Hiral Vadodaria
  • 19,158
  • 5
  • 39
  • 56