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));
}
}
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"
.....
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 !!
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
}
}