Accualy我读了所有帖子,但我无法解决我的问题。我需要帮助。
我有gridview。我正在填充一个baseadapter网格。我的网格有ImageViews和CheckBoxes。我是LongTouching任何网格并检查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)行并删除else块,则检查不会更改但是当复选框因滚动而无法看到时,返回并滚动到该复选框并且未选中。没有永久检查。
我需要大力帮助。谢谢......
答案 0 :(得分:0)
您需要将检查状态与后备数据保持一致,而不是(仅)在视图中。这意味着OnLongClickListener
必须向tar
地图添加项目:
tar.put("checked", "true");
并且getView(…)
方法(在doubleclick侦听器外部)需要初始化该数据的复选框:
String checked = tar.get("checked");
grid.findViewWithTag(position).setChecked(
checked != null && Boolean.getBoolean(checked)
);
注意:您必须将tar
声明为final
才能在侦听器内访问它。
答案 1 :(得分: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;
}