简体   繁体   English

android:如何使用string []填充微调框,string []是已安装软件包的列表?

[英]android: How to populate a spinner with a string[] which is a list of installed packages?

 final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    final List pkgAppsList = this.getPackageManager().queryIntentActivities( mainIntent, 0);
    final String[] apps = (String[]) pkgAppsList.toArray();
    Spinner appSpinner = (Spinner) findViewById(R.id.spinner1);
    ArrayAdapter<CharSequence> appAdapter = new ArrayAdapter(this, apps, android.R.layout.simple_spinner_item);
    appAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    appSpinner.setAdapter(cloudAdapter);

The previous code is throwing out errors for me in eclipse. 之前的代码在eclipse中为我抛出了错误。 I understand how to get the list of installed apps, and I understand how to populate a spinner using the createFromResource method. 我了解如何获取已安装应用程序的列表,并且了解如何使用createFromResource方法填充微调框。 However I've never attempted to do so in this manner? 但是我从未尝试过以这种方式这样做吗? Anyone able to direct me in the right direction? 有人能引导我正确的方向吗?

Eventough it is late, you should use this constructor of ArrayAdapter instead : 如果已经晚了,您应该改用ArrayAdapter此构造函数:

ArrayAdapter<String> appAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, apps);
appAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
appSpinner.setAdapter(appAdapter);

Create a file named arrays.xml inside values folder. values文件夹中创建一个名为arrays.xml文件。

Inside that give: 里面给出:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="array_name">
        <item>value1</item>
        <item>value2</item>
    </string-array>
</resources>

Then inside your spinner,give: 然后在您的微调框内输入:

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.array_name, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
spinner.setAdapter(adapter);

Hope this may work for you. 希望这对您有用。

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

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