listview项目是重复的

时间:2012-08-16 09:22:39

标签: android listview

1.当我运行此应用程序时,它会在列表视图中显示包名称和ico,但它是异常的。 该项目是重复的。任何人都可以帮助我指出我的代码有什么问题?

/**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;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

从我的头顶看,你看起来已经定义了v两次然后它返回v因此重复列表。