I am showing an image and a text in a listView
using custom Adapter
.On clicking a button i want to replace the image with next image.But it is just getting added at the end of the list instead of replacing.Below is my code snippet:
Method to fetch image from 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();
}
}
setting in custom view
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;
}
}
What should I do to replace the view every time a button is clicked?I hope that some one can guide me in right direction.thanks in advance
What should i do to replace the view every time a button is clicked?
Instead of adding new object of Adapter in ListView do it as:
1. Create a method in BlockAdapter
to clear all data:
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. Call refeshAdapterData
from BlockData()
method :
refeshAdapterData(category_Id,prgmImages);
stopManagingCursor(mCursor);
mCursor.close();