我想做一个小应用程序,将所有已安装的应用程序放入微调器,然后为您提供选项,也选择一个得到UID
。不幸的是,我找不到任何有关如何使用所有已安装的应用程序填充微调器的指南(如您所见代码缺少spinner
)
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
}
}
}
答案 0 :(得分:1)
请参阅here填充微调器的完整教程。
您无法在微调器中添加ApplicationInfo
,因此您需要使用adpater。但教程可以比我更好地解释这一点;)。
例如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)
}