インストールされているすべてのアプリケーションをカスタムリストビューに一覧表示するアプリを作成しました。残念ながら、アクティビティは開始時にクラッシュします! 誰でもこれについて私を助けてください!エラーが表示されないため、エラーが見つかりません。
AppActivity.java
package com.ex.layout;
import java.util.List;
import android.app.Activity;
import android.content.pm.PackageInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
public class AppActivity extends Activity implements OnItemClickListener{
String[] name = getaAppName();
String[] packs = getPackName();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView li = (ListView) findViewById(R.id.applist);
ListviewAdapet list = new ListviewAdapet(this, name, packs);
li.setAdapter(list);
li.setOnItemClickListener(this);
}
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(this,"Title => "+name[position]+"=> n Description"+packs[position], Toast.LENGTH_SHORT).show();
}
public String[] getaAppName() {
List<PackageInfo> apps = getPackageManager().getInstalledPackages(0);
String appname[] = new String[apps.size()];
for (int number = 0; number < apps.size(); number++) {
PackageInfo pack = apps.get(number);
appname[number] = pack.applicationInfo.loadLabel(getPackageManager()).toString();
}
return appname;
}
public String[] getPackName(){
List<PackageInfo> apps = getPackageManager().getInstalledPackages(0);
String packnames[] = new String[apps.size()];
for (int i = 0; i < apps.size(); i++) {
PackageInfo pack = apps.get(i);
packnames[i] = pack.packageName;
}
return packnames;
}
}
次のクラスです!baseadapter を拡張して出力を取得します。listviewadapet.java
package com.ex.layout;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class ListviewAdapet extends BaseAdapter{
Activity context;
String[] appname , packname;
public ListviewAdapet(Activity Context, String[] appname , String[] packname) {
// TODO Auto-generated constructor stub
super();
this.appname = appname;
this.packname = packname;
this.context = Context;
}
public class Viewholder{
TextView appname;
TextView packname;
}
public int getCount(String[] array) {
// TODO Auto-generated method stub
int count = array.length;
return count;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Viewholder view;
LayoutInflater inflator = context.getLayoutInflater();
if (convertView == null) {
convertView = inflator.inflate(R.layout.listitem, null);
view = new Viewholder();
view.appname = (TextView) convertView.findViewById(R.id.appname);
view.packname= (TextView) convertView.findViewById(R.id.packagename);
convertView.setTag(view);
} else {
view = (Viewholder) convertView.getTag();
}
view.appname.setText(appname[position]);
view.packname.setText(packname[position]);
return convertView;
}
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
}
これについて私を助けてください!ありがとうございました。