I am unable to extend my activity to listactivity. help needed

后端 未结 3 1734
感动是毒
感动是毒 2021-01-22 12:10

I am unable to extend my activity to listactivity. I want to extend it to listactivity and add onclicklistener to the list items.

public class MainActivity exten         


        
相关标签:
3条回答
  • 2021-01-22 12:44

    implement OnItemClickListener

    public class MainActivity extends ListActivity implements OnItemClickListener
    {
       //your code;
    
        @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
        // TODO Auto-generated method stub
        results.get(pos);  //this will give you the value in the clicked list item as per your code
    }
    }
    
    0 讨论(0)
  • 2021-01-22 13:04

    if you're gonna use a ListActivity then you don't need this line:

    ListView lView = (ListView) findViewById(R.id.lvApps);
    

    BUT that specific ListView being referred to right now (provided it's in the corresponding xml layout) must have it's id changed to

    <ListView
      android:id="@android:id/list"
    .....
    
    0 讨论(0)
  • 2021-01-22 13:09

    Use following piece of code:

        public class MainActivity extends ListActivity implements OnItemClickListener{
    
        private ArrayList results = new ArrayList();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    //            setContentView(R.layout.activity_main);
    
            PackageManager pm = this.getPackageManager();
    
            Intent intent = new Intent(Intent.ACTION_MAIN, null);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
    
            List < ResolveInfo > list = pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED);
            for (ResolveInfo rInfo: list) {
                results.add(rInfo.activityInfo.applicationInfo
                    .loadLabel(pm).toString());
                Log.w("Installed Applications", rInfo.activityInfo.applicationInfo
                    .loadLabel(pm).toString());
            }
                    getListView().setOnItemClickListener(this);
    
            setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, results));
        }
    
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
    
    }
    
    }
    

    Explaination:

    ListActivity has a default layout that consists of a single, full-screen list in the center of the screen.so you can directly set the adapter.

    Have a look at docs for reference

    I hope it will be helpful !!

    0 讨论(0)
提交回复
热议问题