我正在使用自定义listView
在Adapter
中显示图片和文字。点击按钮后,我想用下一张图片替换图片。但是它只是在最后添加列表而不是replace.Below是我的代码片段:
从sqlite
获取图像的方法 public void BlockData() {
dataBase = dbHelper.getReadableDatabase();
Cursor mCursor = dataBase.rawQuery("SELECT * FROM "+ dbHelper.TABLE_NAME + " WHERE " + dbHelper.KEY_HNUM+ "=" + id, null);
if (mCursor.moveToFirst()) {
do {
category_Id.add(mCursor.getString(mCursor.getColumnIndex(dbHelper.KEY_HNUMM)));
} while (mCursor.moveToNext());
lv.setAdapter(new BlockAdapter(getApplicationContext(),category_Id,prgmImages));
stopManagingCursor(mCursor);
mCursor.close();
}
}
在自定义视图中设置
public View getView(final int pos, View child, final ViewGroup parent) {
final Holder mHolder;
LayoutInflater layoutInflater;
if (child == null) {
layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
child = layoutInflater.inflate(R.layout.secondlist, null);
mHolder = new Holder();
mHolder.txt_id = (TextView) child.findViewById(R.id.testingg);
mHolder.tx_img = (ImageView) child.findViewById(R.id.sac_img);
mHolder.butnxt = (Button) child.findViewById(R.id.button2);
mHolder.butpre = (Button) child.findViewById(R.id.button1);
child.setTag(mHolder);
} else {
mHolder = (Holder) child.getTag();
}
mHolder.txt_id.setText(category_Id.get(pos));
if (pos == 0) {
imgs = getResources().obtainTypedArray(R.array.sac_imgs);
mHolder.tx_img.setImageResource(imgs.getResourceId(id - 1, -1));
imgs.recycle();
}else{
imgs = getResources().obtainTypedArray(R.array.sac_imgs);
mHolder.tx_img.setImageResource(imgs.getResourceId(pos - 1, -1));
imgs.recycle();
}
mHolder.butnxt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
id++;
BlockData();
}
});
return child;
}
protected ViewGroup findViewById(int txtViewPop) {
return null;
}
public class Holder {
TextView txt_id;
Button butnxt;
Button butpre;
TextView txt_fDate;
ImageView tx_img;
}
}
每次点击一个按钮,我该怎么做才能更换视图?我希望有人可以指导我正确的方向。谢谢提前
答案 0 :(得分:1)
每次点击按钮时,如何更换视图?
而不是在ListView中添加适配器的新对象,而不是:
1。在BlockAdapter
中创建方法以清除所有数据:
public void refeshAdapterData(List category_Id,<Data_Type> prgmImages )
{
category_Id.clear();
// clear prgmImages
// add data to adapter
category_Id.addAll(category_Id);
// Add prgmImages
notifyDataSetChanged();
}
2。从refeshAdapterData
方法致电BlockData()
:
refeshAdapterData(category_Id,prgmImages);
stopManagingCursor(mCursor);
mCursor.close();