gpt4 book ai didi

java - 列出所有已安装的应用程序并将它们放入微调器中

转载 作者:行者123 更新时间:2023-12-01 12:45:08 24 4
gpt4 key购买 nike

我想做一个小应用程序,将所有已安装的应用程序放入旋转器中,然后为您提供选择一个并获取UID的选项。不幸的是,我找不到任何有关如何使用所有已安装的应用程序填充微调器的指南(如您所见,代码缺少微调器)

enter code here

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;


public class MainActivity extends Activity {
public static String TAG="activity";
private Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
additemtospinner();
addListenerOnSpinnerItemSelection();
}

public void additemtospinner()
{
spinner = (Spinner)findViewById(R.id.spinner1);
List<String> list = new ArrayList<String>();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
}
public void addListenerOnSpinnerItemSelection() {
spinner.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}
public class CustomOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View arg1,
int app_pos, long app_id) {
// TODO Auto-generated method stub
int UID;
String selectapp=parent.getItemAtPosition(app_pos).toString();
final PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(
PackageManager.GET_META_DATA);
for (ApplicationInfo packageInfo : packages) {
if(packageInfo.packageName.equals(selectapp)){
UID = packageInfo.uid;
break;
}

}

}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub

}
}

}

最佳答案

参见here填充微调器的完整教程。

您无法在微调器中添加 ApplicationInfo ,您需要为此使用适配器。但教程可以比我更好地解释这一点;)。

例如 ArrayAdpater 。您需要创建一个 List<String>

List<String> list = new ArrayList<String>();
list.add("list 1");
list.add("list 2");
list.add("list 3");

之后您需要创建一个 ArrayAdpater有了这个列表。

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

最后将适配器添加到 Spinner并且它将被填充。

spinner.setAdapter(dataAdapter);

编辑

您的代码不正确,您创建了一个列表,但它是空的。

List<String> list = new ArrayList<String>();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);

如果您的列表为空,您的微调框也为空:)

尝试这样的事情:

List<String> list = new ArrayList<String>();
PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo packageInfo : packages) {
list.add(packageInfo.packageName)
}

关于java - 列出所有已安装的应用程序并将它们放入微调器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24776751/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com