问题描述:

1.when I running this app,it shows the package name and ico in the listview,but it is abnormal.

The item is reduplicate.can anybody help me point it out what's wrong with my code?

/**show the packageinfo.*/

public class MainForm extends Activity {

PackageManager pm;

ListView applist;

List<PackageInfo> appInfo;

ImageView itemImage;

TextView appName;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

applist = (ListView) findViewById(R.id.applist);

pm = getPackageManager();

appInfo = (List<PackageInfo>) pm.getInstalledPackages(0);

applist.setAdapter(new MyAdapter());

}

private class MyAdapter extends BaseAdapter {

private LayoutInflater mInflater;

public MyAdapter() {

// TODO Auto-generated constructor stub

this.mInflater=LayoutInflater.from(MainForm.this);

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

View v = null;

if (convertView == null) {

LayoutInflater li =mInflater;

v = li.inflate(R.layout.layout_applist, null);

itemImage = (ImageView) v.findViewById(R.id.item_icon);//get the imageview

appName = (TextView)v.findViewById(R.id.packname);//get the textview

} else {

v = convertView;

}

itemImage.setBackgroundDrawable(((PackageInfo)getItem (position)).applicationInfo.loadIcon(pm));

appName.setText(((PackageInfo)getItem(position)).applicationInfo.loadLabel(pm).toString());

return v;

}

@Override

public int getCount() {

// TODO Auto-generated method stub

return appInfo.size();

}

@Override

public Object getItem(int position) {

// TODO Auto-generated method stub

return appInfo.get(position);

}

@Override

public long getItemId(int position) {

// TODO Auto-generated method stub

return position;

}

}

}

网友答案:

from the top of my head, it looks like you've defined v twice then it returns v hence duplicated list.

相关阅读:
Top