when i was trying tutorial checkbox in listview theres some error on my adapter
public View getView(int position, View convertView, ViewGroup parent) {
ViewTag viewTag;
if (convertView == null) {
//取得listItem容器 view
convertView = myInflater.inflate(R.layout.list_item_pegawai, null);
//建構listItem內容view
viewTag = new ViewTag(
(TextView) convertView.findViewById(R.id.txt_namapegawai),
(CheckBox) convertView.findViewById(R.id.checkBox_pegawai));
//設置容器內容
convertView.setTag(viewTag);
} else {
viewTag = (ViewTag) convertView.getTag();
}
complete code
public class CustomAdapter extends BaseAdapter {
private Context context;
public static ArrayList<Model> modelArrayList;
public CustomAdapter(Context context, ArrayList<Model> modelArrayList) {
this.context = context;
CustomAdapter.modelArrayList = modelArrayList;
}
#Override
public int getViewTypeCount() {
return getCount();
}
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public int getCount() {
return modelArrayList.size();
}
#Override
public Object getItem(int position) {
return modelArrayList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item_select, null, true);
holder.checkBox = convertView.findViewById(R.id.checkBox);
holder.tvName = convertView.findViewById(R.id.textView7);
holder.Tvnumber = convertView.findViewById(R.id.textView8);
convertView.setTag(holder);
} else {
// the getTag returns the viewHolder object set as a tag to the view
holder = (ViewHolder) convertView.getTag();
}
holder.tvName.setText(modelArrayList.get(position).getName());
holder.Tvnumber.setText(modelArrayList.get(position).getPhone());
holder.checkBox.setChecked(modelArrayList.get(position).getSelected());
holder.checkBox.setTag(R.integer.btnplusview, convertView);
holder.checkBox.setTag(position);
holder.checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View tempview = (View) holder.checkBox.getTag(R.integer.btnplusview);
TextView tv = tempview.findViewById(R.id.textView7);
Integer pos = (Integer) holder.checkBox.getTag();
if (modelArrayList.get(pos).getSelected()) {
modelArrayList.get(pos).setSelected(false);
} else {
modelArrayList.get(pos).setSelected(true);
}
}
});
return convertView;
}
private class ViewHolder {
protected CheckBox checkBox;
private TextView tvName;
private TextView Tvnumber;
}
}
Related
I want to convert Listview to RecyclerView in my app, I tried to convert code but stucked at getView method, can any one explain how to use this getView code in Recylcer view.
Adapter for ListView CardAdapter.java
public class CardAdapter extends ArrayAdapter<CardDataModel> {
private Context context;
List<CardDataModel> cards;
SharedPreference sharedPreference;
public CardAdapter(Context context, List<CardDataModel> cards) {
super(context, R.layout.card_list_item, cards);
this.context = context;
this.cards = cards;
sharedPreference = new SharedPreference();
}
private class ViewHolder {
TextView CardNameTxt;
TextView CardDescTxt;
TextView CardPriceTxt;
ImageView favoriteImg;
}
#Override
public int getCount() {
return cards.size();
}
#Override
public CardDataModel getItem(int position) {
return cards.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.card_list_item, null);
holder = new ViewHolder();
holder.CardNameTxt = (TextView) convertView
.findViewById(R.id.txt_crd_name);
holder.CardDescTxt = (TextView) convertView
.findViewById(R.id.txt_crd_desc);
holder.CardPriceTxt = (TextView) convertView
.findViewById(R.id.txt_crd_no);
holder.favoriteImg = (ImageView) convertView
.findViewById(R.id.imgbtn_favorite);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
CardDataModel card = (CardDataModel) getItem(position);
holder.CardNameTxt.setText(card.getName());
holder.CardDescTxt.setText(card.getDescription());
holder.CardPriceTxt.setText(card.getPrice() + " ");
/*If a product exists in shared preferences then set heart_red drawable
* and set a tag*/
if (checkFavoriteItem(card)) {
holder.favoriteImg.setImageResource(R.drawable.heart_red);
holder.favoriteImg.setTag("red");
} else {
holder.favoriteImg.setImageResource(R.drawable.heart_grey);
holder.favoriteImg.setTag("grey");
}
return convertView;
}
public boolean checkFavoriteItem(CardDataModel checkCard) {
boolean check = false;
List<CardDataModel> favorites = sharedPreference.getFavorites(context);
if (favorites != null) {
for (CardDataModel card : favorites) {
if (card.equals(checkCard)) {
check = true;
break;
}
}
}
return check;
}
#Override
public void add(CardDataModel card) {
super.add(card);
cards.add(card);
notifyDataSetChanged();
}
#Override
public void remove(CardDataModel card) {
super.remove(card);
cards.remove(card);
notifyDataSetChanged();
}
}
converted somecode like this in RecyclerView Adapter ... CardRvAdapter.java
public class CardRvAdapter extends RecyclerView.Adapter<CardRvAdapter.MyViewHolder> {
private Context context;
List<CardDataModel> cards;
SharedPreference sharedPreference;
public CardRvAdapter(Context context, List<CardDataModel> cards) {
this.context = context;
this.cards = cards;
sharedPreference = new SharedPreference(); }
#Override
public int getItemCount() {
return cards.size();
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
// on create view holder code
View view;
LayoutInflater mInflater = LayoutInflater.from(context);
view = mInflater.inflate(R.layout.card_list_item, viewGroup,false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder myViewHolder, int i) {
//on bind view holder code
myViewHolder.CardPriceTxt.setText(cards.get(i).getPrice());
myViewHolder.CardNameTxt.setText(cards.get(i).getName());
myViewHolder.CardDescTxt.setText(cards.get(i).getDescription());
if (checkFavoriteItem(card)) {
myViewHolder.favoriteImg.setImageResource(R.drawable.heart_red);
myViewHolder.favoriteImg.setTag("red");
} else {
myViewHolder.favoriteImg.setImageResource(R.drawable.heart_grey);
myViewHolder.favoriteImg.setTag("grey");
} }
// checking for favotites in shared preferences
public boolean checkFavoriteItem(CardDataModel checkCard) {
boolean check = false;
List<CardDataModel> favorites = sharedPreference.getFavorites(context);
if (favorites != null) {
for (CardDataModel card : favorites) {
if (card.equals(checkCard)) {
check = true;
break;
}
}
}
return check;
}
#Override
public void add(CardDataModel card) {
super.add(card);
cards.add(card);
notifyDataSetChanged();
}
#Override
public void remove(CardDataModel card) {
super.remove(card);
cards.remove(card);
notifyDataSetChanged();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView CardNameTxt;
TextView CardDescTxt;
TextView CardPriceTxt;
ImageView favoriteImg;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
this.CardDescTxt = (TextView) itemView.findViewById(R.id.txt_crd_desc);
this.CardNameTxt = (TextView) itemView.findViewById(R.id.txt_crd_name);
this.CardPriceTxt = (TextView) itemView.findViewById(R.id.txt_crd_no);
this.favoriteImg = (ImageView) itemView.findViewById(R.id.imgbtn_favorite);
}}}
The problem part..
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.card_list_item, null);
holder = new ViewHolder();
holder.CardNameTxt = (TextView) convertView
.findViewById(R.id.txt_crd_name);
holder.CardDescTxt = (TextView) convertView
.findViewById(R.id.txt_crd_desc);
holder.CardPriceTxt = (TextView) convertView
.findViewById(R.id.txt_crd_no);
holder.favoriteImg = (ImageView) convertView
.findViewById(R.id.imgbtn_favorite);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
CardDataModel card = (CardDataModel) getItem(position);
holder.CardNameTxt.setText(card.getName());
holder.CardDescTxt.setText(card.getDescription());
holder.CardPriceTxt.setText(card.getPrice() + " ");
/*If a product exists in shared preferences then set heart_red drawable
* and set a tag*/
if (checkFavoriteItem(card)) {
holder.favoriteImg.setImageResource(R.drawable.heart_red);
holder.favoriteImg.setTag("red");
} else {
holder.favoriteImg.setImageResource(R.drawable.heart_grey);
holder.favoriteImg.setTag("grey");
}
return convertView;
}
How to modify this code and where to add this checkFavItem, add , and remove methods..suggest me , i am new to android development.
Finishing RecyclerView Code
// specify the row layout file and click for each row
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(listItemLayout, parent, false);
ViewHolder myViewHolder = new ViewHolder(view);
return myViewHolder;
}
// load data in each row element
#Override
public void onBindViewHolder(final ViewHolder holder, final int listPosition) {
TextView item = holder.item;
item.setText(itemList.get(listPosition).getName());
}
Senario: To get rating feedback for multiple multiple products.
Problem: Ratingbar value resets on scrolling listview by calling setOnRatingBarChangeListener
Expected output:
Customized listview adpater code:
public class FeedbackAdapter extends BaseAdapter {
static Context context;
List<FeedbackRowDetails> row;
ViewHolder holder;
public FeedbackAdapter(Context context,List<FeedbackRowDetails> row)
{
this.context = context;
this.row =row;
}
private class ViewHolder {
TextView tv_slno, tv_productname, tv_time;
RatingBar rb_productrating;
}
#Override
public View getView( final int position, View convertView, ViewGroup parent) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.custom_feedback_row_format,null);
holder = new ViewHolder();
holder.tv_slno = (TextView) convertView.findViewById(R.id.tv_slno);
holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
holder.tv_productname = (TextView) convertView.findViewById(R.id.tv_productname);
holder.rb_productrating=(RatingBar) convertView.findViewById(R.id.rb_productrating);
convertView.setTag(holder);
} else
{
holder = (ViewHolder) convertView.getTag();
}
FeedbackRowDetails row_pos = row.get(position);
holder.tv_slno.setText(String.valueOf(position + 1));
holder.tv_time.setText(row_pos.gettime());
holder.tv_productname.setText(row_pos.getproductsName());
holder.rb_productrating.setRating(Float.valueOf(row_pos.getrating()));
holder.rb_productrating.setOnRatingBarChangeListener(new OnRatingBarChangeListener()
{
public void onRatingChanged(RatingBar ratingBar, float rating,boolean fromUser)
{
FeedbackRowDetails row_pos = row.get(position);
int roundoff_rating = (int)Math.round(rating);
ratingBar.setRating(roundoff_rating);
row_pos.setrating(String.valueOf(roundoff_rating));
}
});
return convertView;
}
#Override
public int getCount() {
return row.size();
}
#Override
public Object getItem(int position) {
return row.get(position);
}
#Override
public long getItemId(int position) {
return row.indexOf(getItem(position));
}
}
Just wrap your codes present inside your onRatingChanged method with a simple if as shown below
if(fromUser) {
FeedbackRowDetails row_pos = row.get(position);
int roundoff_rating = (int) Math.round(rating);
ratingBar.setRating(roundoff_rating);
row_pos.setRating(String.valueOf(roundoff_rating));
}
Have tested it with emulator and it was working as expected. Hope this solves your problem
This problem solved with reference to link
public class FeedbackAdapter extends BaseAdapter {
static Context context;
List<FeedbackRowDetails> row;
ViewHolder holder;
public FeedbackAdapter(Context context,List<FeedbackRowDetails> row)
{
this.context = context;
this.row =row;
}
private class ViewHolder {
TextView tv_slno, tv_productname, tv_time;
RatingBar rb_productrating;
}
#Override
public View getView( final int position, View convertView, ViewGroup parent) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.custom_feedback_row_format,parent, false);
holder = new ViewHolder();
holder.tv_slno = (TextView) convertView.findViewById(R.id.tv_slno);
holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
holder.tv_productname = (TextView) convertView.findViewById(R.id.tv_productname);
holder.rb_productrating=(RatingBar) convertView.findViewById(R.id.rb_productrating);
convertView.setTag(holder);
} else
{
holder = (ViewHolder) convertView.getTag();
}
holder.rb_productrating.setOnRatingBarChangeListener(onRatingChangedListener(holder, position));
holder.rb_productrating.setTag(position);
holder.tv_slno.setText(String.valueOf(position + 1));
holder.tv_time.setText(getItem(position).gettime());
holder.tv_productname.setText(getItem(position).getproductsName());
float rating=Float.valueOf(getItem(position).getrating());
holder.rb_productrating.setRating(rating);
return convertView;
}
private RatingBar.OnRatingBarChangeListener onRatingChangedListener(final ViewHolder holder, final int position)
{
return new RatingBar.OnRatingBarChangeListener() {
#Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean b)
{
FeedbackRowDetails item = row.get(position);
int roundoff_rating = (int)Math.round(rating);
ratingBar.setRating(roundoff_rating);
item.setrating(String.valueOf(roundoff_rating));
}
};
}
#Override
public int getCount() {
return row.size();
}
#Override
public FeedbackRowDetails getItem(int position)
{
return row.get(position);
}
#Override
public long getItemId(int position) {
return row.indexOf(getItem(position));
}
}
Well i have a listview populated with BaseAdapter. In listview i have 2 textviews and 2 buttons that i want to be clickable and i did set it class and works fine but problem is when i click one textview, 4th, 8th list item below is clicked too and i just find out that they have same position by printing position with toast.
How can i make that positions go ++ and every of them be uniqe?
Adapter Class
public class CustomListAdapter extends BaseAdapter
{
private ArrayList<FeedItem> listData;
private LayoutInflater layoutInflater;
private Context mContext;
protected ListView feedListView;
public CustomListAdapter(Context context, ArrayList<FeedItem> listData)
{
this.listData = listData;
layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mContext = context;
}
public void addItem(final FeedItem item) {
listData.add(item);
notifyDataSetChanged();
}
#Override
public int getCount()
{
return listData.size();
}
#Override
public Object getItem(int position)
{
return listData.get(position);
}
#Override
public long getItemId(int position)
{
return position;
}
public View getView( final int position, View convertView, ViewGroup parent)
{
final ViewHolder holder;
View row=convertView;
if (row == null)
{
convertView = layoutInflater.inflate(R.layout.list_row_layout, null);
holder = new ViewHolder();
holder.headlineView = (TextView)convertView.findViewById(R.id.name);
holder.reportedDateView = (TextView) convertView.findViewById(R.id.confid);
holder.approve = (TextView) convertView.findViewById(R.id.approveTV);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
FeedItem newsItem = (FeedItem) listData.get(position);
holder.approve.setFocusable(true);
holder.approve.setClickable(true);
holder.headlineView.setText(Html.fromHtml(newsItem.getTitle()));
holder.reportedDateView.setText(Html.fromHtml(newsItem.getContent()));
holder.approve.setTag(position);
holder.approve.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
Toast.makeText(mContext, String.valueOf(v.getTag()), Toast.LENGTH_LONG).show();
holder.approve.setTag(v);
holder.approve.setText("Approved");
}
});
return convertView;
}
static class ViewHolder
{
TextView approve;
TextView headlineView;
TextView reportedDateView;
ImageView imageView;
FeedItem newsItem;
}
}
Listview recycles views.
How ListView's recycling mechanism works
Change getView
public View getView( final int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if (convertView == null)
{
convertView = layoutInflater.inflate(R.layout.list_row_layout, null);
holder = new ViewHolder();
holder.headlineView = (TextView)convertView.findViewById(R.id.name);
holder.reportedDateView = (TextView) convertView.findViewById(R.id.confid);
holder.approve = (TextView) convertView.findViewById(R.id.approveTV);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
FeedItem newsItem = (FeedItem) listData.get(position);
holder.approve.setFocusable(true);
holder.approve.setClickable(true);
holder.headlineView.setText(Html.fromHtml(newsItem.getTitle()));
holder.reportedDateView.setText(Html.fromHtml(newsItem.getContent()));
holder.approve.setTag(position);
holder.approve.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
Toast.makeText(mContext, String.valueOf(v.getTag()), Toast.LENGTH_LONG).show();
}
});
return convertView;
}
As you are reusing the view by
else{
holder = (ViewHolder) convertView.getTag();
}
so multiple textView using same listener. So Take your onclickListener outside of the if condition.
if (convertView == null)
{
convertView = layoutInflater.inflate(R.layout.list_row_layout, null);
holder = new ViewHolder();
holder.headlineView = (TextView)convertView.findViewById(R.id.name);
holder.reportedDateView = (TextView) convertView.findViewById(R.id.confid);
holder.approve = (TextView) convertView.findViewById(R.id.approveTV);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.approve.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
Toast.makeText(mContext, String.valueOf(position), Toast.LENGTH_LONG).show();
}
});
Add this code in your list adapter :
#Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return questionsClassArrayList_.size();
}
#Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
return position;
}
Hi i am using checkbox in listview, but the checkbox get auto unchecked or checked when i scroll my list view.
public class MyContactsAdapter extends BaseAdapter {
private LayoutInflater inflater;
private List<Friend> mAllFriends;
public MyContactsAdapter(Context context,List<Friend> allFriends) {
inflater = LayoutInflater.from(context);
this.mAllFriends =allFriends;
}
#Override
public int getCount() {
if(mAllFriends==null)
{
return 0;
}else{
return mAllFriends.size();
}
}
#Override
public Object getItem(int position) {
return mAllFriends.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder mHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.contact_list_item, null);
mHolder = new ViewHolder();
mHolder.mProFileImage = (ImageView) convertView
.findViewById(R.id.profleImage);
mHolder.mPersonName = (TextView) convertView
.findViewById(R.id.person_Name);
mHolder.mEmailId = (TextView) convertView
.findViewById(R.id.person_emailId);
mHolder.mSelectPerson = (CheckBox)convertView.findViewById(R.id.personSelect);
mHolder.mSelectAllLabel = (TextView)convertView.findViewById(R.id.personSelectAllLabel);
convertView.setTag(mHolder);
} else {
mHolder = (ViewHolder) convertView.getTag();
}
if(position==0)
{
mHolder.mProFileImage.setVisibility(View.GONE);
mHolder.mPersonName.setVisibility(View.GONE);
mHolder.mEmailId.setVisibility(View.GONE);
mHolder.mSelectAllLabel.setVisibility(View.VISIBLE);
}else{
mHolder.mProFileImage.setVisibility(View.VISIBLE);
mHolder.mPersonName.setVisibility(View.VISIBLE);
mHolder.mEmailId.setVisibility(View.VISIBLE);
mHolder.mSelectAllLabel.setVisibility(View.GONE);
}
mHolder.mProFileImage.setImageResource(R.drawable.ic_launcher);
mHolder.mPersonName.setText(""+mAllFriends.get(position).getName());
mHolder.mEmailId.setText(""+mAllFriends.get(position).getEmail());
mHolder.mSelectPerson.setTag(position);
mHolder.mSelectPerson.setChecked(mAllFriends.get(position).isSelected());
mHolder.mSelectPerson.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mAllFriends.get((Integer) buttonView.getTag()).setSelected(isChecked);
}
});
return convertView;
}
private static class ViewHolder {
ImageView mProFileImage;
TextView mPersonName;
TextView mEmailId;
CheckBox mSelectPerson;
TextView mSelectAllLabel;
}
}
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