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;
}
Related
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;
}
}
I'm using Roboletric and I would like to test the onClick into the "viewHolder.cardView" element.
This is my custom adapter (that has two elements). I already build my list and adapter, but how I can test the onClick event with roboletric?
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_TIME_FORMAT, Locale.getDefault());
if (position < mMessageList.size()) {
ItemHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(getContext())
.inflate(R.layout.item_list, parent, false);
viewHolder = new ItemHolder();
viewHolder.cardView = (RelativeLayout) convertView.findViewById(R.id.clickable_area);
viewHolder.txtDate = (TextView) convertView.findViewById(R.id.message_date);
convertView.setTag(viewHolder);
} else {
viewHolder = (ItemHolder) convertView.getTag();
}
final ItemModel item = mMessageList.get(position);
if (item != null) {
viewHolder.id = item.getId();
if (item.getTimestamp() != null){
viewHolder.txtDate.setText(dateFormat.format(item.getTimestamp()));
}
viewHolder.cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
addSelection();
}
});
viewHolder.cardView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
return true;
}
});
viewHolder.cardView.setSelected(item.isSelected());
}
}
return convertView;
}
static class ItemHolder {
protected RelativeLayout cardView;
protected TextView txtDate;
}
Here is what we are doing similar:
SiteAdapter.ViewHolder viewHolder = adapter.onCreateViewHolder(viewGroup, -1);
adapter.setSites(SITES);
adapter.bindViewHolder(viewHolder, 0);
viewHolder.rowLayout.performClick();
verify(busMock).send(selectedEventCaptor.capture());
assertThat(selectedEventCaptor.getValue().getSite()).isEqualTo(SITES.get(0));
where:
#Captor
private ArgumentCaptor<SiteSelectedEvent> selectedEventCaptor;
#Mock
private EventBus busMock;
private static final List<Site> SITES =
Arrays.asList(new Site("1", "Test 1", new Links("one", "two")),
new Site("2", "Test 2", new Links("three", "four")));
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));
}
}
Here is My code for item Click and print data in list view .
listView = (ListView) layout.findViewById(R.id.listView1);
Notifcationadapter adapter = new Notifcationadapter(this, notifies);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
View parentView = (View) view.getParent();
TextView value1 = ((TextView) parentView.findViewById(R.id.title));
String value = value1.getText().toString();
if (value.equals("Post"))
Toast.makeText(this, "Post", 1000).show();
if (value.equals("Request For travelling"))
Toast.makeText(this, "Request for travelling", 1000).show();
}
Here is my adapter :
public class Notifcationadapter extends BaseAdapter {
private static ArrayList<Notify> values;
Context context;
// List<RowItem> rowItems;
ViewHolder holder = null;
LinearLayout linear2;
DataBaseManager dbManager = new DataBaseManager(context);
ArrayList<Notify> notifies;
public Notifcationadapter(Context context, ArrayList<Notify> notifies) {
super();
this.context = context;
this.notifies = notifies;
}
/* private view holder class */
private class ViewHolder {
TextView txtTitle;
TextView txtDesc;
ImageView yesimage;
ImageView noimage;
TextView revresetime;
LinearLayout linear2;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.notifcationlistitem, null);
holder = new ViewHolder();
holder.linear2 = (LinearLayout) convertView.findViewById(R.id.linear2);
holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
holder.yesimage = (ImageView) convertView.findViewById(R.id.imageView1);
holder.noimage = (ImageView) convertView.findViewById(R.id.imageView2);
holder.revresetime = (TextView) convertView.findViewById(R.id.revresetime);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// holder.txtTitle.setText("Title");
String statustype = notifies.get(position).getNotificationType();
if (statustype.equals("1")) {
holder.txtDesc.setText(notifies.get(position).getNotificationDescrpiton());
holder.revresetime.setText(Functions.setLastSeenNotifcationTime(notifies.get(position).getNotificationDatetime()));
holder.txtTitle.setText("Request For travelling");
holder.yesimage.setVisibility(View.VISIBLE);
holder.noimage.setVisibility(View.VISIBLE);
} else {
holder.txtDesc.setText(notifies.get(position).getNotificationDescrpiton());
holder.txtTitle.setText("Post");
holder.revresetime.setText(Functions.setLastSeenNotifcationTime(notifies.get(position).getNotificationDatetime()));
holder.yesimage.setVisibility(View.GONE);
holder.noimage.setVisibility(View.GONE);
}
if (position % 2 == 0) {
convertView.setBackgroundColor(Color.parseColor("#ffffff"));
} else {
convertView.setBackgroundColor(Color.parseColor("#f5f6f1"));
}
holder.yesimage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(context, "Yes", 10000).show();
holder.linear2.setVisibility(View.INVISIBLE);
}
});
holder.noimage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "NO", 10000).show();
holder.linear2.setVisibility(View.INVISIBLE);
}
});
return convertView;
}
#Override
public int getCount() {
return notifies.size();
}
#Override
public Object getItem(int position) {
return notifies.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
}
I am displaying data in List view i wan i want to apply on item click function so that if text view list display post in list then its item click it should Print post if it Request for traveling then it should show Request for travel but i am unable to do this please help where am doing wrong .
you are getting the same value because of this line
TextView value1 = ((TextView) parentView.findViewById(R.id.title));
to get the String value in your adapter or arraylist you can do something like this.
i assume you have arraylist of objects
//you arraylist contains object/class
String value1 = notifies.get(position).getTitle();
and using the adapter
String value1 = adapter.getItem(position);
then #Override the getItem() function in your adapter
#Override
public String getItem(int position){
return notifies.get(position).getTitle();
}
then to move to other activity according to your if statement
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
View parentView = (View) view.getParent();
String value = notifies.get(position).getTitle();
Intent i = null;
if (value.equals("Post")){
i = new Intent(context,PostActivity.class);
}
else if (value.equals("Request For travelling")){
i = new Intent(context,RequestActivity.class);
}
if(i !=null){
startActivity(i);
}
}
I think you should use int position of onItemClick.
and then use if(notifies.get(position).getNotificationType() == 1)
it will be faster and should effectively work also.
First remove the onitemclick listener from activity. and use this code -
listView.setOnItemClickListener(this); // remove this line
as well as remove onitem click-
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
View parentView = (View) view.getParent();
TextView value1 = ((TextView) parentView.findViewById(R.id.title));
String value = value1.getText().toString();
if (value.equals("Post")) {
Toast.makeText(this, "Post", 1000).show();
}
if (value.equals("Request For travelling")) {
Toast.makeText(this, "Request for travelling", 1000).show();
}
}
**just add convertview click listener**
public class Notifcationadapter extends BaseAdapter {
private static ArrayList<Notify> values;
Context context;
// List<RowItem> rowItems;
ViewHolder holder = null;
LinearLayout linear2;
DataBaseManager dbManager = new DataBaseManager(context);
ArrayList<Notify> notifies;
public Notifcationadapter(Context context, ArrayList<Notify> notifies) {
super();
this.context = context;
this.notifies = notifies;
}
/* private view holder class */
private class ViewHolder {
TextView txtTitle;
TextView txtDesc;
ImageView yesimage;
ImageView noimage;
TextView revresetime;
LinearLayout linear2;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.notifcationlistitem, null);
holder = new ViewHolder();
holder.linear2 = (LinearLayout) convertView
.findViewById(R.id.linear2);
holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
holder.yesimage = (ImageView) convertView
.findViewById(R.id.imageView1);
holder.noimage = (ImageView) convertView
.findViewById(R.id.imageView2);
holder.revresetime = (TextView) convertView
.findViewById(R.id.revresetime);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// holder.txtTitle.setText("Title");
String statustype = notifies.get(position).getNotificationType();
if (statustype.equals("1")) {
holder.txtDesc.setText(notifies.get(position)
.getNotificationDescrpiton());
holder.revresetime.setText(Functions
.setLastSeenNotifcationTime(notifies.get(position)
.getNotificationDatetime()));
holder.txtTitle.setText("Request For travelling");
holder.yesimage.setVisibility(View.VISIBLE);
holder.noimage.setVisibility(View.VISIBLE);
}
else {
holder.txtDesc.setText(notifies.get(position)
.getNotificationDescrpiton());
holder.txtTitle.setText("Post");
holder.revresetime.setText(Functions
.setLastSeenNotifcationTime(notifies.get(position)
.getNotificationDatetime()));
holder.yesimage.setVisibility(View.GONE);
holder.noimage.setVisibility(View.GONE);
}
if (position % 2 == 0) {
convertView.setBackgroundColor(Color.parseColor("#ffffff"));
}
else {
convertView.setBackgroundColor(Color.parseColor("#f5f6f1"));
}
holder.yesimage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(context, "Yes", 10000).show();
holder.linear2.setVisibility(View.INVISIBLE);
}
});
holder.noimage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "NO", 10000).show();
holder.linear2.setVisibility(View.INVISIBLE);
}
});
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// write your code to redirect to another activity like this way
Intent i = new Intent(context,
Youractivity.class);
context.startActivity(i);
}
});
return convertView;
}
#Override
public int getCount() {
return notifies.size();
}
#Override
public Object getItem(int position) {
return notifies.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
}
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