listview的项目重复

问题描述:

1.当我运行这个应用程序,它显示包名和ico在列表视图,但它是不正常的。 该项目是reduplicate.can任何人都可以帮我指出我的代码有什么问题?listview的项目重复

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

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

+0

谢谢。我想我得到的解决方案。 – anonym 2012-08-22 03:31:18

+0

说实话,你的答案不是正确的答案。无论如何,thans的回复! – anonym 2012-08-30 03:29:58