1

我想学习android sqlite,所以我创建了一个即时标记应用程序,用户可以在其中标记他的照片并将其上传到他的网络(google +,fb,twitter)问题是我有一个ListFragment其中用户可以看到他所做的所有标记,列表中的图像元素存储在一个隐藏的文件夹和单词存储在一个sqlite数据库..问题是滚动向上或向下一些项目只是随机切换,即使我有更多比8个项目多8个第一项显示重复(即1-8而不是9-12我再次看到1-4) 唯一的问题可能是在适配器,但在调试会话的会话后,我无法找到问题我的适配器代码是 -listfragment中的奇怪错误

public class FlowAdapter extends BaseAdapter { 

    private Activity activity; 
    private ArrayList<HashMap<String, List<String>>> data; 
    private static LayoutInflater layoutInflater = null; 

    public FlowAdapter(Activity activityContext, 
      ArrayList<HashMap<String, List<String>>> data) { 
     this.activity = activityContext; 
     this.data = data; 
     this.layoutInflater = (LayoutInflater) activity 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    public int getCount() { 
     return data.size(); 
    } 

    public Object getItem(int position) { 
     return data.get(position); 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // method variables 
     ViewHolder cacheView; 
     HashMap<String, List<String>> photos = null; 
     photos = data.get(position); 
     ImageView iv; 
     FlowLayout flow; 

     ArrayList<String> subjects = new ArrayList<String>(); 

     if (convertView == null) { 
      cacheView = new ViewHolder(); 
      convertView = layoutInflater.inflate(R.layout.list_item, null); 
      flow = (FlowLayout) convertView.findViewById(R.id.flow_tags);; 

      // add the tags to the flowlayout 
      int size = photos.get(DatabaseHandler.KEY_TAGS).size(); 
      for (int i = 0; i < size; i++) { 
       String name = String.format("#%s", 
         photos.get(DatabaseHandler.KEY_TAGS).get(i)); 
       Bubble.getBubble(name, flow, subjects, activity, photos 
         .get(DatabaseHandler.KEY_THUMBNAILPATH).get(1), false); 
      } 
      cacheView.image = (ImageView)convertView.findViewById(R.id.list_image);//iv; 
      cacheView.tags = flow; 
      convertView.setTag(cacheView); 

     } 

     cacheView = (ViewHolder) convertView.getTag(); 
     cacheView.image.setImageBitmap(null); 
     DecodeTask task = new DecodeTask(cacheView.image); 
     task.execute(photos.get(DatabaseHandler.KEY_THUMBNAILPATH).get(1)); 
     cacheView.image.setTag(R.id.list_image, task); 

     return convertView; 
    } 

    static class ViewHolder { 
     static FlowLayout tags; 
     static ImageView image; 

     public static FlowLayout getFlowLayout() { 
      return tags; 
     } 
    } 
} 

的流布局是从这里开始 - http://nishantvnair.wordpress.com/2010/09/28/flowlayout-in-android/ 泡沫布局就是从这里开始 - http://nishantvnair.wordpress.com/2010/09/28/android-create-bubble-like-facebook/ 我用这个SO线程至后台加载图像 - Large ListView containing images in Android 任何帮助吗? = \ p.s 我知道已经有这样的应用程序[但实际上,但最好的学习来编写代码,因此我做了=]]

+0

如何放置此行** cacheView =(ViewHolder)convertView.getTag(); **在else条件内?不知道它是否会有帮助,但你可以试试看? –

+0

仍然没有效果...我怀疑它有某种方式连接到flowlayout或bubbleview,因为它们是动态的(换句话说,没有id附加到泡泡布局我只是将它们添加到运行时的flowlayout看看他们在我添加的链接上的实施) – crazyPixel

回答

4

原因是显而易见的!!!你有静态变量在ViewHolder

相反的:

static class ViewHolder { 
    static FlowLayout tags; 
    static ImageView image; 

    public static FlowLayout getFlowLayout() { 
     return tags; 
    } 
} 

必须是:

static class ViewHolder { 
    FlowLayout tags; 
    ImageView image; 

    public FlowLayout getFlowLayout() { 
     return tags; 
    } 
} 

更多关于static变量可用互联网上,总之他们不是实例变量所以改变它的值会改变它的每个实例(虽然static变量应该使用类引用来访问)。

UPDATE:

您还需要取消DecodeTask如果convertView是null作为教程说:Large ListView containing images in Android

但保持这些变量static是简单的错误,容易察觉,你可以检测2+2 = 5中的错误。说删除static修饰符似乎打破了一切意味着你的代码有其他错误。

+0

不工作......它甚至变得更糟.. = \ 在使用静态之前,有一个elemtn在整个列表“行为不端”,有些只是没有出现,现在有甚至没有一个图像在其正确的位置(旁边的相关说明)... – crazyPixel

+0

“更糟糕”并不一定意味着你做错了什么。这清楚地表明照片加载代码在某些地方是错误的。由于使变量静态似乎工作。在第二次看来,我能够确定你没有取消''DecodeTask'',就像'getView()'中的'else'中的教程所建议的那样。我会尽快更新答案,以便更清楚地指出问题。 –