具有gridview,其setArray的参数名为ImageAdapter的设置适配器。适配器内部具有onclicklistener,在此期间从arraylist中删除一项,然后当我在gridview项目中使用此行ImageAdapter.notifyDataSetChanged
时删除。现在我需要在活动中更改arrayArray,以便如何获取它。
这是我的代码:
public class ImageAdapter extends BaseAdapter {
Context context;
ArrayList<String> listCheck = new ArrayList<String>();
ImageAdapter adapter = this;
public ImageAdapter(Context context, ArrayList<String> list) {
this.context = context;
listCheck = list;
}
@Override
public int getCount() {
return listCheck.size();
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder121 holder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(
R.layout.item_gridmain, null);
holder = new ViewHolder121();
holder.imageView = (ImageView) convertView
.findViewById(R.id.img_selected_image);
holder.close = (ImageButton) convertView
.findViewById(R.id.img_btn_cancel);
convertView.setTag(holder);
}
else {
holder = (ViewHolder121) convertView.getTag();
}
holder.close.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg1) {
listCheck.remove(position);
adapter.notifyDataSetChanged();
}
}); Bitmap bm = decodeSampledBitmapFromUri(listCheck.get(position), 220,
220);
holder.imageView.setImageBitmap(bm);
return convertView;
} }
片段代码:
ImageAdapter mainActivityAdapter = new ImageAdapter(getActivity(), ar1);
gridview_withimage.setAdapter(mainActivityAdapter);
问题:如何将数组列表从ImageAdapter更改为Fragement
最佳答案
如何将数组列表从ImageAdapter更改为Fragement
在ImageAdapter
中创建一个方法,该方法将返回ArrayList用作适配器中的数据源:
public ArrayList<String> getModifyList() {
return listCheck;
}
调用Fragment中的
getModifyList
方法以使用Adapter对象获取ArrayList:gridview_withimage.setAdapter(mainActivityAdapter);
gridview_withimage.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
ArrayList<String> arrNewList=mainActivityAdapter.getModifyList();
}
}, 100);