Gridview custom adapter button onclick - android

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;
}
}

Related

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;
}
}

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.

Click event not working on button in listview item

I am using a custom Listview with Custom views as list elements, each list item is a separate Custom view. The problem which I am facing is , click event on button in 1st item of ListView is not getting fired when we click on it. After clicking on it if we click somewhere else in the screen the click event get fires. I am not able to find the solutions for it and I am struggling with it. Any help will be highly appreciated.
Updated with code: Here is the getview method
public override View GetView(int position, View convertView, ViewGroup parent)
{
if (position == 0)
{
convertView = this.context.LayoutInflater.Inflate(Resource.Layout.home_hero_container, null);
this.heroSection = convertView.FindViewById<FrameLayout>(Resource.Id.heroContainer);
this.setHeroCard();
}
else
{
convertView = (View)GetItem(position - 1);
}
return convertView;
}
GetItem returns CustomView. 1st item will be the hero layout, after that all the Customviews will be added to Convertview. Click event is not working on the 1st item after hero.
Update with my answer:
Instead of assigning Customview directly on to Convertview I inflated a FrameLayout and add Customview to FrameLayout. Now I don't have click issue.
try this it will work
public class ContactsAdapter extends BaseAdapter {
ArrayList<ContactInfo> mlist;
Context mcontext;
public BluetoothChatadpter(Context context,ArrayList<ChatInfo> mchtlist) {
mlist = mchtlist;
mcontext = context;
}
#Override
public int getCount() {
return mlist.size();
}
#Override
public Object getItem(int postion) {
return mlist.get(postion);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertview, ViewGroup viewgroup){
View view = null;
if(convertview == null){
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(R.layout.contactrow, null);
ContactHolder holder = new ContactHolder();
holder.txtviewfirstname = (TextView)view.findViewById(R.id.firstname);
holder.txtviewphone = (TextView)view.findViewById(R.id.phone);
holder.chkselected = (CheckBox)view.findViewById(R.id.check);
setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// to open the selected file in resp
// do your work here
}});
chkselected .setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Toast.makeText(context,// "checked is clicke="+pos, 12).show();
if (chkselected.isChecked())
{
// do your work here
} else {
// do your work here
}
}
});
view.setTag(holder);
}
else{
view = convertview;
}
ContactHolder holder2 = (ContactHolder) view.getTag();
holder2.txtviewfirstname.setText(list.get(position).firstname);
holder2.txtviewphone.setText(list.get(position).phonenumber);
holder2.chkselected.setChecked(list.get(position).selected);
return view;
}
}
public class ContactsAdapter extends BaseAdapter {
ArrayList<ContactInfo> mlist;
Context mcontext;
public BluetoothChatadpter(Context context,ArrayList<ChatInfo> mchtlist) {
mlist = mchtlist;
mcontext = context;
}
#Override
public int getCount() {
return mlist.size();
}
#Override
public Object getItem(int postion) {
return mlist.get(postion);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertview, ViewGroup viewgroup){
View view = null;
if(convertview == null){
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(R.layout.contactrow, null);
ContactHolder holder = new ContactHolder();
holder.txtviewfirstname = (TextView)view.findViewById(R.id.firstname);
holder.txtviewphone = (TextView)view.findViewById(R.id.phone);
holder.chkselected = (CheckBox)view.findViewById(R.id.check);
holder.chkselected.setOnCheckChangeListener(new CheckchangeListener() );
view.setTag(holder);
}
else{
view = convertview;
}
ContactHolder holder2 = (ContactHolder) view.getTag();
holder2.txtviewfirstname.setText(list.get(position).firstname);
holder2.txtviewphone.setText(list.get(position).phonenumber);
holder2.chkselected.setChecked(list.get(position).selected);
return view;
}
class CheckchangeListener implements OnCheckedChangeListener {
public CheckchangeListener() {
// TODO Auto-generated constructor stub
}
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
// do your work here
} else {
// do your work here
}
}
}
}
You can try to set onClick event in your Custom adapter and if you have time then check this tutorial for reference - http://androidforbeginners.blogspot.it/2010/03/clicking-buttons-in-listview-row.html

Button onClick in ListView

Hi i have dynamic ListView with next form:
----------------------------
[TextView][Button]
----------------------------
I want to be able to receive OnClick Button; How can I do it:
Following method doesn't helped:
lv = (ListView)findViewById(R.id.listViewmain);
...
lv.setOnItemClickListener(new ListView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("click on button", "click on button");
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
});
this one doesn't help too (moreover app is crashed):
Button b = (Button)findViewById(R.id.buttonTest);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
How can I do it?
If u have a button inside a listview and you need to capture onclick even on that button. you have to override base adapter.
public class CustomBaseAdapter extends BaseAdapter {
Context context;
List<RowItem> rowItems;
public CustomBaseAdapter(Context context, List<RowItem> items) {
this.context = context;
this.rowItems = items;
}
/*private view holder class*/
private class ViewHolder {
ImageView button;
TextView txtTitle;
TextView txtDesc;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
holder.button= (Button) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
RowItem rowItem = (RowItem) getItem(position);
holder.txtDesc.setText(rowItem.getDesc());
holder.txtTitle.setText(rowItem.getTitle());
holder.imageView.setImageResource(rowItem.getImageId());
return convertView;
}
#Override
public int getCount() {
return rowItems.size();
}
#Override
public Object getItem(int position) {
return rowItems.get(position);
}
#Override
public long getItemId(int position) {
return rowItems.indexOf(getItem(position));
}
// Tty this one
1. Add this class to your adapter.
static class ViewHolder {
TextView textview;
Button button;
}
2. Your getView() look like
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
convertView = LayoutInflater.from(yourActivityName.this).inflate(R.layout.yourListItemXml, null);
holder = new ViewHolder();
holder.textview = (TextView) convertView.findViewById(R.id.textview);
holder.button = (Button) convertView.findViewById(R.id.button);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textview.setText("textview value");
holder.button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// write your start another activity code here
}
});
}
Create a custome adapter as follows in your activity
static class ViewHolder {
public TextView text;
public Button button;
}
private class myAdapter extends BaseAdapter {
LayoutInflater inflater;
public ApproversListAdapter(Context context) {
inflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return myarrayList.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text = (TextView) convertView
.findViewById(R.id.listitem_text);
holder.button = (Button) convertView
.findViewById(R.id.listitem_button);
holder.text.setText("Your String Here");
holder.button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//your code to be executed on button click
}
});
return convertView;
}
}
Now create a an object of myAdapter and use that as your list adapter.
Hope it helped

Optimize listview performance Android

I have a listView (vertical) and every list item has a horizontal list view (horizontal).
But the problem is when i scroll the horizontal scrollview in the row the vertical list is also calling getView()...
So, there is a huge performance hit..
So , can any one tell me a better solution to it ..
public class GridViewAdapter extends BaseAdapter {
List<List<Hotel>> gridDatasource;
Context mContext;
public GridViewAdapter(List<List<Hotel>> gridDatasource, Context context) {
this.gridDatasource = gridDatasource;
this.mContext = context;
}
public void setGridDatasource(List<List<Hotel>> gridDatasource) {
this.gridDatasource = gridDatasource;
}
#Override
public int getCount() {
if (gridDatasource == null) {
return 0;
}
return gridDatasource.size();
}
#Override
public Object getItem(int position) {
return gridDatasource.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
GridViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.gridview_table_cell,
null);
holder = new GridViewHolder();
holder.headerView = (TextView) convertView
.findViewById(R.id.gridViewRowHeader);
holder.listView = (HorizontalListView) convertView
.findViewById(R.id.gridViewHorizontalListView);
convertView.setTag(holder);
} else {
holder = (GridViewHolder) convertView.getTag();
Log.d("TAG", "Reaching Here");
}
holder.headerView.setText("Hello From Sandeep");
HorizontalListViewAdapter adapter = new HorizontalListViewAdapter(
mContext, gridDatasource.get(position));
holder.listView.setAdapter(adapter);
return convertView;
}
}
static class GridViewHolder {
TextView headerView;
HorizontalListView listView;
}
public class HorizontalListViewAdapter extends BaseAdapter {
Context mContext;
List<Hotel> mHotels;
public HorizontalListViewAdapter(Context context, List<Hotel> hotels) {
this.mContext = context;
this.mHotels = hotels;
}
#Override
public int getCount() {
if (mHotels == null) {
return 0;
}
return mHotels.size();
}
#Override
public Object getItem(int position) {
return mHotels.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
HotelCell cell = (HotelCell) convertView;
if (cell == null) {
cell = new HotelCell(mContext);
} else {
Log.d("TAG", "Reached here 2");
}
cell.setHotel(mHotels.get(position));
cell.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(mContext,
HotelDetailActivity.class);
intent.putExtra("DATA", ((HotelCell) v).getHotel());
startActivity(intent);
}
});
cell.setPadding(0, 0, 10, 0);
return cell;
}
}
Dear i suggest to try My this Code
public View getView(final int position, View convertView, ViewGroup parent)
{
View v = convertView;
ViewHolder holder;
if (v == null)
{
v = inflater.inflate(R.layout.custom_image_layout, null);
holder = new ViewHolder();
holder.txtFileName = (TextView) v.findViewById(R.id.txtFileName);
holder.imageView = (ImageView) v.findViewById(R.id.imgView);
v.setTag(holder);
} else
{
holder = (ViewHolder) v.getTag();
}
holder.imageView.setImageBitmap(bm);
holder.txtFileName.setText(""+nameoffile);
return v;
}
static class ViewHolder
{
public ImageView imageView;
public TextView txtFileName;
}
Use The Holder Class

Categories

Resources