Android Drawer – Change Icon onClick - android

I have got a simple navigation drawer with a header (see picture).
What I want to do now is whenever the header (listview-item with position 0) is clicked to either change the down-arrow-icon (bottom right corner of header) to an up-arrow-icon or to rotate it so the arrow points up. How can i achieve this with my adapter?
My List-Adapter:
public class MyAdapter extends BaseAdapter {
private String[] _navTitles;
private int[] _navIcons;
private String _nameHeader;
private int _profileHeader;
private String _emailHeader;
public int _arrowHeader;
public MyAdapter(String[] navTitles, int[] navIcons, String nameHeader, String emailHeader, int profileHeader, int arrowHeader) {
_navTitles = navTitles;
_navIcons = navIcons;
_nameHeader = nameHeader;
_emailHeader = emailHeader;
_profileHeader = profileHeader;
_arrowHeader = arrowHeader;
}
#Override
public int getCount() {
return _navTitles.length;
}
#Override
public Object getItem(int position) {
return _navTitles[position];
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder viewHolder = null;
if (view == null) {
if (position == 0) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.header, parent, false);
viewHolder = new ViewHolder();
viewHolder.profileView = (CircleImageView) view.findViewById(R.id.circleView);
viewHolder.nameView = (TextView) view.findViewById(R.id.name);
viewHolder.emailView = (TextView) view.findViewById(R.id.email);
viewHolder.arrowView = (ImageView) view.findViewById(R.id.arrow);
viewHolder.holderID = 0;
view.setTag(viewHolder);
} else {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row, parent, false);
viewHolder = new ViewHolder();
viewHolder.textView = (TextView) view.findViewById(R.id.rowText);
viewHolder.imageView = (ImageView) view.findViewById(R.id.rowIcon);
viewHolder.holderID = 1;
view.setTag(viewHolder);
}
} else {
viewHolder = (ViewHolder) view.getTag();
}
if (_navTitles.length > position && position != 0) {
viewHolder.textView.setText(_navTitles[position]);
viewHolder.textView.setTag(_navTitles[position]);
viewHolder.imageView.setImageResource(_navIcons[position]);
viewHolder.imageView.setTag(_navIcons[position]);
}
if (position == 0) {
viewHolder.profileView.setImageResource(_profileHeader);
viewHolder.nameView.setText(_nameHeader);
viewHolder.emailView.setText(_emailHeader);
viewHolder.arrowView.setImageResource(_arrowHeader);
}
return view;
}
public static class ViewHolder {
int holderID;
TextView textView;
ImageView imageView;
CircleImageView profileView;
TextView nameView;
TextView emailView;
ImageView arrowView;
}
}
Thanks for any help!

I solved it by myself. If anybody needs it:
final ViewHolder finalViewHolder = viewHolder;
viewHolder.arrowView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (finalViewHolder.arrowView.getTag().equals("down")) {
finalViewHolder.arrowView.setImageResource(R.drawable.ic_arrow_drop_up_white_48dp);
finalViewHolder.arrowView.setTag("up");
} else if (finalViewHolder.arrowView.getTag().equals("up")) {
finalViewHolder.arrowView.setImageResource(R.drawable.ic_arrow_drop_down_white_48dp);
finalViewHolder.arrowView.setTag("down");
}
}
});

Related

ListView Item's position repeated

I am creating a dynamic listview using fragment in which item's position are repeating after 6th index(Note- items are not repeating).
I have to change quantity of item on button click, but when I try to click and change quantity at index 0 it also changes the 7th index quantity.
Please help me out
Thanx in advance.
public class MenuListAdapter extends ArrayAdapter<DataModel> {
private Activity activity;
public String[] ItemsList = new String[1000];
public String[] ItemsRelPrice;
ArrayList<String> foodItemTitle = new ArrayList<>();
private static String currentTitle;
private String selection;
private String ttl;
public MenuListAdapter(Activity activity, int resource, List<DataModel> dataModelList) {
super(activity, resource, dataModelList);
this.activity = activity;
}
#Nullable
#Override
public DataModel getItem(int position) {
return super.getItem(position);
}
#Override
public int getCount() {
return super.getCount();
}
#Override
public long getItemId(int position) {
return super.getItemId(position);
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.food_item_list, parent, false);
holder = new ViewHolder(convertView);
final ImageButton addBtn = convertView.findViewById(R.id.img_add);
final ImageButton removeBtn = convertView.findViewById(R.id.img_remove);
final TextView qty = convertView.findViewById(R.id.item_quantity);
final TextView priceTtl = convertView.findViewById(R.id.txt_cost);
final TextView foodItem = convertView.findViewById(R.id.item_name);
final TextView foodPrice = convertView.findViewById(R.id.per_item_cost);
addBtn.setTag(position);
addBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.v("btn tag", String.valueOf(addBtn.getTag()));
Log.v("qty tag", String.valueOf(qty.getTag()));
Log.v("Price tag", String.valueOf(priceTtl.getTag()));
int curntQty = Integer.parseInt(qty.getText().toString()) + 1;
int costPerItem = Integer.valueOf(foodPrice.getText().toString());
if (qty.getTag() == addBtn.getTag())
qty.setText(String.valueOf(curntQty));
if (priceTtl.getTag() == addBtn.getTag())
priceTtl.setText(String.valueOf(costPerItem * curntQty));
new MenuPage().addItem(costPerItem);
}
});
removeBtn.setTag(position);
removeBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (qty.getText().toString() != "0") {
int qtyTtl = Integer.parseInt(qty.getText().toString()) - 1;
qty.setText(String.valueOf(qtyTtl));
int costPerItem = Integer.valueOf(foodPrice.getText().toString());
int curPrice = Integer.valueOf(priceTtl.getText().toString());
priceTtl.setText(String.valueOf(curPrice - costPerItem));
new MenuPage().removeItem(costPerItem);
} else {
Toast.makeText(getContext(), "Add Item First", Toast.LENGTH_SHORT).show();
}
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
DataModel dataModel = getItem(position);
holder.foodItem.setTag(position);
holder.foodItem.setText(dataModel.getItemTitleFinal());
holder.foodPrice.setTag(position);
holder.foodPrice.setText(dataModel.getItemPriceFinal());
holder.foodType.setTag(position);
holder.foodType.setImageResource(dataModel.getItemImgFinal());
holder.addBtn.setTag(position);
holder.removeBtn.setTag(position);
holder.qty.setTag(position);
holder.priceCurnt.setTag(position);
return convertView;
}
private static class ViewHolder {
private TextView foodItem;
private TextView foodPrice;
private ImageView foodType;
private ImageButton addBtn;
private ImageButton removeBtn;
private TextView qty;
private TextView priceCurnt;
public ViewHolder(View v) {
foodItem = v.findViewById(R.id.item_name);
foodPrice = v.findViewById(R.id.per_item_cost);
foodType = v.findViewById(R.id.fdTypeImg);
addBtn = v.findViewById(R.id.img_add);
removeBtn = v.findViewById(R.id.img_remove);
qty = v.findViewById(R.id.item_quantity);
priceCurnt = v.findViewById(R.id.txt_cost);
}
}
}
I modified your adapter class. You need to have dataModelList which will act as source to inflate the view holders. Now, you need to supply the list through the constructor and make small modifications at places where I might have missed. Let me know if modifications are needed.
public class MenuListAdapter extends ArrayAdapter<DataModel> {
private Activity activity;
public String[] ItemsList = new String[1000];
public String[] ItemsRelPrice;
ArrayList<String> foodItemTitle = new ArrayList<>();
private static String currentTitle;
private String selection;
private String ttl;
//List of DataModel objects to inflate into view holders
private List<DataModel> dataModelList;
public MenuListAdapter(Activity activity, int resource, List<DataModel> dataModelList) {
super(activity, resource, dataModelList);
this.activity = activity;
//set the list in constructor
this.dataModelList = dataModelList;
}
#Nullable
#Override
public DataModel getItem(int position) {
return dataModelList.get(position);
}
#Override
public int getCount() {
return dataModelList.size();
}
#Override
public long getItemId(int position) {
return dataModelList.get(position).getId();
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.food_item_list, parent, false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//findViewById codes removed because they are unnecessary
//Copy to final object so that it can be used inside inner classes
final ViewHolder finalHolder = holder;
DataModel dataModel = getItem(position);
finalHolder.foodItem.setTag(position);
finalHolder.foodItem.setText(dataModel.getItemTitleFinal());
finalHolder.foodPrice.setTag(position);
finalHolder.foodPrice.setText(dataModel.getItemPriceFinal());
finalHolder.foodType.setTag(position);
finalHolder.foodType.setImageResource(dataModel.getItemImgFinal());
finalHolder.addBtn.setTag(position);
finalHolder.removeBtn.setTag(position);
finalHolder.qty.setTag(position);
finalHolder.priceCurnt.setTag(position);
finalHolder.addBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.v("btn tag", String.valueOf(finalHolder.addBtn.getTag()));
Log.v("qty tag", String.valueOf(finalHolder.qty.getTag()));
Log.v("Price tag", String.valueOf(finalHolder.priceCurnt.getTag()));
int curntQty = Integer.parseInt(finalHolder.qty.getText().toString()) + 1;
int costPerItem = Integer.valueOf(finalHolder.foodPrice.getText().toString());
if (finalHolder.qty.getTag() == finalHolder.addBtn.getTag())
finalHolder.qty.setText(String.valueOf(curntQty));
if (finalHolder.priceCurnt.getTag() == finalHolder.addBtn.getTag())
finalHolder.priceCurnt.setText(String.valueOf(costPerItem * curntQty));
new MenuPage().addItem(costPerItem);
}
});
finalHolder.removeBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (finalHolder.qty.getText().toString() != "0") {
int qtyTtl = Integer.parseInt(finalHolder.qty.getText().toString()) - 1;
finalHolder.qty.setText(String.valueOf(qtyTtl));
int costPerItem = Integer.valueOf(finalHolder.foodPrice.getText().toString());
int curPrice = Integer.valueOf(finalHolder.priceCurnt.getText().toString());
finalHolder.priceCurnt.setText(String.valueOf(curPrice - costPerItem));
new MenuPage().removeItem(costPerItem);
} else {
Toast.makeText(getContext(), "Add Item First", Toast.LENGTH_SHORT).show();
}
}
});
return convertView;
}
private static class ViewHolder {
private TextView foodItem;
private TextView foodPrice;
private ImageView foodType;
private ImageButton addBtn;
private ImageButton removeBtn;
private TextView qty;
private TextView priceCurnt;
public ViewHolder(View v) {
foodItem = v.findViewById(R.id.item_name);
foodPrice = v.findViewById(R.id.per_item_cost);
foodType = v.findViewById(R.id.fdTypeImg);
addBtn = v.findViewById(R.id.img_add);
removeBtn = v.findViewById(R.id.img_remove);
qty = v.findViewById(R.id.item_quantity);
priceCurnt = v.findViewById(R.id.txt_cost);
}
}
}
Change your code addBtn.setOnClickListener to and do the same with removeBtn button.setOnClickListener
addBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.v("btn tag", String.valueOf(addBtn.getTag()));
Log.v("qty tag", String.valueOf(qty.getTag()));
Log.v("Price tag", String.valueOf(priceTtl.getTag()));
int curntQty = Integer.parseInt(qty.getText().toString()) + 1;
// update your quantity to data model here
dataModel.updateQuantiy(curntQty);
int costPerItem = Integer.valueOf(foodPrice.getText().toString());
/*if (qty.getTag() == addBtn.getTag())
qty.setText(String.valueOf(curntQty));*/
if (priceTtl.getTag() == addBtn.getTag())
priceTtl.setText(String.valueOf(costPerItem * curntQty));
new MenuPage().addItem(costPerItem);
// Notify that the data set change at the position to re-binding layout.
notifyItemChanged(position);
}
});
And
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
// Your normal code
DataModel dataModel = getItem(position);
holder.foodItem.setTag(position);
holder.foodItem.setText(dataModel.getItemTitleFinal());
holder.foodPrice.setTag(position);
holder.foodPrice.setText(dataModel.getItemPriceFinal());
holder.foodType.setTag(position);
holder.foodType.setImageResource(dataModel.getItemImgFinal());
holder.addBtn.setTag(position);
holder.removeBtn.setTag(position);
holder.qty.setTag(position);
holder.priceCurnt.setTag(position);
// add your quantity to textview
holder.qty.setText(dataModel.getQuantity());
return convertView;
}
Change following code:
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.food_item_list, parent, false);
holder = new ViewHolder(convertView);
final ImageButton addBtn = convertView.findViewById(R.id.img_add);
final ImageButton removeBtn = convertView.findViewById(R.id.img_remove);
final TextView qty = convertView.findViewById(R.id.item_quantity);
final TextView priceTtl = convertView.findViewById(R.id.txt_cost);
final TextView foodItem = convertView.findViewById(R.id.item_name);
final TextView foodPrice = convertView.findViewById(R.id.per_item_cost);
addBtn.setTag(position);
addBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.v("btn tag", String.valueOf(addBtn.getTag()));
Log.v("qty tag", String.valueOf(qty.getTag()));
Log.v("Price tag", String.valueOf(priceTtl.getTag()));
int curntQty = Integer.parseInt(qty.getText().toString()) + 1;
int costPerItem = Integer.valueOf(foodPrice.getText().toString());
if (qty.getTag() == addBtn.getTag())
qty.setText(String.valueOf(curntQty));
if (priceTtl.getTag() == addBtn.getTag())
priceTtl.setText(String.valueOf(costPerItem * curntQty));
new MenuPage().addItem(costPerItem);
}
});
removeBtn.setTag(position);
removeBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (qty.getText().toString() != "0") {
int qtyTtl = Integer.parseInt(qty.getText().toString()) - 1;
qty.setText(String.valueOf(qtyTtl));
int costPerItem = Integer.valueOf(foodPrice.getText().toString());
int curPrice = Integer.valueOf(priceTtl.getText().toString());
priceTtl.setText(String.valueOf(curPrice - costPerItem));
new MenuPage().removeItem(costPerItem);
} else {
Toast.makeText(getContext(), "Add Item First", Toast.LENGTH_SHORT).show();
}
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
DataModel dataModel = getItem(position);
holder.foodItem.setTag(position);
holder.foodItem.setText(dataModel.getItemTitleFinal());
holder.foodPrice.setTag(position);
holder.foodPrice.setText(dataModel.getItemPriceFinal());
holder.foodType.setTag(position);
holder.foodType.setImageResource(dataModel.getItemImgFinal());
holder.addBtn.setTag(position);
holder.removeBtn.setTag(position);
holder.qty.setTag(position);
holder.priceCurnt.setTag(position);
return convertView;
}
private static class ViewHolder {
private TextView foodItem;
private TextView foodPrice;
private ImageView foodType;
private ImageButton addBtn;
private ImageButton removeBtn;
private TextView qty;
private TextView priceCurnt;
public ViewHolder(View v) {
foodItem = v.findViewById(R.id.item_name);
foodPrice = v.findViewById(R.id.per_item_cost);
foodType = v.findViewById(R.id.fdTypeImg);
addBtn = v.findViewById(R.id.img_add);
removeBtn = v.findViewById(R.id.img_remove);
qty = v.findViewById(R.id.item_quantity);
priceCurnt = v.findViewById(R.id.txt_cost);
}
}
To:
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.item_stock, parent, false);
convertView = inflater.inflate(R.layout.food_item_list, parent, false);
holder = new ViewHolder(convertView);
final ImageButton addBtn = convertView.findViewById(R.id.img_add);
final ImageButton removeBtn = convertView.findViewById(R.id.img_remove);
final TextView qty = convertView.findViewById(R.id.item_quantity);
final TextView priceTtl = convertView.findViewById(R.id.txt_cost);
final TextView foodItem = convertView.findViewById(R.id.item_name);
final TextView foodPrice = convertView.findViewById(R.id.per_item_cost);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
DataModel dataModel = getItem(position);
holder.foodItem.setTag(position);
holder.foodItem.setText(dataModel.getItemTitleFinal());
holder.foodPrice.setTag(position);
holder.foodPrice.setText(dataModel.getItemPriceFinal());
holder.foodType.setTag(position);
holder.foodType.setImageResource(dataModel.getItemImgFinal());
holder.addBtn.setTag(position);
holder.removeBtn.setTag(position);
holder.qty.setTag(position);
holder.priceCurnt.setTag(position);
holder.addBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.v("btn tag", String.valueOf(addBtn.getTag()));
Log.v("qty tag", String.valueOf(qty.getTag()));
Log.v("Price tag", String.valueOf(priceTtl.getTag()));
int curntQty = Integer.parseInt(qty.getText().toString()) + 1;
int costPerItem = Integer.valueOf(foodPrice.getText().toString());
if (qty.getTag() == addBtn.getTag())
qty.setText(String.valueOf(curntQty));
if (priceTtl.getTag() == addBtn.getTag())
priceTtl.setText(String.valueOf(costPerItem * curntQty));
new MenuPage().addItem(costPerItem);
}
});
holder.removeBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (qty.getText().toString() != "0") {
int qtyTtl = Integer.parseInt(qty.getText().toString()) - 1;
qty.setText(String.valueOf(qtyTtl));
int costPerItem = Integer.valueOf(foodPrice.getText().toString());
int curPrice = Integer.valueOf(priceTtl.getText().toString());
priceTtl.setText(String.valueOf(curPrice - costPerItem));
new MenuPage().removeItem(costPerItem);
} else {
Toast.makeText(getContext(), "Add Item First", Toast.LENGTH_SHORT).show();
}
}
});
return convertView;
}
private static class ViewHolder {
private TextView foodItem;
private TextView foodPrice;
private ImageView foodType;
private ImageButton addBtn;
private ImageButton removeBtn;
private TextView qty;
private TextView priceCurnt;
public ViewHolder(View v) {
foodItem = v.findViewById(R.id.item_name);
foodPrice = v.findViewById(R.id.per_item_cost);
foodType = v.findViewById(R.id.fdTypeImg);
addBtn = v.findViewById(R.id.img_add);
removeBtn = v.findViewById(R.id.img_remove);
qty = v.findViewById(R.id.item_quantity);
priceCurnt = v.findViewById(R.id.txt_cost);
}
}

Android getView() in BaseAdapter not called (but getCount() is called)

There no errors but the ListView is empty. I have implemented getCount() which returns right number of items in my ArrayList. And the ListView is visibility。
I had call setListAdapter on the ListView,and the xml is match_parent.
This is my Fragment.
mNewsList = infoBean.getNews();
if (mNewsList != null) {
mNewsAdapter = new NewsAdapter();
mListView.setAdapter(mNewsAdapter);
mNewsAdapter.notifyDataSetChanged();
}
} else {//load more
List<HomeBean.NewsBean> news = infoBean.getNews();
if (mNewsList != null) {
mNewsList.addAll(news);
mNewsAdapter.notifyDataSetChanged();
}
This is my Adapter:
private class NewsAdapter extends BaseAdapter {
#Override
public int getCount() {
return mNewsList.size();
}
#Override
public HomeBean.NewsBean getItem(int position) {
return mNewsList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = View.inflate(MyApplication.getmContext(), R.layout.item_list, null);
holder = new ViewHolder();
holder.ivIcon = convertView.findViewById(R.id.iv_item);
holder.tvTitle = convertView.findViewById(R.id.tv_item_list_title);
holder.content = convertView.findViewById(R.id.tv_item_list_content);
holder.tvTime = convertView.findViewById(R.id.tv_time);
holder.author = convertView.findViewById(R.id.tv_author);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvTitle.setText(getItem(position).getTitle());
holder.content.setText(getItem(position).getContent());
SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd");
String itemTime = dataFormat.format(getItem(position).getCreate_at() * 1000);
holder.tvTime.setText(itemTime);
holder.author.setText(getItem(position).getSection_name());
if (getItem(position).getCover_pic().startsWith("http:")) {
picUrl = getItem(position).getCover_pic();
} else {
picUrl = RBConstants.SERVER_PIC + getItem(position).getCover_pic();
}
HttpLoader.getInstance(MyApplication.getmContext()).display(holder.ivIcon, picUrl);
return convertView;
}
}
This is my holder.
class ViewHolder {
public ImageView ivIcon;
public TextView tvTitle;
public TextView tvTime;
public TextView content;
public TextView author;
}
this is because mListView.setAdapter(mNewsAdapter); is never executed and of cource there is no need to call mNewsAdapter.notifyDataSetChanged(); immediatelly after calling mListView.setAdapter(mNewsAdapter);
ListView is old, please switch to RecyclerView.

Android GridView item touched listener

I can't find a solution for my problem. I have a gridview with layouts. The layouts contains an imageview and a textview. What I need is to animate the imageview when I touch an item. How can I detect the on item keydown event in the gridview?
Here is my adapter:
public class GridViewAdapter extends BaseAdapter {
private ArrayList<SingleButton> buttons;
private MainActivity mainActivity;
public GridViewAdapter(MainActivity mainActivity) {
this.mainActivity = mainActivity;
this.buttons = new ArrayList<SingleButton>();
Resources res = mainActivity.getApplicationContext().getResources();
String[] temp = res.getStringArray(R.array.buttons);
int[] buttonImages = { R.drawable.pic1, R.drawable.pic2,
R.drawable.pic3, R.drawable.pic4, R.drawable.pic5,
R.drawable.pic6, R.drawable.pic7, R.drawable.pic8,
R.drawable.pic9 };
for (int count = 0; count < 9; ++count) {
this.buttons
.add(new SingleButton(buttonImages[count], temp[count]));
}
}
#Override
public int getCount() {
return this.buttons.size();
}
#Override
public Object getItem(int position) {
return this.buttons.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#SuppressLint("ViewHolder")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if (row == null) {
LayoutInflater layoutInflater = (LayoutInflater) mainActivity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.single_button, parent, false);
holder = new ViewHolder(row);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
holder.buttonImage.setImageResource(buttons.get(position).image);
holder.buttonText.setText(buttons.get(position).buttonText);
return row;
}
class ViewHolder {
ImageView buttonImage;
TextView buttonText;
public ViewHolder(View v) {
this.buttonImage = (ImageView) v.findViewById(R.id.imageViewButton);
this.buttonText = (TextView) v.findViewById(R.id.textViewButton);
}
}
class SingleButton {
int image;
String buttonText;
SingleButton(int image, String buttonText) {
this.image = image;
this.buttonText = buttonText;
}
}
You need to add View.OnTouchListener to the parent view, in which your image is. Here parent view is your grid row.
It can be done via using setOnTouchListener(View.OnTouchListener). Below is code, you need to replace ViewHolder class with below code.
class ViewHolder implements View.OnTouchListener {
ImageView buttonImage;
TextView buttonText;
public ViewHolder(View v) {
this.buttonImage = (ImageView) v.findViewById(R.id.imageViewButton);
this.buttonText = (TextView) v.findViewById(R.id.textViewButton);
v.setOnTouchListener(this);
}
#Override
public boolean onTouch(View v, MotionEvent event) {
if (MotionEvent.ACTION_DOWN == event.getAction()) {
// Here you can put animation for your image view
// this.buttonImage is the target image view.
}
return true;
}
}

Image not visible in list view

I have a custom list view with image and text.Now whenever i open the list view initially the image is visible.Now when i scroll the list the images are being visible.Also i have put condition on which image should be displayed when,but in beginning all the images are same the images are only changed when i scroll the list.I know this issue is related to recycle of the list but i am not able to solve it.Please do help me
Code
public class ShareWithMeAdapter extends BaseAdapter {
private Context context;
private ArrayList<SharedWithMeModel> data;
public ShareWithMeAdapter(Context context, ArrayList<SharedWithMeModel> data) {
this.context = context;
this.data = data;
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = LayoutInflater.from(context).inflate(R.layout.share_with_me_custom_list, viewGroup, false);
holder.tvTitle = (TextView) view.findViewById(R.id.tv_sharedwithme_title);
holder.tvName = (TextView) view.findViewById(R.id.tv_sharedwithme_name);
holder.tvSharedOn = (TextView) view.findViewById(R.id.tv_sharedwithme_on);
holder.image = (ImageView) view.findViewById(R.id.iv_sharedwithme_image);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.tvTitle.setText(data.get(i).getTitle());
if (data.get(i).getName().equals("")) {
holder.tvName.setText("Shared With " + data.get(i).getName());
} else {
holder.tvName.setText("Shared With " + data.get(i).getName());
}
holder.tvSharedOn.setText(data.get(i).getDate());
if (data.get(i).getExtension().equals(".jpg") || data.get(i).getExtension().equals(".png") || data.get(i).getExtension().equals(".jpeg") || data.get(i).getExtension().equals(".gif") || data.get(i).getExtension().equals(".tiff")) {
holder.image.setBackgroundResource(R.drawable.image);
}
if (data.get(i).getExtension().equals(".docx") || data.get(i).getExtension().equals(".doc")) {
holder.image.setBackgroundResource(R.drawable.docx);
}
if (data.get(i).getExtension().equals(".xls") || data.get(i).getExtension().equals(".xlxs")) {
holder.image.setBackgroundResource(R.drawable.excel);
}
if (data.get(i).getExtension().equals(".pdf")) {
holder.image.setBackgroundResource(R.drawable.pdf);
}
// if (data.get(i).getExtension().equals(".ppt")) {
// holder.image.setBackgroundResource(R.drawable.);
// }
return view;
}
private class ViewHolder {
TextView tvTitle, tvName, tvSharedOn;
ImageView image;
}
}
Try this way,hope this will help you to solve your problem.
public class ShareWithMeAdapter extends BaseAdapter {
private Context context;
private ArrayList<SharedWithMeModel> data;
public ShareWithMeAdapter(Context context, ArrayList<SharedWithMeModel> data) {
this.context = context;
this.data = data;
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int i) {
return data.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = LayoutInflater.from(context).inflate(R.layout.share_with_me_custom_list, null);
holder.tvTitle = (TextView) view.findViewById(R.id.tv_sharedwithme_title);
holder.tvName = (TextView) view.findViewById(R.id.tv_sharedwithme_name);
holder.tvSharedOn = (TextView) view.findViewById(R.id.tv_sharedwithme_on);
holder.image = (ImageView) view.findViewById(R.id.iv_sharedwithme_image);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.tvTitle.setText(data.get(i).getTitle());
if (data.get(i).getName().equals("")) {
holder.tvName.setText("Shared With " + data.get(i).getName());
} else {
holder.tvName.setText("Shared With " + data.get(i).getName());
}
holder.tvSharedOn.setText(data.get(i).getDate());
if (data.get(i).getExtension().equals(".jpg") || data.get(i).getExtension().equals(".png") || data.get(i).getExtension().equals(".jpeg") || data.get(i).getExtension().equals(".gif") || data.get(i).getExtension().equals(".tiff")) {
holder.image.setImageResource(R.drawable.image);
}else if (data.get(i).getExtension().equals(".docx") || data.get(i).getExtension().equals(".doc")) {
holder.image.setImageResource(R.drawable.docx);
}else if (data.get(i).getExtension().equals(".xls") || data.get(i).getExtension().equals(".xlxs")) {
holder.image.setImageResource(R.drawable.excel);
}else{
holder.image.setImageResource(R.drawable.pdf);
}
return view;
}
private class ViewHolder {
TextView tvTitle;
TextView tvName;
TextView tvSharedOn;
ImageView image;
}
}
try this , hope it might helpfull
replace all
holder.image.setBackgroundResource(R.drawable.filename);
with
holder.image.setImageResource(R.drawable.filename);
or change these methods
#Override
public Object getItem(int position) {
return data.get(position);
}
#Override
public long getItemId(int position) {
return position;
}

Android - Pull to refresh on custom adapter

I'm developing an Android Application and I have a custom adapter for my ListView. I want to use a Pull to refresh library, such as this one. This works fine with an ArrayAdapter, but not with a custom adapter.
How can I get my pull to refresh working with a custom adapter?
Here my adapter code:
public class AdvertListAdapter extends BaseAdapter {
private AdvertListActivity activity;
private int currentFirstVisibleItem=0;
private int currentLastVisibleItem=8;
public AdvertListAdapter(AdvertListActivity activity) {
this.activity = activity;
}
public void setFirstVisibleItem(int currentFirstVisibleItem) {
this.currentFirstVisibleItem=currentFirstVisibleItem;
}
public void setLastVisibleItem(int currentLastVisibleItem) {
this.currentLastVisibleItem=currentLastVisibleItem;
}
#Override
public int getCount() {
return activity.getAdvertList().size();
}
#Override
public Advert getItem(int pos) {
return activity.getAdvertList().get(pos);
}
#Override
public long getItemId(int pos) {
return activity.getAdvertList().get(pos).getId();
}
private class ViewHolder {
public ImageView image;
public TextView title;
public TextView price;
public TextView size;
public TextView city;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder view;
LayoutInflater inflator = activity.getLayoutInflater();
if(convertView == null) {
view = new ViewHolder();
convertView = inflator.inflate(R.layout.listview_advertlist, null);
view.image = (ImageView) convertView.findViewById(R.id.advertImage);
view.title = (TextView) convertView.findViewById(R.id.advertTitleText);
view.price = (TextView) convertView.findViewById(R.id.advertPriceText);
view.size = (TextView) convertView.findViewById(R.id.advertSizeText);
view.city = (TextView) convertView.findViewById(R.id.advertCityText);
convertView.setTag(view);
}else {
view = (ViewHolder) convertView.getTag();
}
Advert advert = activity.getAdvertList().get(position);
view.title.setText(advert.getTitle());
view.price.setText(String.valueOf(advert.getPrice()+"€"));
view.size.setText("Taille: "+advert.getSize());
view.city.setText(advert.getCity());
if(position <= currentFirstVisibleItem || position >= currentLastVisibleItem) {
view.image.setImageResource(R.drawable.defaultadvertimage);
ServiceHelper.loadAsyncImage(view.image, activity.getApplicationContext(), String.valueOf(advert.getId()+"-mini"), "advert");
}
return convertView;
}
}
Here my activity code:
list = (PullToRefreshListView) findViewById(R.id.advertList);
advertList = getAdvertList();
((PullToRefreshListView) list).setOnRefreshListener(new OnRefreshListener() {
#Override
public void onRefresh() {
// Do work to refresh the list here.
new GetDataTask().execute();
}
});
list.setAdapter(adapter);

Categories

Resources