我想写一个非常简单的应用程序,显示设备的列表视图上每个已安装的应用程序的名称。我使用谷歌的ListView的教程为基础。
下面是我的code:
@覆盖
公共无效的onCreate(包savedInstanceState)
{
super.onCreate(savedInstanceState);
最终PackageManager下午= this.getPackageManager();
意向意图=新的意图(Intent.ACTION_MAIN,NULL);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
最后的ArrayList< ResolveInfo>名单=
(ArrayList的< ResolveInfo>)pm.queryIntentActivities(意向,
PackageManager.PERMISSION_GRANTED);
对于(ResolveInfo rInfo:名单)
{
Log.i(TAG:安装的应用程序+ rInfo.activityInfo。
applicationInfo.loadLabel(下午)的ToString());
}
最后ArrayAdapter< ResolveInfo>适配器=
新的ArrayAdapter< ResolveInfo>(这一点,R.layout.list_item,列表)
{
@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup)
{
如果(convertView == NULL)
convertView = LayoutInflater.from(parent.getContext())。
膨胀(R.layout.list_item,父母,假);
最后字符串文本= list.get(位置).activityInfo。
applicationInfo.loadLabel(PM)的ToString();
((TextView中)convertView.findViewById(R.id.text))的setText(文本)。
最终绘制对象绘制= list.get(位置).activityInfo.applicationInfo.loadIcon(PM);
((ImageView的)convertView.findViewById(R.id.image))setImageDrawable(绘制);
返回convertView;
}
};
setListAdapter(适配器);
ListView的LV = getListView();
lv.setTextFilterEnabled(真正的);
lv.setOnItemClickListener(新OnItemClickListener(){
公共无效onItemClick(适配器视图<>母公司视图中查看,
INT位置,长的id){
//在项目点击活动
//这是我想送选择要传递给方法的应用程序的包名。
}
});
}
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=横向机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT>
< ImageView的机器人:ID =@ + ID /图像
机器人:layout_width =50dp机器人:layout_height =50dp/>
< TextView的机器人:ID =@ + ID /文
机器人:layout_width =FILL_PARENT机器人:layout_height =WRAP_CONTENT
机器人:填充=10dp机器人:TEXTSIZE =16SP/>
< / LinearLayout中>
更新:我现在需要使用OnItemClickListener传递应用程序的包名称选择的方法
。 解决方案要在列表中正确显示你的活动的名字,你应该重写的 getView
方法,你的 ListAdapter
,并设置一些本地变量为最终(与内部类工作):
@覆盖
公共无效的onCreate(包savedInstanceState)
{
super.onCreate(savedInstanceState);
最终PackageManager下午= this.getPackageManager();
意向意图=新的意图(Intent.ACTION_MAIN,NULL);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
最后的ArrayList< ResolveInfo>名单=
(ArrayList的< ResolveInfo>)pm.queryIntentActivities(意向,
PackageManager.PERMISSION_GRANTED);
对于(ResolveInfo rInfo:名单)
{
Log.i(TAG:安装的应用程序+ rInfo.activityInfo。
applicationInfo.loadLabel(下午)的ToString());
}
最后ArrayAdapter< ResolveInfo>适配器=
新的ArrayAdapter< ResolveInfo>(这一点,R.layout.list_item,列表)
{
@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup)
{
convertView = super.getView(位置,convertView,父母);
最后字符串文本= list.get(位置).activityInfo。
applicationInfo.loadLabel(PM)的ToString();
((TextView中)convertView).setText(文本);
返回convertView;
}
};
setListAdapter(适配器);
ListView的LV = getListView();
lv.setTextFilterEnabled(真正的);
}
此方式,您有您的自定义 ArrayAdapter
实现,这显示applicationinfo在的TextView
。
您也可以做到这一点,如果你创建一个新的的ArrayList<字符串>
,并填充它里面的循环,你登录的应用程序:
最后的ArrayList<字符串> labelList =新的ArrayList<字符串>();
对于(ResolveInfo rInfo:名单)
{
Log.i(TAG:安装的应用程序+ rInfo.activityInfo。
applicationInfo.loadLabel(下午)的ToString());
labelList.add(rInfo.activityInfo。
applicationInfo.loadLabel(下午)的ToString());
}
最后ArrayAdapter<字符串>适配器=新的ArrayAdapter<字符串>(这一点,
R.layout.list_item,labelList);
setListAdapter(适配器);
然后你使用这个新的 labelList
作为源的适配器
。
更新
要包含的图标到项目渲染器,重写的 getView
方法将如下所示:
@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup)
{
如果(convertView == NULL)
convertView = LayoutInflater.from(parent.getContext())。
膨胀(R.layout.list_item,父母,假);
最后字符串文本= list.get(位置).activityInfo。
applicationInfo.loadLabel(PM)的ToString();
((TextView中)convertView.findViewById(R.id.text))的setText(文本)。
最终绘制对象绘制= list.get(位置).activityInfo.applicationInfo.loadIcon(PM);
((ImageView的)convertView.findViewById(R.id.image))setImageDrawable(绘制);
返回convertView;
}
和您的 RES /布局/ list_item.xml
布局文件必须包含在 文本
的TextView
和 图片
的ImageView
:
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=横向机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT>
< ImageView的机器人:ID =@ + ID /图像
机器人:layout_width =50dp机器人:layout_height =50dp/>
< TextView的机器人:ID =@ + ID /文
机器人:layout_width =FILL_PARENT机器人:layout_height =WRAP_CONTENT
机器人:填充=10dp机器人:TEXTSIZE =16SP/>
< / LinearLayout中>
I am trying to write a very simple app that displays the name of every installed app on the device in a listview. I am using Google's ListView tutorial as a base.
Here is my code:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
final PackageManager pm = this.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
final ArrayList<ResolveInfo> list =
(ArrayList<ResolveInfo>) pm.queryIntentActivities(intent,
PackageManager.PERMISSION_GRANTED);
for (ResolveInfo rInfo : list)
{
Log.i(TAG, ": Installed Applications " + rInfo.activityInfo.
applicationInfo.loadLabel(pm).toString());
}
final ArrayAdapter<ResolveInfo> adapter =
new ArrayAdapter<ResolveInfo>(this, R.layout.list_item, list)
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
convertView = LayoutInflater.from(parent.getContext()).
inflate(R.layout.list_item, parent, false);
final String text = list.get(position).activityInfo.
applicationInfo.loadLabel(pm).toString();
((TextView)convertView.findViewById(R.id.text)).setText(text);
final Drawable drawable = list.get(position).activityInfo.applicationInfo.loadIcon(pm);
((ImageView)convertView.findViewById(R.id.image)).setImageDrawable(drawable);
return convertView;
}
};
setListAdapter(adapter);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// On Item Click Activity
// This is where I want to send the Package Name of the app selected to be passed to a method.
}
});
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="@+id/image"
android:layout_width="50dp" android:layout_height="50dp" />
<TextView android:id="@+id/text"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:padding="10dp" android:textSize="16sp" />
</LinearLayout>
UPDATE: I now need to use an OnItemClickListener to pass the Package Name of the App selected to a method.
解决方案To display your activities' names correctly in the list, you should override the getView
method of your ListAdapter
, and set some of your local variables as final (to work with inner class):
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
final PackageManager pm = this.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
final ArrayList<ResolveInfo> list =
(ArrayList<ResolveInfo>) pm.queryIntentActivities(intent,
PackageManager.PERMISSION_GRANTED);
for (ResolveInfo rInfo : list)
{
Log.i(TAG, ": Installed Applications " + rInfo.activityInfo.
applicationInfo.loadLabel(pm).toString());
}
final ArrayAdapter<ResolveInfo> adapter =
new ArrayAdapter<ResolveInfo>(this, R.layout.list_item, list)
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
convertView = super.getView(position, convertView, parent);
final String text = list.get(position).activityInfo.
applicationInfo.loadLabel(pm).toString();
((TextView)convertView).setText(text);
return convertView;
}
};
setListAdapter(adapter);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
}
This way you have your custom ArrayAdapter
implementation, which displays the proper label of applicationinfo in the TextView
.
You can also achieve this, if you create a new ArrayList<String>
, and populate it inside the for cycle where you log the applications:
final ArrayList<String> labelList = new ArrayList<String>();
for (ResolveInfo rInfo : list)
{
Log.i(TAG, ": Installed Applications " + rInfo.activityInfo.
applicationInfo.loadLabel(pm).toString());
labelList.add(rInfo.activityInfo.
applicationInfo.loadLabel(pm).toString());
}
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.list_item, labelList);
setListAdapter(adapter);
Then you use this new labelList
as the source of your adapter
.
Update
To include the icon into the item renderers, the overridden getView
method would look like:
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
convertView = LayoutInflater.from(parent.getContext()).
inflate(R.layout.list_item, parent, false);
final String text = list.get(position).activityInfo.
applicationInfo.loadLabel(pm).toString();
((TextView)convertView.findViewById(R.id.text)).setText(text);
final Drawable drawable = list.get(position).activityInfo.applicationInfo.loadIcon(pm);
((ImageView)convertView.findViewById(R.id.image)).setImageDrawable(drawable);
return convertView;
}
and your res/layout/list_item.xml
layout file has to contain the text
TextView
and the image
ImageView
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="@+id/image"
android:layout_width="50dp" android:layout_height="50dp" />
<TextView android:id="@+id/text"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:padding="10dp" android:textSize="16sp" />
</LinearLayout>
上一篇:恕不另行通知图标或WAP-PUSH信息的Android手机短信拦截图标、另行通知、手机短信、恕不
下一篇:如何安装/更新/删除APK使用&QUOT; PackageInstaller&QUOT;在Android中大号类?大号、QUOT、APK、Android