There is a custom listview which contains image and text. Am implemented click event on the image in the custom listview and it is working partially. Which means when the list loads when i click on it, i am able to see the image in dialog but when i scroll down and came up then when i try to click on the same image which i clicked earlier the click event is not working. I am not sure what is the reason for this behavior.
code:
public class DescAdapter extends BaseAdapter implements Filterable {
private final listdisplay ds;
private ArrayList<descusers> dusers;
private ArrayList<descusers> orig;
private Activity listdisplay;
PhotoViewAttacher p;
public DescAdapter(listdisplay ds, ArrayList<descusers> dusers,Activity listdisplay) {
this.ds = ds;
this.dusers = dusers;
this.listdisplay = listdisplay;
}
#Override
public int getCount() {
return dusers.size();
}
#Override
public Object getItem(int position) {
return dusers.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
descusers du = dusers.get(position);
String username = du.loginname;
String descCrip=du.descCrip;
final String Limage = du.image;
long Ptime = du.Ptime;
Viewholder viewholder = null;
if(convertView==null) {
viewholder = new Viewholder();
convertView = LayoutInflater.from(ds).inflate(R.layout.customlist, null);
viewholder.uname = (TextView) convertView.findViewById(R.id.username);
viewholder.desc = (TextView) convertView.findViewById(R.id.description);
viewholder.time = (TextView)convertView.findViewById(R.id.time);
viewholder.iview = (ImageView) convertView.findViewById(R.id.imageList);
convertView.setTag(viewholder);
}else {
viewholder = (Viewholder) convertView.getTag();
}
viewholder.uname.setText(username);
viewholder.desc.setText(descCrip);
if (Limage.trim().isEmpty()) {
viewholder.iview.setEnabled(false);
} else {
Glide.with(convertView.getContext()).load(Limage)
.diskCacheStrategy(DiskCacheStrategy.ALL).centerCrop().into(viewholder.iview);
}
return convertView;
}
Onclick code:
final Viewholder finalViewholder = viewholder;
viewholder.iview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Dialog builder = new Dialog(listdisplay, android.R.style.Theme_DeviceDefault);
builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
builder.getWindow().setBackgroundDrawable(
new ColorDrawable(Color.BLACK));
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialogInterface) {
//nothing;
}
});
ImageView imageView = new ImageView(listdisplay);
// Glide.with(finalConvertView.getContext()).load(Limage)
// .diskCacheStrategy(DiskCacheStrategy.ALL).into(imageView);
final GlideBitmapDrawable bitmapDrawable = (GlideBitmapDrawable) finalViewholder.iview.getDrawable();
final Bitmap yourBitmap = bitmapDrawable.getBitmap();
imageView.setImageBitmap(yourBitmap);
p = new PhotoViewAttacher(imageView);
builder.addContentView(imageView, new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
builder.show();
}
});
I am using Glide library to load the image. I tried implement click listener inside the if(convertView == null) and outside as well. so where do i need to implement onclick inside adapter so that i will work?
Related
This is my Custom Adapter. I have three ImageViews here. I wanted to give onclick for every imageview.
First I have called a method when click on ImageView. In that method I need to replace with the another image. And also need to replace remaining two Imageview images also.
Now When I click on first imageview, method is calling and image also changing for that. But for Remaining two Imageviews, It is showing NULL POINTER EXCEPTION.
How to access those ImageViews in that method.
Any help would be appreciated.
public class CustomTestingProductsListAdapter extends BaseAdapter{
public CustomTestingProductsListAdapter(Context context, Activity activity, List<V_APP_PACK_QR_DET_INFO> pack_qr_det_infos) {
layoutInFlater = LayoutInflater.from(context);
this.pack_qr_det_infos = pack_qr_det_infos;
this.curActivity = activity;
this.mContext = (VilanApplication) VilanApplication.mContext;
}
#Override
public int getCount() {
return pack_qr_det_infos.size();
}
#Override
public Object getItem(int position) {
return pack_qr_det_infos.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
try{
CustomTestingProductsListAdapter.ViewHolder holder;
if(convertView == null){
holder = new CustomTestingProductsListAdapter.ViewHolder();
convertView = layoutInFlater.inflate(R.layout.tst_ver_prdouct_list_items,null);
holder.tvProduct = convertView.findViewById(R.id.tv_qc_productName);
holder.DLSno = convertView.findViewById(R.id.tvtst_DLSno);
holder.iv_tst_received = convertView.findViewById(R.id.iv_tst_received);
holder.iv_tst_not_received = convertView.findViewById(R.id.iv_tst_not_received);
holder.iv_tst_wrong_product = convertView.findViewById(R.id.iv_tst_wrong_product);
convertView.setTag(holder);
}else{
holder = (CustomTestingProductsListAdapter.ViewHolder) convertView.getTag();
}
holder.DLSno.setText(pack_qr_det_infos.get(position).PRODUCTSLNO);
holder.iv_tst_received.setOnClickListener(onClickListener(position));
}catch (Exception e){
Log.d(VilanConstants.TAG,"/Excp #listAdp"+ e.toString());
}
return convertView;
}
private View.OnClickListener onClickListener(final int position){
return new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
CustomTestingProductsListAdapter.ViewHolder holder;
holder = new CustomTestingProductsListAdapter.ViewHolder();
holder.iv_tst_received = v.findViewById(R.id.iv_tst_received);
holder.iv_tst_not_received = v.findViewById(R.id.iv_tst_not_received);
holder.iv_tst_wrong_product = v.findViewById(R.id.iv_tst_wrong_product);
Toast.makeText(mContext, "Item Received" , Toast.LENGTH_SHORT).show();
holder.iv_tst_received.setImageResource(R.drawable.ic_received_green);
holder.iv_tst_not_received.setImageResource(R.drawable.ic_not_received);
holder.iv_tst_wrong_product.setImageResource(R.drawable.ic_wrong_product);
Status = 0 ;
}catch (Exception e){
Log.e(VilanConstants.TAG,"/Excp due to"+e.toString());
}
}
};
}
public class ViewHolder{
private TextView tvProduct;
private TextView DLSno;
private ImageView iv_tst_received;
private ImageView iv_tst_not_received;
private ImageView iv_tst_wrong_product;
}
}
Update Your getView():
#Override
public View getView(int position, View convertView, ViewGroup parent) {
try{
CustomTestingProductsListAdapter.ViewHolder holder;
if(convertView == null){
holder = new CustomTestingProductsListAdapter.ViewHolder();
convertView = layoutInFlater.inflate(R.layout.tst_ver_prdouct_list_items,null);
holder.tvProduct = convertView.findViewById(R.id.tv_qc_productName);
holder.DLSno = convertView.findViewById(R.id.tvtst_DLSno);
holder.iv_tst_received = convertView.findViewById(R.id.iv_tst_received);
holder.iv_tst_not_received = convertView.findViewById(R.id.iv_tst_not_received);
holder.iv_tst_wrong_product = convertView.findViewById(R.id.iv_tst_wrong_product);
convertView.setTag(holder);
}else{
holder = (CustomTestingProductsListAdapter.ViewHolder) convertView.getTag();
}
holder.DLSno.setText(pack_qr_det_infos.get(position).PRODUCTSLNO);
holder.iv_tst_received.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
Toast.makeText(mContext, "Item Received" , Toast.LENGTH_SHORT).show();
holder.iv_tst_received.setImageResource(R.drawable.ic_received_green);
holder.iv_tst_not_received.setImageResource(R.drawable.ic_not_received);
holder.iv_tst_wrong_product.setImageResource(R.drawable.ic_wrong_product);
Status = 0 ;
}
});
}catch (Exception e){
Log.d(VilanConstants.TAG,"/Excp #listAdp"+ e.toString());
}
return convertView;
}
I have few images in the json API, and I managed to fetch those images using volley Library. I used recyclerview with an image adapter to display image views vertically in two columns, but I want to make one image big and display it as the first image that user can click on. Also that image will be changed each interval of time. Basically, the API will do all the backend task like setting the time and telling which image to be displayed on the top of the recyclerview if not all images must have the same size and be shown in 2 columns vertically.
I have square layout class for recyclerview. I just want to know how I can do this. Even the concept will be fine.
you will have to create two view one for small item other for the header item and in onBindViewHolder have to bind those accordingly the following code is an example of a news article app and should help.
public class AdapterNewsArticlesListWithHeader extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<NewsArticles> items = new ArrayList<>();
private Context ctx;
private NewsArticles header;
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;
private OnItemClickListener mOnItemClickListener;
public interface OnItemClickListener {
void onItemClick(View view, NewsArticles obj, int position);
}
public void setOnItemClickListener(final OnItemClickListener mItemClickListener) {
this.mOnItemClickListener = mItemClickListener;
}
// Provide a suitable constructor (depends on the kind of dataset)
public AdapterNewsArticlesListWithHeader(Context context, NewsArticles header, List<NewsArticles> items) {
this.items = items;
this.header = header;
ctx = context;
}
public class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView title;
public TextView short_content;
public TextView date;
public ImageView image;
public LinearLayout lyt_parent;
public ViewHolder(View v) {
super(v);
title = (TextView) v.findViewById(R.id.title);
short_content = (TextView) v.findViewById(R.id.short_content);
date = (TextView) v.findViewById(R.id.date);
image = (ImageView) v.findViewById(R.id.image);
lyt_parent = (LinearLayout) v.findViewById(R.id.lyt_parent);
}
}
class ViewHolderHeader extends RecyclerView.ViewHolder {
public TextView title;
public TextView date;
public ImageView image;
public LinearLayout lyt_parent;
public ViewHolderHeader(View v) {
super(v);
title = (TextView) v.findViewById(R.id.title);
date = (TextView) v.findViewById(R.id.date);
image = (ImageView) v.findViewById(R.id.image);
lyt_parent = (LinearLayout) v.findViewById(R.id.lyt_parent);
}
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_HEADER) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_header, parent, false);
return new ViewHolderHeader(v);
} else if (viewType == TYPE_ITEM) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_news, parent, false);
return new ViewHolder(v);
}
return null;
}
// Replace the contents of a view (invoked by the layout manager)
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof ViewHolderHeader) {
ViewHolderHeader vHeader = (ViewHolderHeader) holder;
vHeader.title.setText(header.getTitle());
vHeader.date.setText(header.getDate());
Picasso.with(ctx).load(header.getImage()).into(vHeader.image);
vHeader.lyt_parent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//TODO: do your thing
}
});
} else if (holder instanceof ViewHolder) {
final NewsArticles c = items.get(position);
ViewHolder vItem = (ViewHolder) holder;
vItem.title.setText(c.getTitle());
vItem.short_content.setText(c.getShort_content());
vItem.date.setText(c.getDate());
Picasso.with(ctx).load(c.getImage()).into(vItem.image);
vItem.lyt_parent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//TODO: do your thing
}
});
}
}
// need to override this method
#Override
public int getItemViewType(int position) {
if (isPositionHeader(position)) {
return TYPE_HEADER;
}
return TYPE_ITEM;
}
private boolean isPositionHeader(int position) {
return position == 0;
}
public NewsArticles getItem(int position) {
return items.get(position);
}
// Return the size of your dataset (invoked by the layout manager)
#Override
public int getItemCount() {
return items.size();
}
}
I'm new at android. and I want to load a new template which contains two button on a selected item of grid view object.
Is that possible.
I added a gridview to my project and by using base adapter a template was loaded to each item of gridview. But what I want is that when I clicked an item of gridview, I want to load a new template (layout) to the selected item.
THE PROBLEM WAS SOLVED, followings are the edited codes
Base Adapter
public class KategoriAdapter extends BaseAdapter{
private Context mContext;
private String[] categoryValues;
private Bitmap[] pictures;
//indicate that positon for new template
private int mNewTemplatePos = -1;
public KategoriAdapter(Context context, String[] categoryValues, Bitmap[] pictures) {
this.mContext = context;
this.categoryValues = categoryValues;
this.pictures = pictures;
}
//apply new template to positon
public void useNewTemplate(int pos) {
mNewTemplatePos =pos;
//notiy list that data has changed and the list will refresh ui itself.
notifyDataSetChanged();
}
#Override
public int getCount() {
return categoryValues.length;
}
#Override
public Object getItem(int possition) {
return null;
}
#Override
public long getItemId(int possition) {
return 0;
}
#Override
public View getView(int possition, View convertView, ViewGroup parent) {
final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int posId = mNewTemplatePos;
if (convertView == null){
if (mNewTemplatePos ==possition){
convertView = getNewTemplate(inflater,possition);
}else {
convertView = getNormalTemplate(inflater,possition);
}
}else {
if (posId==possition){
convertView = getNewTemplate(inflater,possition);
}else{
convertView = getNormalTemplate(inflater,possition);
}
}
return convertView;
}
private View getNormalTemplate(LayoutInflater inflater, int possition) {
final View grid = inflater.inflate(R.layout.kategoriler_list_item, null);
TextView cName = (TextView) grid.findViewById(R.id.grid_item_ad);
ImageView categoryPictures = (ImageView) grid.findViewById(R.id.grid_item_resim);
cName.setText(categoryValues[possition]);
categoryPictures.setImageBitmap(pictures[possition]);
return grid;
}
private View getNewTemplate(LayoutInflater inflater, int possition) {
final View grid = inflater.inflate(R.layout.kategori_secenek_template, null);
TextView cName = (TextView) grid.findViewById(R.id.grid_item_ad);
cName.setText(categoryValues[possition]);
Button btn_nesne_tani = (Button) grid.findViewById(R.id.btn_nesneleri_taniyalim);
Button btn_cumle_kur = (Button) grid.findViewById(R.id.btn_cumle_kuralim);
btn_nesne_tani.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(mContext,"nesne",Toast.LENGTH_SHORT).show();
}
});
btn_cumle_kur.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(mContext,"cümle",Toast.LENGTH_SHORT).show();
}
});
return grid;
}
}
KategoriActivity.java
.....
final KategoriAdapter adapter = new KategoriAdapter(getApplicationContext(), mKategoriler, kategoriResimleri);
grid=(GridView)findViewById(R.id.gv_kategoriler);
grid.setAdapter(adapter);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
adapter.useNewTemplate(position);
Toast.makeText(getApplicationContext(), mKategoriler[position].toString(),Toast.LENGTH_SHORT).show();
}
});
}
I have rewrite your KategoriAdapter class:
public class KategoriAdapter extends BaseAdapter {
private Context mContext;
private final String[] categoryValues;
private final Bitmap[] pictures;
//indicate that positon in list are all use new template
private List<Integer> mNewTemplatePos;
public ImageView categoryPictures;
//indicate that this is normal template view
private final String NORMAL_TEMPLATE = "NORMAL_TEMPLATE";
//indicate that this is new template view
private final String NEW_TEMPLATE = "NEW_TEMPLATE";
public KategoriAdapter(Context context, String[] categoryValues, Bitmap[] pictures) {
this.mContext = context;
this.categoryValues = categoryValues;
this.pictures = pictures;
this.mNewTemplatePos = new ArrayList<>();
}
//apply new template to positon
public void useNewTemplate(int pos) {
mNewTemplatePos.add(pos);
//notiy list that data has changed and the list will refresh ui itself.
notifyDataSetChanged();
}
#Override
public int getCount() {
return categoryValues.length;
}
#Override
public Object getItem(int possition) {
return null;
}
#Override
public long getItemId(int possition) {
return 0;
}
#Override
public View getView(int possition, View convertView, ViewGroup parent) {
final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
if (mNewTemplatePos.contains(possition)) {
convertView = getNewTemplate(inflater, possition);
//use tag to indicate the type of the template
convertView.setTag(NEW_TEMPLATE);
} else {
convertView = getNormalTemplate(inflater, possition);
convertView.setTag(NORMAL_TEMPLATE);
}
} else {
switch ((String) convertView.getTag()) {
case NORMAL_TEMPLATE:
//convertView is the normal template view but you need a new template view in possition
if (mNewTemplatePos.contains(possition))
convertView = getNewTemplate(inflater, possition);
break;
case NEW_TEMPLATE:
//convertView is the new template view but you need a normal template view in possition
if (!mNewTemplatePos.contains(possition))
convertView = getNormalTemplate(inflater, possition);
break;
}
}
return convertView;
}
private View getNormalTemplate(LayoutInflater inflater, int possition) {
View grid = inflater.inflate(R.layout.kategoriler_list_item, null);
TextView cName = (TextView) grid.findViewById(R.id.grid_item_ad);
categoryPictures = (ImageView) grid.findViewById(R.id.grid_item_resim);
cName.setText(categoryValues[possition]);
categoryPictures.setImageBitmap(pictures[possition]);
return grid;
}
private View getNewTemplate(LayoutInflater inflater, int possition) {
// TODO: 31/08/16 inflate you new template view layout here
return youNewTemplateView;
}
}
You should determine wether if current contentView is the right template type in getView() because contentView may be one of the new template in your list when it is not null.It is convenient to use tag to indicate the template type.
When to use useNewTemplate(position)?
Just apply the position that you need to use new template to useNewTemplate() and use it in your onItemClick() method.
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
useNewTemplate(position);
}
});
I need to display the video in thumbnails style but i get no display, if anyone know what i'am done wrong please give me an idea to follow.
I get the idea in this tutorial
http:android-er.blogspot.com/2011/05/display-video-thumbnail-in-listview.html
public class RemarksAdapter extends ArrayAdapter<RemarksInformation> {
Context context;
LayoutInflater inflater;
List<RemarksInformation> remarksInformationList;
public RemarksAdapter(Context context, int resourceId, List<RemarksInformation> remarksInformationList) {
super(context, resourceId, remarksInformationList);
this.context = context;
this.remarksInformationList = remarksInformationList;
inflater = LayoutInflater.from(context);
}
static class RemarksHolder{
TextView remarks;
TextView image_name;
TextView image_path;
TextView video_name;
TextView video_path;
TextView date_send;
TextView time_send;
TextView remarks_by;
RelativeLayout image_wrapper;
RelativeLayout video_wrapper;
ImageView thumbnail;
}
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
RemarksHolder holder = null;
View v = convertView;
if(v == null){
v = inflater.inflate(R.layout.item_view_remarks, null);
holder = new RemarksHolder();
holder.remarks = (TextView)v.findViewById(R.id.txt_remarks);
holder.date_send = (TextView)v.findViewById(R.id.txt_date_send);
holder.time_send = (TextView)v.findViewById(R.id.txt_time_send);
holder.image_name = (TextView)v.findViewById(R.id.image_name);
holder.image_path = (TextView)v.findViewById(R.id.image_path);
holder.video_name = (TextView)v.findViewById(R.id.video_name);
holder.video_path = (TextView)v.findViewById(R.id.video_path);
holder.remarks_by = (TextView)v.findViewById(R.id.remarks_by);
holder.image_wrapper = (RelativeLayout)v.findViewById(R.id.image_wrapper);
holder.video_wrapper = (RelativeLayout)v.findViewById(R.id.video_wrapper);
holder.thumbnail = (ImageView)v.findViewById(R.id.vid);
v.setTag(holder);
}else{
holder = (RemarksHolder)v.getTag();
}
holder.remarks.setText(remarksInformationList.get(position).getRemarks());
holder.image_name.setText(remarksInformationList.get(position).getImage_name());
holder.image_path.setText(remarksInformationList.get(position).getImage_path());
holder.video_name.setText(remarksInformationList.get(position).getVideo_name());
holder.video_path.setText(remarksInformationList.get(position).getVideo_path());
Bitmap bmThumbnail = ThumbnailUtils.createVideoThumbnail(remarksInformationList
.get(position).getVideo_path(), Thumbnails.MICRO_KIND);
holder.thumbnail.setImageBitmap(bmThumbnail); //Get no display
holder.date_send.setText(remarksInformationList.get(position).getDate_send());
holder.time_send.setText(remarksInformationList.get(position).getTime_send());
holder.remarks_by.setText(remarksInformationList.get(position).getRemarks_by());
holder.image_wrapper.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((ListView) parent).performItemClick(v, position, 0);
}
});
holder.video_wrapper.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((ListView) parent).performItemClick(v, position, 0);
}
});
return v;
}
Could anybody explain me, how to realize?
I have an activity with listview and footer with some elements(textview).
Listview built with custom adapter. Each listview item has few elements. And my question: how can i change textview in footer, from custom adapter, when i clicking on some listview's element?
Thx a lot!
/**** My adapter ****/
public class MyListAdapter extends ArrayAdapter<Product> implements UndoAdapter {
private final Context mContext;
private HashMap<Product, Integer> mIdMap = new HashMap<Product, Integer>();
ArrayList<Product> products = new ArrayList<Product>();
final int INVALID_ID = -1;
LayoutInflater lInflater;
String imagePath;
public MyListAdapter(Context context, int textViewResourceId, List<Product> prod) {
//super(context, textViewResourceId, prod);
super(prod);
lInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mContext = context;
for (int i = 0; i < prod.size(); i++) {
//add(prod.get(i));
mIdMap.put(prod.get(i),i);
}
}
#Override
public long getItemId(final int position) {
//return getItem(position).hashCode();
Product item = (Product) getItem(position);
return mIdMap.get(item);
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
ViewHolder holder = null;;
Product p = getItem(position);
if (convertView == null) {
convertView = lInflater.inflate(R.layout.item, null);
//convertView.setBackgroundResource(R.drawable.rounded_corners);
int currentTheme = Utils.getCurrentTheme(convertView.getContext());
switch (currentTheme) {
case 0:
convertView.setBackgroundResource(R.drawable.rounded_corners);
break;
case 1:
convertView.setBackgroundResource(R.drawable.border);
break;
default:
convertView.setBackgroundResource(R.drawable.rounded_corners);
break;
}
holder = new ViewHolder();
holder.tvDescr = (TextView) convertView.findViewById(R.id.tvDescr);
holder.list_image = (ImageView) convertView.findViewById(R.id.list_image);
holder.products_amount = (TextView) convertView.findViewById(R.id.amountDigits);
holder.products_price = (TextView) convertView.findViewById(R.id.priceDigits);
holder.ivImage = (ImageView) convertView.findViewById(R.id.ivImage);
holder.unit = (TextView) convertView.findViewById(R.id.unit);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(p.getProductImageBitmap() != null && p.getProductImageBitmap() != "") {
Log.d("PATH -- ", p.getProductImageBitmap());
ImageLoader imageLoader = ImageLoader.getInstance();
DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
.resetViewBeforeLoading(true)
.showImageForEmptyUri(R.drawable.ic_launcher)
.showImageOnFail(R.drawable.ic_launcher)
/*.showImageOnLoading(R.id.progress_circular)*/
.build();
imageLoader.displayImage(p.getProductImageBitmap(), holder.list_image, options);
} else {
holder.list_image.setImageResource(R.drawable.ic_launcher);
}
holder.tvDescr.setText(p.getProductName());
holder.ivImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String deletedItem = getItem(position).getProductName();
MyListAdapter.this.remove(getItem(position));
if (MyListAdapter.this.getCount() > 0) {
Toast.makeText(mContext, deletedItem + " " + mContext.getString(R.string.deleted_item), Toast.LENGTH_SHORT).show();
MyListAdapter.this.notifyDataSetChanged();
} else {
Toast.makeText(mContext,mContext.getString(R.string.sklerolist_empty), Toast.LENGTH_SHORT).show();
}
}
});
//Функционал для большой картинки продукта
//открывается новое активити с большой картинкой
holder.list_image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
imagePath = getItem(position).getProductImageBitmap();
if(imagePath != null && imagePath != "") {
Pattern normalPrice = Pattern.compile("^file");
Matcher m2 = normalPrice.matcher(imagePath);
if (m2.find()) {
Intent myIntent = new Intent(view.getContext(), ViewImage.class).putExtra("imagePath", imagePath);
view.getContext().startActivity(myIntent);
}
}
}
});
holder.products_price.setText(fmt(p.getProductPrice()));
holder.products_amount.setText(fmt(p.getProductAmount()));
holder.unit.setText(p.getProductUnit());
return convertView;
}
public static String fmt(double d){
if(d == (long) d)
return String.format("%d",(long)d);
else
return String.format("%s",d);
}
static class ViewHolder {
ImageView list_image;
TextView tvDescr;
TextView products_amount;
TextView products_price;
TextView unit;
ImageView ivImage;
ProgressBar circleProgress;
}
#NonNull
#Override
public View getUndoView(final int position, final View convertView, #NonNull final ViewGroup parent) {
View view = convertView;
if (view == null) {
//view = LayoutInflater.from(mContext).inflate(R.layout.undo_row, parent, false);
view = lInflater.inflate(R.layout.undo_row, parent, false);
}
return view;
}
#NonNull
#Override
public View getUndoClickView(#NonNull final View view) {
return view.findViewById(R.id.undo_row_undobutton);
}
public View getHeaderView(final int position, final View convertView, final ViewGroup parent) {
TextView view = (TextView) convertView;
//View view = convertView;
if (view == null) {
//view = (TextView) LayoutInflater.from(mContext).inflate(R.layout.list_header, parent, false);
//view = lInflater.inflate(R.layout.list_header, parent, false);
}
//view.setText(mContext.getString(R.string.header, getHeaderId(position)));
return view;
}
public long getHeaderId(final int position) {
return position / 10;
}
}
Your ListView has a listener for the click events on list elements.
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Do something when a list item is clicked
}
But if you want to pas something else back from the adapter to the Activity or the Fragment that contains that ListView and Adapter, you should create a simple interface and set it as listener to your adapter. After that, set click events on your rows from within the adapter, and notify the Activity or Fragment using your own interface.
For example you have the interface defined like this
public interface OnItemClickedCustomAdapter {
public void onClick(ItemPosition position);
}
and in your Adapter class you will have a private member
private OnItemClickedCustomAdapter mListener;
and a method used to set the listener
public void setOnItemClickedCustomAdapter(OnItemClickedCustomAdapter listener){
this.mListener = listener;
}
From your Activity or Fragment where your ListView is defined, and your adapter is set, you will be able to call setOnItemClickedCustomAdapter with this as parameter, and there you go. Your activity will now listen for your events. To trigger an event, just call mListener.onClick() from your custom adapter. You can pass back data you need back to the Activity or Fragment, and from there you have access to your Header or Footer directly, and you can change the text on them.