我需要在点击按钮时重新加载适配器。但是我该怎么做呢?我的代码如下。
public class CustomGridAdapter extends BaseAdapter {
private Context context;
//private final String[] gridValues;
ArrayList<Bitmap> arrayList;
ImageButton i1;
//Constructor to initialize values
public CustomGridAdapter(Context context, ArrayList<Bitmap> ob) {
arrayList = ob;
this.context = context;
// this.gridValues = gridValues;
}
@Override
public int getCount() {
return arrayList.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView( int position, View convertView, ViewGroup parent) {
final int pos = position;
// LayoutInflator to call external grid_item.xml file
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from grid_item.xml ( Defined Below )
gridView = inflater.inflate(R.layout.grid_item, null);
ImageView imageView = (ImageView) gridView
.findViewById(R.id.grid_item_image);
imageView.setImageBitmap(arrayList.get(position));
ImageButton imageButton = (ImageButton) gridView
.findViewById(R.id.imageButton);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("myLogs", "Yes, onclick "+pos);
arrayList.remove(pos);
!!!THERE I NEED TO RELOAD IT
}
});
} else {
gridView = (View) convertView;
}
return gridView;
}
}
答案 0 :(得分:2)
首先,您必须设置事件并在活动中使用它...您必须修改适配器并向其添加OnClickListener
attr:
private Context context;
private OnClickListener listener;
ArrayList<Bitmap> arrayList;
ImageButton i1;
//Constructor to initialize values
public CustomGridAdapter(Context context, ArrayList<Bitmap> ob, OnClickListener listener) {
arrayList = ob;
this.context = context;
this.listener = listener;
}
然后设置setOnClickListener
,您必须执行以下操作:
imageView.setImageBitmap(arrayList.get(position));
ImageButton imageButton = (ImageButton) gridView
.findViewById(R.id.imageButton);
imageButton.setOnClickListener(listener);
然后,在java中,您需要让您的活动实现OnClickListener,然后覆盖onClickListener方法,您可以使用以下方法重新加载列表:
@Override
public void onClick(View v) {
adapter = new CustomGridAdapter(MyActivity.this, myArrayListWithMyData, MyActivity.this);
listView.setAdapter(adapter);
listView.invalidate();
}
要设置适配器,您可以传递上下文(例如this
)作为OnClickListener
的参数:
adapter = new CustomGridAdapter(this, myArrayListWithMyData, this);
希望它有所帮助!
答案 1 :(得分:1)
我相信你可以使用NotifyDataSetChanged();那应该重新加载数据。
答案 2 :(得分:1)
您的代码的问题在于您的列表正在重用旧视图,并且在重用视图时,不会为当前视图重新注册侦听器,因为只有在创建新视图(未重用)时才注册侦听器。使用以下代码:
...
if (convertView == null) {
gridView = new View(context);
// get layout from grid_item.xml ( Defined Below )
gridView = inflater.inflate(R.layout.grid_item, null);
} else {
gridView = (View) convertView;
}
ImageView imageView = (ImageView) gridView.findViewById(R.id.grid_item_image);
imageView.setImageBitmap(arrayList.get(position));
ImageButton imageButton = (ImageButton) gridView.findViewById(R.id.imageButton);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("myLogs", "Yes, onclick " + pos);
arrayList.remove(pos);
notifyDataSetChanged();
}
});
...
有关使用和优化ListView
的详细信息,请参阅this Blog。