我想在列表视图中列出用户安装的应用。我尝试了很多方法,例如@android:id和@ + id但似乎没有任何工作......可以指出这段代码中的错误是什么..
applist.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
listrow.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:textSize="16sp"
android:id="@+id/rowTextView">
</TextView>
InstalledApp类
public class InstalledApps extends ListActivity{
private ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.applist);
listview = (ListView)findViewById(R.id.listView1);
PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
List<ApplicationInfo> installedApps = new ArrayList<ApplicationInfo>();
for(ApplicationInfo app : packages) {
//checks for flags; if flagged, check if updated system app
if((app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 1) {
//installedApps.add(app);
//it's a system app, not interested
} else if ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
//Discard this one
//in this case, it should be a user-installed app
} else {
installedApps.add(app);
}
}
ArrayAdapter<ApplicationInfo> adapter = new ArrayAdapter<ApplicationInfo>(this,
R.layout.listrow, installedApps);
listview.setAdapter(adapter);
}
}
答案 0 :(得分:1)
ListActivity具有默认布局,该布局由屏幕中央的单个全屏列表组成。但是,如果需要,可以通过在onCreate()中使用setContentView()设置自己的视图布局来自定义屏幕布局。要做到这一点,你自己的视图必须包含一个ID为“@android:id / list”的ListView对象(如果它在代码中则列出)
答案 1 :(得分:1)
如果您延长public class InstalledApps extends Activity
,那么您的代码似乎只是一个小错误public class InstalledApps extends ListActivity
而不是ListActivity
,那么您将收到错误Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'