2015-07-21 99 views
0

Accualy我阅读所有帖子,但我无法解决我的问题。我需要帮助。当GridView滚动时,它会更改复选框选择

我有gridview。我正在填充网格基础适配器。我的网格有ImageViews和CheckBoxes。我正在打开任何网格并检查CheckBox。一切都在这里完成。

但是,当我滚动gridview所有选择变化。我尝试从其他帖子的一些解决方案,但我无法修复问题。

适配器GetView方法;

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 

    View grid; 

    if (convertView == null) { 
     grid = new View(mContext); 
     LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     grid = inflater.inflate(R.layout.mygrid, parent, false); 
    } else 
    { 
     grid = (View) convertView; 
    } 

    ImageView imageView = (ImageView) grid.findViewById(R.id.imagepart); 
    final CheckBox ch = (CheckBox)grid.findViewById(R.id.checkBox); 
    ch.setTag(position); 

    HashMap<String,String> tar; 
    tar = data.get(position); 

    imageLoader.DisplayImage(tar.get(FragmentB.FOTOYOL),imageView); 

    grid.setOnLongClickListener(new View.OnLongClickListener() { 
     @Override 
     public boolean onLongClick(View v) { 
      CheckBox chh = (CheckBox)v.findViewWithTag(position); 
      chh.setChecked(true); 
      return false; 
     } 
    }); 

    return grid; 
} 

如果我删除(convertView == NULL)线和删除其他块,然后,检查不改变,但是当复选框会看不见,因为滚动,带滚动返回到该复选框,并没有选中。没有永久检查。

我需要很大的帮助。谢谢...

回答

0

您需要保持与您的支持数据的检查状态,而不是(仅)在视图中。这意味着,OnLongClickListener具有将项目添加到tar地图:

tar.put("checked", "true"); 

getView(…)方法(双击听者外)需要初始化从该数据的复选框:

String checked = tar.get("checked"); 
grid.findViewWithTag(position).setChecked(
     checked != null && Boolean.getBoolean(checked) 
    ); 

注:您必须声明tarfinal,因为它在侦听器内可以访问。

+0

你能解释一下吗?抱歉。 –

+0

GridView旨在重用视图以提高效率。例如,您可能有100项数据,但只有10个视图可以放在屏幕上。如果显示数据项1的视图在顶部的屏幕上滚动,那么该视图将移动到底部,并成为项目11的“convertView”。项目1的所有数据仍然存在,因此您必须覆盖*全部*它显示数据11正确(即你必须清除复选框)。当您向另一个方向滚动时,您希望复选框的设置与滚动前相同,这就是为什么您必须跟踪数据本身中的已检查状态。 – Barend

+0

谢谢,但我不能正确应用你的代码。我仅使用tar图像来显示图像。我必须在clicklistener内部分配一个新的“tar地图”吗?你能更新我的代码吗?我很久没有打过这个东西..我真的很抱歉,因为你更累了.. –

0

我在Barend的帮助下找到了解决方案;

我改变了这样的getview方法;

@Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 

     View grid; 

      grid = new View(mContext); 
      LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      grid = inflater.inflate(R.layout.mygrid, parent, false); 

     ImageView imageView = (ImageView) grid.findViewById(R.id.imagepart); 
     final CheckBox ch = (CheckBox)grid.findViewById(R.id.checkBox); 
     ch.setTag(position); 

     final HashMap<String,String> tar; 
     tar = data.get(position); 

     imageLoader.DisplayImage(tar.get(FragmentB.FOTOYOL), imageView); 

     String checked = tar.get("checked"); 
     if (checked == "true") 
     { 
      CheckBox hh = (CheckBox)grid.findViewWithTag(position); 
        hh.setChecked(true); 
     } 
     else if (checked == "false") 
     { 
      CheckBox hh = (CheckBox)grid.findViewWithTag(position); 
      hh.setChecked(false); 
     } 

     grid.setOnLongClickListener(new View.OnLongClickListener() { 
      @Override 
      public boolean onLongClick(View v) { 
       CheckBox chh = (CheckBox)v.findViewWithTag(position); 
       if (chh.isChecked()) 
       { 
        chh.setChecked(false); 
        tar.put("checked","false"); 
       } 
       else 
       { 
        chh.setChecked(true); 
        tar.put("checked", "true"); 
       } 
       return true; 
      } 
     }); 

     return grid; 
    } 
相关问题