简体   繁体   English

ListView添加项目

[英]ListView adding items

Okay, first off, i started (trying) to learn Java about 2 days ago. 好吧,首先,我大约2天前开始(尝试)学习Java。 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 我试图制作一个列出所有已安装的应用程序(activityInfo.loadLabel)的应用程序,我想在项目单击(activityInfo.packageName)时启动该应用程序,并将这些存储在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. 但是我如何在列表视图的同一行中添加2个不同的值(可以这么说)-在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. 您需要为行定义自己的布局(可以容纳任意数量的元素),然后在适配器中覆盖getView() ,对布局进行充气,并在listview要求时返回它。 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). 这使您能够使每一行看起来像您想要的那样,或使每一行根据数据类型而有所不同(这完全取决于您的getView()返回的内容-列表视图将仅显示适配器重新调整的内容)。 I am not giving any code here as literally any Android ListView tutorial will give you that as this is quite fundamental. 我在这里没有提供任何代码,因为从字面上看,任何Android ListView教程都会为您提供,因为这是非常基础的。

Randomly spotted tutorials: here or here 随机发现的教程: 此处此处

Your ArrayAdapter displays the content of AppItem.toString() in the layout you give it. 您的ArrayAdapter在您给它的布局中显示AppItem.toString()的内容。

You can override toString() to display the label: 您可以重写toString()以显示标签:

public void toString() {
    return _appName;
}

in the onItemClick, you can get the item by calling 在onItemClick中,您可以通过调用

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) 或者,鉴于appItem是可访问的(活动的最终值或字段,而不是方法的局部变量)

apps.get(position);

You can then have the package and call the activity : 然后,您可以获取包并调用活动:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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