Sharp
Sharp

Reputation: 121

add your own listener to a list

i tried to add a listener to that list but nothing i don't understand why if you want to see the rest of the code please check on add your own listener to a list

public void onCreatebis(final ResolveInfo resolveInfo)  {

            setContentView(R.layout.main);
            final Intent mainIntent=new Intent(Intent.ACTION_MAIN,null);
            mainIntent.addCategory(Intent.CATEGORY_LAUNCHER) ;
            final PackageManager pm = getApplicationContext().getPackageManager();
            final ArrayList<ResolveInfo> listP= (ArrayList<ResolveInfo>)                    pm.queryIntentActivities( mainIntent, 0);
            final int trimLength = "com.android.".length();
            ArrayList<String> maliste = new ArrayList<String>();
                        // Loop over each item.
            for (ResolveInfo info : listP) {
                // Get the (full, qualified) package name.
                String packag = info.activityInfo.applicationInfo.packageName;

                // Now, trim it with substring and the trim length.
                String trimmed = packag.substring(trimLength);
                maliste.add(trimmed);
            }
            ListView list = (ListView)findViewById(R.id.list);
            monadaptateur adapter2 = new monadaptateur(this, maliste);

            list.setOnItemClickListener(new OnItemClickListener(){

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {
                    Log.v("lalalala","lalala");

                }
                   });
            list.setAdapter(adapter2);
    }

Upvotes: 0

Views: 121

Answers (1)

Ahmed Aeon Axan
Ahmed Aeon Axan

Reputation: 2139

if the ListView has focusable items then onClickListener will fire instead of the onItemClickListener. Set items can focus to false

list.setItemsCanFocus(false);

have a look at this thread. Also note there are workarounds for this. But the better choice would be to set items non focusable and use OnItemClickListener, or make them focusable and use an onClickListener on the views

Also, the onClickListener should not be set for the listview. Instead for each listview item in the getView() method

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // ...

    view.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // your code
        }

    });

    return view;
}

Upvotes: 2

Related Questions