ListView item showing wrong while scrolling android? - android

I am trying to show some words in the ListView.But the problem is when i start to scroll ListView is not populating the next words.It populates randomly the same duplicated words.
DictListAdapter:
public class DictListViewAdapter extends BaseSwipeAdapter {
private Context mContext;
private ArrayList<String> searchResult;
private String word;
private LayoutInflater inflater;
TextView wrd;
public DictListViewAdapter(Context mContext, ArrayList<String>searchResult) {
this.mContext = mContext;
this.searchResult = searchResult;
}
#Override
public int getSwipeLayoutResourceId(int position) {
return R.id.swipe;
}
#Override
public View generateView(final int position, ViewGroup parent,View v) {
if(inflater==null){
inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
if(v==null) {
v = inflater.inflate(R.layout.listview_item,null);
}
wrd = (TextView) v.findViewById(R.id.word);
final TextView meaning=(TextView)v.findViewById(R.id.meaning);
SwipeLayout swipeLayout = (SwipeLayout)v.findViewById(getSwipeLayoutResourceId(position));
swipeLayout.addSwipeListener(new SimpleSwipeListener() {
#Override
public void onOpen(SwipeLayout layout) {
// YoYo.with(Techniques.Tada).duration(500).delay(100).playOn(layout.findViewById(R.id.trash));
//Toast.makeText(mContext, DatabaseHelper.getSingleMeaning(searchResult.get(position), mContext), Toast.LENGTH_SHORT).show();
String mean=DatabaseHelper.getSingleMeaning(searchResult.get(position),mContext);
meaning.setText(mean);
}
});
swipeLayout.setOnDoubleClickListener(new SwipeLayout.DoubleClickListener() {
#Override
public void onDoubleClick(SwipeLayout layout, boolean surface) {
Toast.makeText(mContext, "DoubleClick", Toast.LENGTH_SHORT).show();
}
});
/*v.findViewById(R.id.delete).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(mContext, "click delete", Toast.LENGTH_SHORT).show();
}
});
*/
word = searchResult.get(position);
wrd.setText(word);
return v;
}
This class extends BaseSwipeAdapter and it has the getView() method like below where it has the implementation of getView():
#Override
public final View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null){
v = generateView(position, null,convertView);
}
mItemManger.bind(v, position);
fillValues(position, v);
return v;
}
I know that "Every time ListView needs to show a new row on screen, it will call the getView() method from its adapter." That's why i added if(v==null) but not working.But i have done this tweak in my other projects.It worked fine then but in this project it's not working.Any Solution??Thanks in advance.
EDIT: I have tried using ViewHolder but not working.Did i miss anything??
static class ViewHolder {
TextView wrd;
int position;
}
public DictListViewAdapter(Context mContext, ArrayList<String>searchResult) {
this.mContext = mContext;
this.searchResult = searchResult;
}
#Override
public int getSwipeLayoutResourceId(int position) {
return R.id.swipe;
}
#Override
public View generateView(final int position, ViewGroup parent,View v) {
ViewHolder viewHolder;
if(inflater==null){
inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
if(v==null) {
v = inflater.inflate(R.layout.listview_item,null);
viewHolder = new ViewHolder();
viewHolder.wrd=(TextView) v.findViewById(R.id.word);
viewHolder.position=position;
v.setTag(viewHolder);
}else{
viewHolder=(ViewHolder)v.getTag();
}
viewHolder.wrd.setText(searchResult.get(position));
final TextView meaning=(TextView)v.findViewById(R.id.meaning);
SwipeLayout swipeLayout = (SwipeLayout)v.findViewById(getSwipeLayoutResourceId(position));
swipeLayout.addSwipeListener(new SimpleSwipeListener() {
#Override
public void onOpen(SwipeLayout layout) {
// YoYo.with(Techniques.Tada).duration(500).delay(100).playOn(layout.findViewById(R.id.trash));
//Toast.makeText(mContext, DatabaseHelper.getSingleMeaning(searchResult.get(position), mContext), Toast.LENGTH_SHORT).show();
String mean=DatabaseHelper.getSingleMeaning(searchResult.get(position),mContext);
meaning.setText(mean);
}
});
swipeLayout.setOnDoubleClickListener(new SwipeLayout.DoubleClickListener() {
#Override
public void onDoubleClick(SwipeLayout layout, boolean surface) {
Toast.makeText(mContext, "DoubleClick", Toast.LENGTH_SHORT).show();
}
});
/*v.findViewById(R.id.delete).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(mContext, "click delete", Toast.LENGTH_SHORT).show();
}
});
*/
return v;
}

if you have to use ViewHolder pattern and check the null view inside the generateView methos then you will have to change the code inside BaseSwipeAdapter
#Override
public final View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
// if(v == null){
v = generateView(position, v, parent);
// }
mItemManger.bind(v, position);
fillValues(position, v);
return v;
}
comment this null check and try now.

Related

Android custom listadapter always deleting the last item

Good day,
I am trying to refresh my knowledge in android programming
by creating a simple memo app with custom adapter list.
My problem is, even I call "NotifyDataSetChanged();",
the value deleted in my list is always the last one.
Though when I refresh it, it will display the correct one.
Here is my code :
public class CustomAdapter extends BaseAdapter{
List<Memo> memos;
Context context;
int [] imageId;
private static LayoutInflater inflater = null;
public CustomAdapter(MainActivity mainActivity, List<Memo> _memos) {
memos = _memos;
context = mainActivity;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return memos.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
public class Holder
{
TextView tv;
TextView tv2;
Button deleteMemoBtn;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
Holder mViewHolder = null;
if (convertView == null) {
mViewHolder = new Holder();
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.list_memo, parent, false);
mViewHolder.tv = (TextView) convertView.findViewById(R.id.textView1);
mViewHolder.tv2 = (TextView) convertView.findViewById(R.id.textView2);
mViewHolder.deleteMemoBtn = (Button) convertView.findViewById(R.id.deleteMemoBtn);
mViewHolder.tv.setText(memos.get(position).getMemoTitle());
mViewHolder.tv2.setText(Integer.toString(memos.get(position).getMemoID()));
mViewHolder.tv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "You Clicked " + memos.get(position).getMemoTitle(), Toast.LENGTH_LONG).show();
}
});
mViewHolder.deleteMemoBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "deleted " + Integer.toString(position), Toast.LENGTH_LONG).show();
memos.remove(position);
notifyDataSetChanged();
}
});
}
return convertView;
}
}
Hoping someone can lighten me up why this code is not working.
Thank you!
Try this code.
public class CustomAdapter extends BaseAdapter {
List<Memo> memos;
Context context;
int[] imageId;
private LayoutInflater inflater = null;
public CustomAdapter(Context mainActivity, List<Memo> _memos) {
memos = _memos;
context = mainActivity;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return memos.size();
}
#Override
public Object getItem(int position) {
return memos.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public class Holder {
TextView tv;
TextView tv2;
Button deleteMemoBtn;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
Holder mViewHolder = null;
if (convertView == null) {
mViewHolder = new Holder();
convertView = inflater.inflate(R.layout.list_memo, parent, false);
mViewHolder.tv = (TextView) convertView.findViewById(R.id.textView1);
mViewHolder.tv2 = (TextView) convertView.findViewById(R.id.textView2);
mViewHolder.deleteMemoBtn = (Button) convertView.findViewById(R.id.deleteMemoBtn);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (Holder) convertView.getTag();
}
mViewHolder.tv.setText(memos.get(position).getMemoTitle());
mViewHolder.tv2.setText(Integer.toString(memos.get(position).getMemoID()));
mViewHolder.tv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "You Clicked " + memos.get(position).getMemoTitle(), Toast.LENGTH_LONG).show();
}
});
mViewHolder.deleteMemoBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "deleted " + Integer.toString(position), Toast.LENGTH_LONG).show();
memos.remove(position);
notifyDataSetChanged();
}
});
return convertView;
}
}

CustomAdapter - CheckedTextView

I have a problem with "remembering" states of Checkboxes in CheckTextView.
Now when I click, it changes the state but on different CheckBox.
class CustomAdapterSmartwatch extends ArrayAdapter<String> {
static class ViewHolder {
protected CheckedTextView cTextView;
}
ViewHolder holder;
public CustomAdapterSmartwatch(Context context, ArrayList<String> variables) {
super(context,R.layout.row_smartwatch_resources,variables);
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater smartwatchinflater = LayoutInflater.from(getContext());
convertView = smartwatchinflater.inflate(R.layout.row_smartwatch_resources,parent,false);
holder.cTextView = (CheckedTextView) convertView.findViewById(R.id.row_smartwatch_checked);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.cTextView.setText(getItem(position));
holder.cTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (holder.cTextView.isChecked()) {
holder.cTextView.setChecked(false);
} else {
holder.cTextView.setChecked(true);
}
}
});
return convertView;
}
}
Now i'm trying to figure out with using Holder but i heard that it's possible to use SparseBooleanArray. Can someone explain how to make it works?
EDIT 1
Ok, I have ListView which is controlled by CustomAdapter. That adapter contains only 1 item (CheckedTextView).
The problem is that after scrolling these checkedboxes don't remember their state.
I saw your answer with explanation but now after clicking any "CheckedTextView", the state of their checkbox doesn't change.
EDIT 2
#NonNull
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater smartwatchinflater = LayoutInflater.from(getContext());
convertView = smartwatchinflater.inflate(R.layout.row_smartwatch_resources,parent,false);
holder.cTextView = (CheckedTextView) convertView.findViewById(R.id.row_smartwatch_checked);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.cTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("MainActivity","Position = " + position);
CheckedTextView checkedText = (CheckedTextView) v;
checkedState.put(position, !checkedText.isChecked());
Log.d("MainActivity","State = " + checkedState.get(position));
}
});
holder.cTextView.setText(getItem(position));
holder.cTextView.setChecked(checkedState.get(position));
return convertView;
}
EDIT 3
class CustomAdapterSmartwatch extends ArrayAdapter<String> {
public SparseBooleanArray checkedState = new SparseBooleanArray();
private CheckedTextView cTextView;
public CustomAdapterSmartwatch(Context context, ArrayList<String> variables) {
super(context,R.layout.row_smartwatch_resources,variables);
}
#NonNull
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater smartwatchinflater = LayoutInflater.from(getContext());
convertView = smartwatchinflater.inflate(R.layout.row_smartwatch_resources,parent,false);
cTextView = (CheckedTextView) convertView.findViewById(R.id.row_smartwatch_checked);
cTextView.setText(getItem(position));
cTextView.setChecked(checkedState.get(position));
cTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("MainActivity","Position = " + position);
CheckedTextView checkedText = (CheckedTextView) v;
checkedText.toggle();
if (checkedText.isChecked())
checkedState.put(position,true);
else
checkedState.delete(position);
Log.d("MainActivity","State = " + checkedState.get(position));
}
});
return convertView;
}
}
You have to keep a boolean flag for each CheckTextView. This can be an array, List, or SparseBooleanArray. The data structure you choose should be a field of your adapter:
class CustomAdapterSmartwatch extends ArrayAdapter<String> {
private SparseBooleanArray checkedState = new SparseBooleanArray();
// ...
}
You do not want to put it in the holder because the holder is retired only fr visible rows. On the other hand, you want to maintain the checked state for every row, visible or not.
Now, you set the stored checked state when the user clicks on a CheckedTextView:
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// ...
holder.cTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CheckedTextView checkedText = (CheckedTextView)v;
checkedState.put(position, checkedText.isChecked());
}
});
}
And you restore the checked state whenever you inflate a view or reuse one from a holder:
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// ...
holder.cTextView.setText(getItem(position));
holder.cTextView.setChecked(checkedState.get(position));
// ...
}
You can simply use the view in onClick() method itself.
holder.cTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(v instanceOf CheckedTextView) {
CheckedTextView c = (CheckedTextView) v;
c.setChecked(!c.isChecked());
}
}
});
Replace your code with following update:
class CustomAdapterSmartwatch extends ArrayAdapter<CustomAdapterSmartwatch.MyPojo> {
class MyPojo {
boolean checked;
String data;
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
static class ViewHolder {
protected CheckedTextView cTextView;
}
ViewHolder holder;
public CustomAdapterSmartwatch(Context context, ArrayList<MyPojo> variables) {
super(context, R.layout.row_smartwatch_resources, variables);
}
#Nullable
#Override
public MyPojo getItem(int position) {
return super.getItem(position);
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater smartwatchinflater = LayoutInflater.from(getContext());
convertView = smartwatchinflater.inflate(R.layout.row_smartwatch_resources,parent,false);
holder.cTextView = (CheckedTextView) convertView.findViewById(R.id.row_smartwatch_checked);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final MyPojo item = getItem(position);
holder.cTextView.setText(item.getData());
holder.cTextView.setChecked(item.isChecked());
holder.cTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean nextStatus = !item.isChecked();
item.setChecked(nextStatus);
holder.cTextView.setChecked(nextStatus);
}
});
return convertView;
}
}

Butterknife not working with ViewFlipper

This is a bit strange. Butterknife seems to be working with all other Views except my ViewFlipper. My ViewFlipper is on a ListView item. When you tap the ListView item, it flips over to display additional options. This doesnt happen anymore when I use #BindView to initialize my ViewFlipper, and the app doesnt crash, so no relevant logs. Code:
public class DepartmentListAdapter extends BaseAdapter {
Context context;
String[] departments;
#BindView(R.id.view_flipper_2) ViewFlipper flipper;
#BindView(R.id.departmentName) TextView departmentName;
#BindView(R.id.departmentOverviewButton) Button overViewButton;
#BindView(R.id.organizationalStructureButton) Button structureButton;
public DepartmentListAdapter(Context context, String[] departments){
this.context = context;
this.departments = departments;
}
#Override
public int getCount() {
return departments.length;
}
#Override
public Object getItem(int position) {
return departments[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.department_list_item, parent, false);
}else{
view = convertView;
}
ButterKnife.bind(this, view);
//this gets set
departmentName.setText(departments[position]);
flipper.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//this effect is gone, not sure onClick is even being called
AnimationFactory.flipTransition(flipper, AnimationFactory.FlipDirection.TOP_BOTTOM, 90);
}
});
overViewButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "DO", Toast.LENGTH_SHORT).show();
}
});
structureButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "DS", Toast.LENGTH_SHORT).show();
}
});
Animation animation = AnimationUtils.loadAnimation(
context, R.anim.listview_slide);
view.startAnimation(animation);
return view;
}
}

Textview duplication in custome listview

I have a custom list view with base adapter.In the listview each item contain button and a textview,I want to visible the textview when clicking on the corresponding button.It is working bt my problem is that when i click on the button the textview is visible in more than one item.Please suggest any solution.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
// convertView = new View(context);
convertView = inflater.inflate(R.layout.secondadapter, null);
}
holder.ans=(Button)convertView.findViewById(R.id.btn_ans);
holder.textview=(TextView)convertView.findViewById(R.id.txt_ans);
holder.ans.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
holder.textview.setText("Answer is correct");
}
});
}
public class Holder
{
TextView textview;
Button ans;
}
Try this code.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
// convertView = new View(context);
convertView = inflater.inflate(R.layout.secondadapter, null);
holder.ans=(Button)convertView.findViewById(R.id.btn_ans);
holder.textview=(TextView)convertView.findViewById(R.id.txt_ans);
convertView.setTag(holder);
}else{
holder=(holder)convertView.getTag();
}
if(!isTextViewVisible[position]) { holder.textview.setVisibility(View.Invisible); }
else {
holder.textview.setVisibility(View.visible);
}
holder.ans.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
holder.textview.setText("Answer is correct"); isTextViewVisible[position] = true;
} });
}
public class Holder
{
TextView textview;
Button ans;
}
You are didn't follow ViewHolder Pattern.You have to use convertView.setTag(holder) and holder=(holder)convertView.getTag()
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
// convertView = new View(context);
convertView = inflater.inflate(R.layout.secondadapter, null);
holder.ans=(Button)convertView.findViewById(R.id.btn_ans);
holder.textview=(TextView)convertView.findViewById(R.id.txt_ans);
convertView.setTag(holder);
}else{
holder=(holder)convertView.getTag();
}
holder.ans.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
holder.textview.setText("Answer is correct");
}
});
}
public class Holder
{
TextView textview;
Button ans;
}
Please have a read to understand , how listview works.
public class SampleAdapter extends BaseAdapter {
public SampleAdapter (Activity callingActivity,
ArrayList datas, Resources imagesEtc) {
this.callingActivity = callingActivity;
this.datas = datas;
this.imagesEtc = imagesEtc;
}
#Override
public void notifyDataSetChanged() {
// Refresh List rows
super.notifyDataSetChanged();
}
#Override
public int getCount() {
if (datas.size() <= 0)
return 1;
return datas.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
public static class ItemsInEachRow {
TextView sample1, sample2, sample3;
}
#Override
public View getView(final int position, View eachRowsView,
ViewGroup parentView) {
// To Inflate Single Item of ListView
View eachRow = eachRowsView;
ItemsInEachRow itemsInEachRow;
if (eachRowsView == null) {
itemsInEachRow = new ItemsInEachRow();
eachRow = callingActivity.getLayoutInflater().inflate(
R.layout.your_layout, null);
itemsInEachRow.sample1= (TextView) eachRow
.findViewById(R.id.sample1);
eachRow.setTag(itemsInEachRow);
} else {
itemsInEachRow = (ItemsInEachRow) eachRow.getTag();
}
itemsInEachRow.sample1
.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
}
});
return eachRow;
}
}
Please see below code hope it will work for you
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
{
convertView = inflater.inflate(R.layout.secondadapter, null);
holder.ans=(Button)convertView.findViewById(R.id.btn_ans);
holder.textview=(TextView)convertView.findViewById(R.id.txt_ans);
convertView.setTag(holder);
}
else
{
m_viewHolder = (ViewHolder) convertView.getTag();
}
holder.ans.setTag(position);
holder.ans.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
int m_position = Integer.parseInt(p_view.getTag().toString());
holder.textview.setText("Answer is correct");
}
});
}
public class Holder
{
TextView textview;
Button ans;
}
If textview is sibling of button. Using its parent get the corresponding textview. here is the code.
holder.ans.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
View vi = (View) v.getParent();
TextView texview = (TextView) vi.findViewById(R.id.txt_ans);
textview.setText("Answer is correct");
textview.setVisiblity(View.VISIBLE);
}
});
There are more than one view getting visible is because ListView reuse your view if it is no longer visible on the screen. To tell ListView to don't do this for a view: itemView.setHasTransientState(true)
When you are done with that view (e.g the item has been removed) you can call the method again and set it to false.
To Learn more. https://www.youtube.com/watch?v=8MIfSxgsHIs
P/s: It seems like you do not follow the viewHolder Pattern as #kishore-jethava said.

Gridview custom adapter button onclick

My gridview custom item is a image button.I want to change image button image when the image button is clicked.You can see below my custom adapter class getView method.But image button image does not change.
ImageButton btn1;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.myItem, null);
btn1 = (ImageButton) view.findViewById(R.id.btn1);
}
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
btn1.setImageResource(R.id.image1);
}
});
return view;
}
Try this way,hope this will help you to solve your problem.
public class CustomGridAdapter extends BaseAdapter {
private Context context;
private ArrayList<HashMap<String,Integer>> imagesList;
public CustomGridAdapter(Context context,ArrayList<HashMap<String,Integer>> imagesLis) {
this.context =context;
this.imagesList =imagesLis;
}
#Override
public int getCount() {
return imagesList.size();
}
#Override
public Object getItem(int position) {
return imagesList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.myItem, null);
holder.btn1 = (TextView) convertView.findViewById(R.id.btn1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.btn1.setImageResource(imagesList.get(position).get("normalImage"));
holder.btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((ImageButton)v).setImageResource(imagesList.get(position).get("selectedImage"));
}
});
return convertView;
}
static class ViewHolder {
ImageButton btn1;
}
}

Categories

Resources