Okay, first off, i started (trying) to learn Java about 2 days ago.
im trying to make an application that lists all installed applications (activityInfo.loadLabel)
and i want to launch the application on item click (activityInfo.packageName)
i have these stored in a list of AppItems
class AppItem{
String _appname;
public String getAppname(){return _appname;};
public void setAppname(String value){_appname = value;};
String _app;
public String getApp(){ return _app; };
public void setApp(String value){_app = value; };
}
I can add an array of strings to my listview just fine.
but how do i add 2 different values to the same row (so to speak) in a listview – in Java.
please tell me if you dont understand my question – and il’l try to elaborate best possible
here is my code:
ArrayList<AppItem> apps;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List<ResolveInfo> pkgAppsList = getPackageManager().queryIntentActivities(mainIntent, 0);
AppItem appItem = new AppItem();
for (ResolveInfo applicationInfo : pkgAppsList) {
appItem._appname = (String) applicationInfo.activityInfo.loadLabel(getPackageManager());
appItem._app= applicationInfo.activityInfo.packageName;
apps.add(appItem);
}
ArrayAdapter<AppItem> adapter = new ArrayAdapter<AppItem>(this,android.R.layout.simple_list_item_1,apps);
ListView listView = new ListView(this);
listView.setAdapter(adapter);
setContentView(listView);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String item = ((TextView)view).getText().toString();
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
});
}
class AppItem{
String _appname;
public String getAppname(){return _appname;};
public void setAppname(String value){_appname = value;};
String _app;
public String getApp(){ return _app; };
public void setApp(String value){_app = value; };
}
or
is there an alternative to using
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.package.address");
startActivity(LaunchIntent);
would i be able to start an application via its label name?
Your ArrayAdapter displays the content of
AppItem.toString()in the layout you give it.You can override toString() to display the label:
in the onItemClick, you can get the item by calling
or, given that appItem is accessible (either final or a field of your activity rather than a local variable to the method)
You can then have the package and call the activity :