简体   繁体   中英

ListView adding items

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?

You need to define own layout for the row (which can hold any number of elements) and then override getView() in your adapter, inflate your layout and return it when listview ask for it. This gives you ability to make each row look as you want, or have each row different depending on data type (it absolutely up to you what your getView() returns - list view will just display what adapter retuns). I am not giving any code here as literally any Android ListView tutorial will give you that as this is quite fundamental.

Randomly spotted tutorials: here or here

Your ArrayAdapter displays the content of AppItem.toString() in the layout you give it.

You can override toString() to display the label:

public void toString() {
    return _appName;
}

in the onItemClick, you can get the item by calling

AppItem item = (AppItem) parent.getItemAtPosition(position);

or, given that appItem is accessible (either final or a field of your activity rather than a local variable to the method)

apps.get(position);

You can then have the package and call the activity :

Intent intent = getPackageManager().getLaunchIntentForPackage(item.getAapp());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM