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 extends Activity {
private ListView lView;
private ArrayList results = new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lView = (ListView) findViewById(R.id.lvApps);
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());
}
lView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, results));
}
}
Use following piece of code:
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 !!
implement OnItemClickListener
if you're gonna use a
ListActivity
then you don't need this line:BUT that specific
ListView
being referred to right now (provided it's in the corresponding xml layout) must have it's id changed to