我在每个单元格中都有一个带有一个ImageView和一个TextView的GridView。
逻辑运作良好。我引用了我的GridView,设置了Columns并设置了一个Adapter。在此期间,我有一个线程,将图像及其名称设置为列表。 (图像的一个DrawAble列表和标签的一个字符串列表)。为了满足我的用户,我想在线程将图像和标签添加到列表时更新GridView。
但是只有在我提供令人讨厌的静态变量时,更新才有效。 (我恨他们)。或者,如果我每次发生更新时都将适配器设置为GridView。 (这也很讨厌,因为你无法在ist更新期间滚动GridView。)
这是我的CustomAdapter:
public CustomAdapter(DialogBox dialogBoxActivity,List<Drawable> imported_scaled_images, List<String> imported_labels, int position) {
this.context = dialogBoxActivity;
this.scaledImage = imported_scaled_images.toArray(new Drawable[imported_scaled_images.size()]);
this.labels = imported_labels.toArray(new String[imported_labels.size()]);
this.used_cells = position;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void updateGrid(List<Drawable> imported_scaled_images, List<String> imported_labels, int position) {
this.scaledImage = imported_scaled_images.toArray(new Drawable[imported_scaled_images.size()]);
this.labels = imported_labels.toArray(new String[imported_labels.size()]);
this.used_cells = position;
this.notifyDataSetChanged();
Log.e("update","true");
}
@Override
public void notifyDataSetChanged() {
// TODO Auto-generated method stub
super.notifyDataSetChanged();
Log.e("notify","true");
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return used_cells;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class Holder {
ImageView img;
TextView tv;
public Holder(ImageView img, TextView tv) {
this.img = img;
this.tv = tv;
}
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView img;
TextView tv;
TextView tv_abc;
if(convertView == null) {
convertView = inflater.inflate(R.layout.customadapter_view_layout, null);
img = (ImageView) convertView.findViewById(R.id.img);
tv =(TextView) convertView.findViewById(R.id.tv);
//tv_abc = (TextView) convertView.findViewById(R.id.tv_normal_portrait_abc);
convertView.setTag(new Holder(img,tv));
} else {
Holder holder = (Holder) convertView.getTag();
img = holder.img;
tv = holder.tv;
//tv_abc = holder.tv_abc;
}
img.setImageDrawable(scaledImage[position]);
tv.setText(labels[position]);
notifyDataSetChanged();
return convertView;
}
}
在我的Activity上我有了updateGrid,我在MainThread上的TimerTask中调用它:
private void updateGrid() {
customAdapter.updateGrid(images, labels, position);
}
发生的事情是GridView保持空白。也许是因为当我将baseadapter设置为gridview时,Position为0。但这可能不是问题,因为当我更新它时应该识别getCount()中的新大小。再次,逻辑运作良好。添加的图像和方法updateGrid和notiyDataSetChanged按我想要的方式调用。但GridView保持不变,我不喜欢显示这些图像。(完全空白)。
希望有些东西可以提供一个好的解决方案。我感谢上面发布的代码的每一个帮助和每一个改进。(关于性能或其他)。谢谢!
更新:位置是我的变量,它计算可用的单元格。因为一些单元格是空的(= null)。由于我不知道将填充多少单元格(因为我按字母顺序对图像进行排序(对于每一行=&gt;一些单元格填充为null),我无法为BaseAdapters getCount方法提供一个数字在我的逻辑尚未完成之前)
答案 0 :(得分:1)
getCount返回总计没有项目计数而不是任何位置:
@Override
public int getCount() {
return imported_scaled_images.size();
}