Null pointer Exception on Adapter because of View Holder - android

here is my Adapter:
public class MainAdapter extends BaseAdapter implements View.OnClickListener {
private Context context;
private LayoutInflater inflater;
private Map<Integer, Info[]> originalDatas;
public MainAdapter(Context context, Map<Integer, Info[]> infoList) {
this.context = context;
originalDatas = infoList;
inflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return originalDatas.size();
}
#Override
public Object getItem(int position) {
return originalDatas.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = inflater.inflate(R.layout.grid_list_row, null);
final ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
viewHolder.rightText = (TextView) convertView.findViewById(R.id.rightText);
viewHolder.leftText = (TextView) convertView.findViewById(R.id.leftText);
viewHolder.centerText = (TextView) convertView.findViewById(R.id.centerText);
viewHolder.rightText.setTypeface(TypeFaces.getTypeFace(context, TypeFaces.FONT_REGULAR));
viewHolder.leftText.setTypeface(TypeFaces.getTypeFace(context, TypeFaces.FONT_REGULAR));
viewHolder.centerText.setTypeface(TypeFaces.getTypeFace(context, TypeFaces.FONT_REGULAR));
viewHolder.rightText.setOnClickListener(this);
viewHolder.centerText.setOnClickListener(this);
viewHolder.leftText.setOnClickListener(this);
convertView.setTag(viewHolder);
} else viewHolder = (ViewHolder) convertView.getTag();
Iterator<Integer> iterator = originalDatas.keySet().iterator();
while (iterator.hasNext()) {
Info[] infos = originalDatas.get(iterator.next());
viewHolder.rightText.setText(infos[0].getTitle());
viewHolder.centerText.setText(infos[1].getTitle());
if (infos[2] != null)
viewHolder.leftText.setText(infos[2].getTitle());
else
viewHolder.leftText.setVisibility(View.INVISIBLE);
}
return convertView;
}
#Override
public void onClick(View v) {
String titleText = ((TextView) v).getText().toString();
if (titleText != "") {
Intent intent = new Intent(context, SecondActivity.class);
intent.putExtra("parent", titleText);
Toast.makeText(context, titleText, Toast.LENGTH_SHORT).show();
context.startActivity(intent);
}
}
static class ViewHolder {
TextView rightText;
TextView centerText;
TextView leftText;
}
}
it gives me a nullpioterexception on
viewHolder.rightText.setText(infos[0].getTitle());
why is the problem?
i also used this :
while (iterator.hasNext() && viewHolder!=null) {
Info[] infos = originalDatas.get(iterator.next());
viewHolder.rightText.setText(infos[0].getTitle());
viewHolder.centerText.setText(infos[1].getTitle());
if (infos[2] != null)
viewHolder.leftText.setText(infos[2].getTitle());
else
viewHolder.leftText.setVisibility(View.INVISIBLE);
}
but all rows fills by first row data , and when i select textviews it does not work

convertView = inflater.inflate(R.layout.grid_list_row, null);
final ViewHolder viewHolder;
if (convertView == null) {
convertView is never null in your code, and ViewHolder is never initialized. Change it like
final ViewHolder viewHolder
if (convertView == null) {
convertView = inflater.inflate(R.layout.grid_list_row, null);

Related

ListView not follow order to display after notifyDataSetChanged

I dont know why my output of listview always in wrong order after I update the data in my activity.
This is how I initialize the adapter in my activity
inboxTradeAdapter=new InboxTradeAdapter(InboxTradeActivity.this, R.layout.activity_inbox_trade, tradeArrayList,loginUserId);
tradeList.setAdapter(inboxTradeAdapter);
I call
inboxTradeAdapter.notifyDataSetChanged();
after tradeArrayList is updated
This is my Adapter
public class InboxTradeAdapter extends ArrayAdapter<Trade> {
private List<Trade> tradeList;
private Context context;
private String loginUserId;
public InboxTradeAdapter(Context context, int resource, List<Trade> tradeList, String loginUserId) {
super(context, resource, tradeList);
this.context = context;
this.tradeList = tradeList;
this.loginUserId=loginUserId;
}
#Override
public boolean isEnabled(int position) {
return true;
}
#Override
public int getCount() {
return ((null != tradeList) ?
tradeList.size() : 0);
}
#Override
public Trade getItem(int position) {
return ((null != tradeList) ?
tradeList.get(position) : null);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (null == view) {
view = layoutInflater.inflate(R.layout.grid_category, null);
}
final Trade trade = tradeList.get(position);
if (trade != null) {
final CardView tradeLayout = (CardView) view.findViewById(R.id.category_gridlayout);
final TextView tradeName = (TextView) view.findViewById(R.id.category_name);
final ImageView tradeImage = (ImageView) view.findViewById(R.id.category_icon);
tradeName.setText(trade.getNameOwn());
tradeName.setSelected(true);
tradeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Toast.makeText(context, category.getId()+" "+category.getName(), Toast.LENGTH_LONG).show();
Intent intent = new Intent(context, InboxActivity.class);
intent.putExtra("loginUserId", loginUserId);
intent.putExtra("tradeId",String.valueOf(trade.getId()));
context.startActivity(intent);
}
});
}
return view;
}
}
I can confirm that the tradeArrayList order is correct in my activity but the listview display the trade in random order. I cannot find the bug, please help
I noticed your not using a holder pattern. Always utilize it for optimization.
To the issue at hand, try refactoring your code using the holder pattern. You can learn more about it online.
There is no need overriding the getItem() method too.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
Trade trade = getItem(position);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.grid_category, parent, false);
holder = new ViewHolder();
holder.tradeName = (TextView) convertView.findViewById(R.id.category_name);
holder.tradeLayout = (CardView) convertView.findViewById(R.id.category_gridlayout);
holder.tradeImage = (ImageView) convertView.findViewById(R.id.category_icon);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tradeName.setText(trade.getNameOwn());
holder.tradeName.setSelected(true);
holder.tradeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Toast.makeText(context, category.getId()+" "+category.getName(), Toast.LENGTH_LONG).show();
Intent intent = new Intent(context, InboxActivity.class);
intent.putExtra("loginUserId", loginUserId);
intent.putExtra("tradeId",String.valueOf(trade.getId()));
context.startActivity(intent);
}
});
}
return convertView;
}
}
Your viewholder class code:
static class ViewHolder {
private TextView tradeName;
private TextView tradeLayout;
private ImageView tradeImage;
}
I have found the solution, create a view holder in adapter and create a setter to set the tradeList before call the notifyDataSetChanged()
This is my answer
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
final Trade trade = tradeList.get(position);
Log.d("worked",position+": "+trade.getId());
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.list_trade, parent, false);
holder = new ViewHolder();
holder.tradeName = (TextView) convertView.findViewById(R.id.trade_name);
holder.tradeLayout = (CardView) convertView.findViewById(R.id.trade_listlayout);
holder.tradeImage = (ImageView) convertView.findViewById(R.id.trade_icon);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (trade != null) {
holder.tradeName.setText(trade.getNameOwn());
holder.tradeName.setSelected(true);
try {
Picasso.with(context)
.load(trade.getImageOwnOne())
.error(R.drawable.ic_menu_camera)
.resize(200, 200)
.into(holder.tradeImage);
}catch(Exception e){
holder.tradeImage.setImageResource(R.drawable.ic_menu_camera);
}
holder.tradeImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Toast.makeText(context, category.getId()+" "+category.getName(), Toast.LENGTH_LONG).show();
Intent intent = new Intent(context, DisplayTradeActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("trade", trade);
intent.putExtras(bundle);
context.startActivity(intent);
}
});
holder.tradeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Toast.makeText(context, category.getId()+" "+category.getName(), Toast.LENGTH_LONG).show();
Intent intent = new Intent(context, InboxActivity.class);
intent.putExtra("loginUserId", loginUserId);
intent.putExtra("tradeId",String.valueOf(trade.getId()));
context.startActivity(intent);
}
});
}
return convertView;
}
public void setTradeList(ArrayList<Trade> tl) {
this.tradeList = tl;
}
static class ViewHolder {
private TextView tradeName;
private CardView tradeLayout;
private ImageView tradeImage;
}

How to set visibility true of one button onclick of another button in listView - android

I want to set the visibility to true of btnProductDetailMinus to true on click of btnProductDetailAddToCart. This is my code.
public class ProductVariantAdapter extends BaseAdapter {
private Context context;
private ArrayList<ProductVariant> productVariants = new ArrayList<>();
/*private view holder class*/
private class ViewHolder {
TextView productVariantName;
TextView productVariantMrp;
TextView productVariantSellPrice;
Button btnProductDetailAddToCart, btnProductDetailPlus, btnProductDetailQty, btnProductDetailMinus;
}
public ProductVariantAdapter(Context context, ArrayList<ProductVariant> productVariants) {
this.context = context;
this.productVariants = productVariants;
}
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.listview_product_variant, null);
holder = new ViewHolder();
holder.productVariantName = (TextView) convertView.findViewById(R.id.productVariantName);
holder.productVariantMrp = (TextView) convertView.findViewById(R.id.productVariantMrp);
holder.productVariantSellPrice = (TextView) convertView.findViewById(R.id.productVariantSellPrice);
holder.btnProductDetailAddToCart = (Button) convertView.findViewById(R.id.btnProductDetailAddToCart);
holder.btnProductDetailPlus = (Button)convertView.findViewById(R.id.btnProductDetailPlus);
holder.btnProductDetailQty = (Button)convertView.findViewById(R.id.btnProductDetailQty);
holder.btnProductDetailMinus = (Button)convertView.findViewById(R.id.btnProductDetailMinus);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ProductVariant productVariant = (ProductVariant) getItem(position);
if (productVariant != null) {
holder.productVariantName.setText(productVariant.getVariant().getVariantName());
holder.productVariantMrp.setText(productVariant.getMrp().toString());
holder.productVariantSellPrice.setText(productVariant.getSellPrice().toString());
holder.btnProductDetailAddToCart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Utility.displayToast("wonderful");
holder.btnProductDetailMinus.setVisibility(View.VISIBLE);
}
});
} else {
Toast.makeText(context, "product variant is null", Toast.LENGTH_SHORT).show();
}
return convertView;
}
#Override
public int getCount() {
return productVariants.size();
}
#Override
public Object getItem(int position) {
return productVariants.get(position);
}
#Override
public long getItemId(int position) {
return productVariants.indexOf(getItem(position));
}
}
But its giving error "Variable holder is accessed from within inner class. needs to declared final"
This is the line giving error.
holder.btnProductDetailMinus.setVisibility(View.VISIBLE);
How to fix this?
Assign holder tot a final variabele:
final ViewHolder fHolder = holder;
Then in onClick:
fHolder.btnProductDetailMinus.setVisibility(View.VISIBLE);
Move this line out of the if (convertView == null)
holder = new ViewHolder();
You can declare final ViewHolder holder = null; instead of ViewHolder holder = null;
You can just make it a Global variable.
set public static class ViewHolder instead of private class ViewHolder
One of the approach that I follow is create a Map or List in Adapter class and onClick set value and that collection, and in adapter getView method check for the collection value. .
Checkout the below code.
public class ProductVariantAdapter extends BaseAdapter {
private Context context;
private ArrayList<ProductVariant> productVariants = new ArrayList<>();
private Map<Integer, Boolean> visibilityMap = new HashMap<>();
/*private view holder class*/
private class ViewHolder {
TextView productVariantName;
TextView productVariantMrp;
TextView productVariantSellPrice;
Button btnProductDetailAddToCart, btnProductDetailPlus, btnProductDetailQty, btnProductDetailMinus;
}
public ProductVariantAdapter(Context context, ArrayList<ProductVariant> productVariants) {
this.context = context;
this.productVariants = productVariants;
}
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.listview_product_variant, null);
holder = new ViewHolder();
holder.productVariantName = (TextView) convertView.findViewById(R.id.productVariantName);
holder.productVariantMrp = (TextView) convertView.findViewById(R.id.productVariantMrp);
holder.productVariantSellPrice = (TextView) convertView.findViewById(R.id.productVariantSellPrice);
holder.btnProductDetailAddToCart = (Button) convertView.findViewById(R.id.btnProductDetailAddToCart);
holder.btnProductDetailPlus = (Button)convertView.findViewById(R.id.btnProductDetailPlus);
holder.btnProductDetailQty = (Button)convertView.findViewById(R.id.btnProductDetailQty);
holder.btnProductDetailMinus = (Button)convertView.findViewById(R.id.btnProductDetailMinus);
holder.btnProductDetailAddToCart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = (int)v.getTag();
Utility.displayToast("wonderful");
visibilityMap.put(position, true);
notifyDataSetChanged();
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.btnProductDetailAddToCart.setTag(position);
ProductVariant productVariant = (ProductVariant) getItem(position);
if (productVariant != null) {
holder.productVariantName.setText(productVariant.getVariant().getVariantName());
holder.productVariantMrp.setText(productVariant.getMrp().toString());
holder.productVariantSellPrice.setText(productVariant.getSellPrice().toString());
} else {
Toast.makeText(context, "product variant is null", Toast.LENGTH_SHORT).show();
}
if(visibilityMap.containsKey(position) && visibilityMap.get(position)){
holder.btnProductDetailMinus.setVisibility(View.VISIBLE);
}else{
holder.btnProductDetailMinus.setVisibility(View.GONE);
}
return convertView;
}
#Override
public int getCount() {
return productVariants.size();
}
#Override
public Object getItem(int position) {
return productVariants.get(position);
}
#Override
public long getItemId(int position) {
return productVariants.indexOf(getItem(position));
}
}

How to deal with an array item from a spinner?

I need a code to deal with an item in array inside a spinner to allow me to check
.....
if(item == "item name"){
do something
}
or
if(item.position == 1){
do something
}
You can change in adapter like this code
public class PlannedRideAdapter extends BaseAdapter {
// private LayoutInflater mInflater;
private Context context;
private RideData rideData = new RideData();
private List<RideData> ride_data;
public PlannedRideAdapter(Context context, List<RideData> ridedata) {
this.context = context;
this.ride_data = ridedata;
}
#Override
public int getCount() {
if (ride_data.size() > 0) {
return ride_data.size();
}
return 1;
}
#Override
public Object getItem(int arg0) {
return arg0;
}
#Override
public long getItemId(int arg0) {
return arg0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewholder;
if (convertView == null) {
LayoutInflater inflator = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflator.inflate(R.layout.plannedride_km_list, null);
viewholder = new ViewHolder();
convertView.setTag(viewholder);
} else {
viewholder = (ViewHolder) convertView.getTag();
}
rideData = ride_data.get(position);
// init view
viewholder.imageView = (ImageView) convertView
.findViewById(R.id.imageView1);
viewholder.date = (TextView) convertView.findViewById(R.id.date_value);
viewholder.source = (CustomFontTextView) convertView
.findViewById(R.id.source_icon);
viewholder.time = (TextView) convertView.findViewById(R.id.time_value);
viewholder.destination = (CustomFontTextView) convertView
.findViewById(R.id.dest_icon);
viewholder.rideId = (CustomFontTextView) convertView
.findViewById(R.id.rideId);
viewholder.ride_id = (TextView) convertView
.findViewById(R.id.rideIdText);
viewholder.distance = (CustomFontTextView) convertView
.findViewById(R.id.distance);
viewholder.source_value = (TextView) convertView
.findViewById(R.id.source_value);
viewholder.ride_id_value = (TextView) convertView
.findViewById(R.id.rideId_value);
viewholder.distance_value = (TextView) convertView
.findViewById(R.id.distance_value);
// viewholder.ride_detail = (TextView) convertView
// .findViewById(R.id.textRideDetail);
// setview
viewholder.date.setText("" + rideData.getDate());
viewholder.source.setText(context.getResources().getString(
R.string.font_awesome_marker_icon));
viewholder.source_value.setText("" + rideData.getSource());
viewholder.time.setText("" + rideData.getTime());
viewholder.rideId.setText(context.getResources().getString(
R.string.font_awesome_bookingnumber_icon));
viewholder.ride_id_value.setText("" + rideData.getRideId());
if (rideData.getRideType().equalsIgnoreCase("k")) {
viewholder.distance.setVisibility(View.VISIBLE);
viewholder.distance_value.setVisibility(View.VISIBLE);
viewholder.imageView.setImageResource((R.drawable.one_way));
viewholder.destination.setText(context.getResources().getString(
R.string.font_awesome_radio_tick_icon));
viewholder.distance.setText(context.getResources().getString(
R.string.font_awesome_distance_icon));
viewholder.distance_value.setText(""
+ rideData.getKilometerRide().getDestination());
} else {
viewholder.imageView.setImageResource((R.drawable.time));
viewholder.destination.setText(context.getResources().getString(
R.string.font_awesome_clock_icon));
viewholder.distance.setVisibility(View.GONE);
viewholder.distance_value.setVisibility(View.GONE);
// viewholder.ride_detail.setVisibility(View.GONE);
}
return convertView;
}
public List<RideData> getRide_data() {
return ride_data;
}
public void setRide_data(List<RideData> ride_data) {
this.ride_data = ride_data;
}
public static class ViewHolder {
public CustomFontTextView source;
public CustomFontTextView destination;
public CustomFontTextView rideId;
public CustomFontTextView distance;
public CustomFontTextView total_time;
public TextView date, time, distance_value, source_value,
destination_value, time_value, ride_id_value, ride_id;
public ImageView imageView;
}
}
I am showing list based on kilometer(rideType k) or hourly (rideType h) with same layout.

Android: Populate ListView with CustomAdapter

I would like to create ListView using CustomAdapter that extends BaseAdapter. I have CustomAdapter(Context context, List<Map<String, String>> dataList).
How do I get data from Map and set my views in the getView() method? Before I've tried to use SimpleAdapter but I need to set bitmap stored on SD, and path to image I've stored in map. Every help is appreciated.
Here's my source of CustomAdapter:
public class CustomAdapter extends BaseAdapter {
private Context context;
private String pricePrecision = null;
private List<Map<String, String>> data;
public CustomAdapter(Context context, List<Map<String, String>> data) {
super();
this.context = context;
this.data = data;
}
#Override
public int getCount() {
return (data == null) ? 0 : data.size();
}
#Override
public Object getItem(int position) {
return data.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
LayoutInflater inflater = ((LayoutInflater) context
.getSystemService(Context.LOCATION_SERVICE));
if (convertView == null) {
convertView = inflater.inflate(R.layout.order_list_item, null);
viewHolder = new ViewHolder();
viewHolder.itemThumbnail = ((ImageView) convertView
.findViewById(R.id.ivItemThumb));
viewHolder.itemName = ((TextView) convertView
.findViewById(R.id.tvItemName));
viewHolder.itemPrice = ((TextView) convertView
.findViewById(R.id.tvItemPrice));
convertView.setTag(viewHolder);
} else {
viewHolder = ((ViewHolder) convertView.getTag());
}
return null;
}
private class ViewHolder {
private ImageView itemThumbnail;
private TextView itemName;
private TextView itemPrice;
}
}
Change your getView method as :
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
LayoutInflater inflater = ((LayoutInflater) context
.getSystemService(Context.LOCATION_SERVICE));
if (convertView == null) {
convertView = inflater.inflate(R.layout.order_list_item, null);
viewHolder = new ViewHolder();
viewHolder.itemThumbnail = ((ImageView) convertView
.findViewById(R.id.ivItemThumb));
viewHolder.itemName = ((TextView) convertView
.findViewById(R.id.tvItemName));
viewHolder.itemPrice = ((TextView) convertView
.findViewById(R.id.tvItemPrice));
convertView.setTag(viewHolder);
} else {
viewHolder = ((ViewHolder) convertView.getTag());
}
viewHolder.itemThumbnail.setImageBitmap(data.get(position).get("thumbnail"));
viewHolder.itemName.setText(data.get(position).get("name"));
viewHolder.itemPrice.setText(""+data.get(position).get("price"));
return viewHolder;
}
private class ViewHolder {
private ImageView itemThumbnail;
private TextView itemName;
private TextView itemPrice;
}
A little addition to (ρяσѕρєя K)'s code.
You might want to set listeners on your thumbnails.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
LayoutInflater inflater = ((LayoutInflater) context
.getSystemService(Context.LOCATION_SERVICE));
if (convertView == null) {
convertView = inflater.inflate(R.layout.order_list_item, null);
viewHolder = new ViewHolder();
viewHolder.itemThumbnail = ((ImageView) convertView
.findViewById(R.id.ivItemThumb));
viewHolder.itemName = ((TextView) convertView
.findViewById(R.id.tvItemName));
viewHolder.itemPrice = ((TextView) convertView
.findViewById(R.id.tvItemPrice));
viewHolder.itemThumbnail.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
convertView.setTag(viewHolder);
} else {
viewHolder = ((ViewHolder) convertView.getTag());
}
viewHolder.itemThumbnail.setImageBitmap(data.get(position).get(
"thumbnail"));
viewHolder.itemName.setText(data.get(position).get("name"));
viewHolder.itemPrice.setText("" + data.get(position).get("price"));
return viewHolder;
}
private class ViewHolder {
private ImageView itemThumbnail;
private TextView itemName;
private TextView itemPrice;
}

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