我正在尝试制作列表视图,其中单行中有多个图像,如下图所示
错误
我想因为arrayList<>而知道错误当我滚动viewPager时,大小会改变....但我没有解决方案..如何做到这一点
我在数据库中存储了图像路径..当打开listview
时,从sdcard获取图像 @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.notidata, container, false);
lvContent = (ListView) rootView.findViewById(R.id.lv_conetent);
lvContent.setAdapter(adapter);
return rootView;
}
活动
public class CustomCursorAdapter extends CursorAdapter {
private LayoutInflater cursorInflater;
//for imge
ArrayList<Stuff> mArrayList;
CustomPagerAdapter mCustomPagerAdapter;
public CustomCursorAdapter(Context context, Cursor c, boolean autoRequery) {
super(context, c, autoRequery);
// TODO Auto-generated constructor stub
cursorInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public void bindView(View view, final Context context, final Cursor cursor) {
// TODO Auto-generated method stub
TextView title = (TextView) view.findViewById(R.id.tv_title);
TextView summary = (TextView) view.findViewById(R.id.tv_summary);
TextView name = (TextView) view.findViewById(R.id.tv_name);
title.setText(cursor.getString(cursor.getColumnIndex("title")));
name.setText(cursor.getString(cursor.getColumnIndex("name")));
summary.setText(cursor.getString(cursor.getColumnIndex("summary")));
// final String img = cursor.getString(cursor.getColumnIndex("img"));
String id = cursor.getString(cursor.getColumnIndex("_id"));
mArrayList = new ArrayList<Stuff>();
if (Getimage(context, id) == 1) {
mCustomPagerAdapter = new CustomPagerAdapter(context, mArrayList);
ViewPager mViewPager = (ViewPager) view.findViewById(R.id.viewpager_img);
mViewPager.setVisibility(view.VISIBLE);
// mViewPager.setId(Integer.parseInt(id));
mViewPager.setAdapter(mCustomPagerAdapter);
} else {
ViewPager mViewPager = (ViewPager) view.findViewById(R.id.viewpager_img);
mViewPager.setVisibility(view.GONE);
}
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// TODO Auto-generated method stub
return cursorInflater.inflate(R.layout.notidata_layout, parent, false);
}
/**
* for horizontal imageView
*/
class Stuff {
private String imgName;
private int imgPosition;
// getters
public String getImgName() {
return this.imgName;
}
public void setImgName(String imgName) {
this.imgName = imgName;
}
public Integer getImgPosition() {
return this.imgPosition;
}
public void setImgPosition(int imgPosition) {
this.imgPosition = imgPosition;
}
}
private int Getimage(Context mContext, String id) {
int returnValue = 1;
DatabaseHelper databaseHelper = new DatabaseHelper(mContext);
SQLiteDatabase db = databaseHelper.getWritableDatabase();
db = mContext.openOrCreateDatabase("artlovers.db",
SQLiteDatabase.CREATE_IF_NECESSARY, null);
String[] columns = {"_id,img"};
String selection = "p_id=?";
String[] selectionArg = {id};
// Log.e("id of database",""+id);
Cursor c = db.query("img", columns, selection, selectionArg, null, null,
null);
// Log.e("", "" + DatabaseUtils.dumpCursorToString(c));
int counter = 0;
// Log.e("","------"+c.getCount());
if (c.getCount() != 0) {
while (c != null && c.moveToNext()) {
counter++;
Stuff s = new Stuff();
s.setImgName(c.getString(c.getColumnIndex("img")));
s.setImgPosition(counter);
mArrayList.add(s);
// Log.i("", "name=" + c.getString(c.getColumnIndex("img")) + " counter" + counter);
}
returnValue = 1;
} else {
returnValue = 0;
}
return returnValue;
}
class CustomPagerAdapter extends PagerAdapter {
Context mContext;
LayoutInflater mLayoutInflater;
private int mcount;
ArrayList<Stuff> marrayList;
public CustomPagerAdapter(Context context, ArrayList<Stuff> arrayList1) {
mContext = context;
marrayList = arrayList1;
mLayoutInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
//
@Override
public int getCount() {
// Log.e("", "" + marrayList.size());
// notifyDataSetChanged();
return marrayList.size();
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View itemView = mLayoutInflater.inflate(R.layout.pager_item,
container, false);
ImageView imageView = (ImageView) itemView
.findViewById(R.id.imageView);
imageView.setImageBitmap(BitmapFactory.decodeFile(new File(
"/sdcard/artlovers/"
+ mArrayList.get(position).getImgName())
//error at "mArrayList.get(position).getImgName())"
.getAbsolutePath()));
container.addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
}
}
*适用于Listview的CustomCursor适配器,具有用于视图寻呼机的寻呼机适配器*
$http.get()
预先感谢您的建议,意见和答案 。
答案 0 :(得分:1)
错误
mArrayList.get(position).getImgName())
mArrayList = new ArrayList<Stuff>();
if (Getimage(context, id) == 1) {
mCustomPagerAdapter = new CustomPagerAdapter(context, mArrayList);
您在适配器中使用
ArrayList<Stuff> marrayList;
你对 marrayList&amp; mArrayList 强>
需要使用marrayList,最好遵循android指南来解决这类错误。