class adapter in android studio - java

i have class adapter that contain image view and text view , i receive arraylist from the post excute then pass this array to the my adapter, when run my application it's give me this error.
java.lang.NullPointerException: Attempt to read from field 'android.widget.ImageView com.gmplatform.gmp.MainActivity_navagation_custmoer$itemAdapter3$ViewHolder.ivpic1' on a null object reference
at com.gmplatform.gmp.MainActivity_navagation_custmoer$itemAdapter3$ViewHolder.access$2600(MainActivity_navagation_custmoer.java:2410)
at com.gmplatform.gmp.MainActivity_navagation_custmoer$itemAdapter3.getView(MainActivity_navagation_custmoer.java:2372)
below here the adapter class
public class itemAdapter3 extends BaseAdapter{
private ArrayList<favouriteitems> itemCatalog;
private int resource;
private LayoutInflater inflater;
public itemAdapter3(Context context, int resource, List<favouriteitems> objects) {
// super(context, resource, objects);
itemCatalog = (ArrayList<favouriteitems>) objects;
this.resource = resource;
inflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
}
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
#Override
public int getCount() {
return itemCatalog.size();
}
#Override
public Object getItem(int position) {
return itemCatalog.get(position);
}
#Override
public long getItemId(int position) {
return itemCatalog.indexOf(getItem(position));
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final int pos = position;
final favouriteitems items = itemCatalog.get(pos);
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(resource, null);
holder.ivpic1 = (ImageView) convertView.findViewById(R.id.iv);
holder.st = (ImageView)convertView.findViewById(R.id.st);
holder.sh=(ImageView)convertView.findViewById(R.id.sh);
holder.name = (TextView) convertView.findViewById(R.id.t4);
holder.pr = (TextView) convertView.findViewById(R.id.textView32);
holder.av = (TextView) convertView.findViewById(R.id.av);
holder.t5 = (TextView) convertView.findViewById(R.id.t5);
final ViewHolder finalHolder = holder;
}
else {
holder = (MainActivity_navagation_custmoer.itemAdapter3.ViewHolder) convertView.getTag();
}
final ProgressBar progressBar = (ProgressBar) convertView.findViewById(R.id.progressBar);
// Then later, when you want to display image
ImageLoader.getInstance().displayImage(itemCatalog.get(position).getM1().getLoc2().getItmc().getIt().getPic(), holder.ivpic1 , new ImageLoadingListener() {
#Override
public void onLoadingStarted(String imageUri, View view) {
progressBar.setVisibility(View.VISIBLE);
}
#Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
progressBar.setVisibility(View.GONE);
}
#Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
progressBar.setVisibility(View.GONE);
}
#Override
public void onLoadingCancelled(String imageUri, View view) {
progressBar.setVisibility(View.GONE);
}
});
holder.pr.setText(String.valueOf(itemCatalog.get(position).getM1().getLoc2().getItmc().getPrice() + " SR"));
holder.name.setText(itemCatalog.get(position).getM1().getLoc2().getItmc().getIt().getName());
if (itemCatalog.get(position).getM1().getLoc2().getItmc().isInSc() == true) {
holder.sh.setImageResource(R.drawable.customern2);
} else {
holder.sh.setImageResource(R.drawable.customern1);
}
holder.t5.setText(itemCatalog.get(position).getM1().getmName()+" ("+itemCatalog.get(position).getM1().getLoc2().getArea()+" )");
return convertView;
}
class ViewHolder {
private ImageView ivpic1,sh,st;
private TextView name,pr,t5,av;
}
}

There is a problem in your Constructor:
Uncomment the super call and run it, you will stop getting null pointer exception.
public itemAdapter3(Context context, int resource, List<favouriteitems> objects) {
super(context, resource, objects);
itemCatalog = (ArrayList<favouriteitems>) objects;
this.resource = resource;
inflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
}

> holder = (ViewHolder) convertView.getTag();
you donot have a corrensponding convertView.setTag(holder) after the holder was created with holder = new ViewHolder();.

Related

Interact with view of a specific item in a custom ListView

I am using a custom BaseAdapter to create a custom list. What I'm trying to do is have a public function outside of getView(), in this example setName(), that can interact with a view of a specific item on the list.
For example calling setName(1); should make invisible the TextView of the second item on the list.
Thank you in advance.
public class ListAdapterNew extends BaseAdapter {
private int size;
private Context context;
private ViewHolder viewHolder;
String[][] data;
ArrayList<DataModel> listArray;
public ListAdapterNew(Context context, String[][] data, int size) {
listArray = new ArrayList<DataModel>(size);
for(int i=0;i<size;i++){
listArray.add(new DataModel(i));
}
this.context=context;
this.data=data;
this.size=size;
}
#Override
public int getCount() {
return listArray.size();
}
#Override
public Object getItem(int i) {
return listArray.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
public int getViewTypeCount() { return getCount(); }
#Override
public int getItemViewType(int position) {
return position;
}
private static class ViewHolder
{
TextView number,name;
}
#Override
public View getView(int index, View view, final ViewGroup parent) {
if (view == null) {
LayoutInflater li = (LayoutInflater) parent.getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
view = li.inflate(R.layout.main_user_list_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.number = view.findViewById(R.id.main_list_number);
viewHolder.name = view.findViewById(R.id.main_list_name);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
viewHolder.number.setText((index+1)+")");
viewHolder.name.setText(data[index][0]);
}
view.setClickable(true);
view.setFocusable(true);
return view;
}
public void setName(int index){
viewHolder.name.setVisibility(View.GONE);
}
}
The answer was simple. I use a public function inside the ListAdapterNew to update the data[][] and call notifyDataSetChanged(); on the adapter.
There is also a small change in the getView() of the adapter that allows the view to update when items become visible, as shown below.
if (view == null) {
LayoutInflater li = (LayoutInflater) parent.getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
view = li.inflate(R.layout.main_user_list_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.number = view.findViewById(R.id.main_list_number);
viewHolder.name = view.findViewById(R.id.main_list_name);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
if (view != null){
viewHolder.number.setText((index+1)+")");
viewHolder.name.setText(data[index][0]);
}
Function that updates the data
public void updateIcons(int index, int data){
this.icons[index]=data;
}

All checkbox in listview is not getting visible from invisible state when button is clicked from Mainactivity

I am new to android. I am struggling with the checkbox in listview. By default I made my checkbox in listview to be in INVISIBLE mode when the button is clicked from MainActivity. I want to all checkbox in listview to be in VISIBLE mode but when I am clicking the button check box is VISIBLE in only one list row in all other rows. Still the check box is in INVISIBLE mode.
FavouriteListAdapter
public class FavouriteListAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mInflater;
ArrayList<FavouritesArray> mArrFavourites;
ViewHolder viewHolder;
String favId;
//Make checkbox to visible in all rows in listview
public void setcheckbox() {
viewHolder.favCheckbox.setVisibility(View.VISIBLE);
}
public static class ViewHolder {
public TextView favName;
public TextView favUrl;
public TextView favAddress;
public CheckBox favCheckbox;
}
public FavouriteListAdapter(Context context, ArrayList<FavouritesArray> lArrFavourites) {
mContext = context;
mArrFavourites = lArrFavourites;
}
#Override
public int getCount() {
if (mArrFavourites == null) {
return 0;
}
return mArrFavourites.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = mInflater.inflate(R.layout.favouritelistadapter, parent, false);
viewHolder.favName = (TextView) convertView.findViewById(R.id.fav_name);
viewHolder.favUrl = (TextView) convertView.findViewById(R.id.fav_url);
viewHolder.favAddress = (TextView) convertView.findViewById(R.id.fav_address);
viewHolder.favCheckbox = (CheckBox) convertView.findViewById(R.id.fav_checkbox);
viewHolder.favCheckbox .setVisibility(View.INVISIBLE);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
final FavouritesArray chooseService = mArrFavourites.get(position);
viewHolder.favName.setText(chooseService.getFavouritename());
viewHolder.favAddress.setText(chooseService.getFavouritelocation());
viewHolder.favCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
favId = chooseService.getFavouritename();
((MainActivity) mContext).check(favId);
}
}
});
return convertView;
}}
I think you need boolean varibale showCheckBox and check it state in method getView.
Look at this sample:
private boolean showCheckBox = false;
public void showChecBox(boolean show) {
showCheckBox = show;
}
public View getView(final int position, View convertView, ViewGroup parent) {
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = mInflater.inflate(R.layout.favouritelistadapter, parent, false);
viewHolder.favName = (TextView) convertView.findViewById(R.id.fav_name);
viewHolder.favUrl = (TextView) convertView.findViewById(R.id.fav_url);
viewHolder.favAddress = (TextView) convertView.findViewById(R.id.fav_address);
viewHolder.favCheckbox = (CheckBox) convertView.findViewById(R.id.fav_checkbox);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
final FavouritesArray chooseService = mArrFavourites.get(position);
viewHolder.favName.setText(chooseService.getFavouritename());
viewHolder.favAddress.setText(chooseService.getFavouritelocation());
int checkBoxVisibility = showCheckBox : View.VISIBLE ? View.INVISIBLE;
viewHolder.favCheckbox.setVisibility(checkBoxVisibility);
viewHolder.favCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
favId = chooseService.getFavouritename();
((MainActivity) mContext).check(favId);
}
}
});
return convertView;
}}
when a button is clicked in activity you need to call adapter.showCheckBoxes(true) of your adapter. then set a field to showCheckBoxes=true; in that function and also refresh the list using notifyDataSetChanged() which will cause the getView method to be called for all the rows.
In getView check if showCheckBoxes is true or not and show check boxes accordingly.
public class FavouriteListAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mInflater;
ArrayList<FavouritesArray> mArrFavourites;
ViewHolder viewHolder;
String favId;
boolean showCheckBoxes = false;
//Make checkbox to visible in all rows in listview
public void setcheckbox() {
viewHolder.favCheckbox.setVisibility(View.VISIBLE);
}
public static class ViewHolder {
public TextView favName;
public TextView favUrl;
public TextView favAddress;
public CheckBox favCheckbox;
}
public FavouriteListAdapter(Context context, ArrayList<FavouritesArray> lArrFavourites) {
mContext = context;
mArrFavourites = lArrFavourites;
}
#Override
public int getCount() {
if (mArrFavourites == null) {
return 0;
}
return mArrFavourites.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = mInflater.inflate(R.layout.favouritelistadapter, parent, false);
viewHolder.favName = (TextView) convertView.findViewById(R.id.fav_name);
viewHolder.favUrl = (TextView) convertView.findViewById(R.id.fav_url);
viewHolder.favAddress = (TextView) convertView.findViewById(R.id.fav_address);
viewHolder.favCheckbox = (CheckBox) convertView.findViewById(R.id.fav_checkbox);
viewHolder.favCheckbox .setVisibility(View.INVISIBLE);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
final FavouritesArray chooseService = mArrFavourites.get(position);
viewHolder.favName.setText(chooseService.getFavouritename());
viewHolder.favAddress.setText(chooseService.getFavouritelocation());
if(showCheckBoxes){
viewHolder.favCheckbox .setVisibility(VISIBLE);
}else{
viewHolder.favCheckbox .setVisibility(INVISIBLE);
}
viewHolder.favCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
favId = chooseService.getFavouritename();
((MainActivity) mContext).check(favId);
}
}
});
return convertView;
}}
public void showCheckBoxes(boolean showCheckBoxes){
this.showCheckBoxes=showCheckBoxes;
notifyDataSetChanged();
}
in your button click function:
void onClick(View v){
mAdapter.showCheckBoxes(true);
}

Android. Java. RecyclerAdapter

I have two different card view in one layout. I need that The latest news is added to one card view, and the rest in another. For exapmle, on image
MyRecyclerAdapter code
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.CustomViewHolder> {
private List<FeedItem> feedItemList;
private Context mContext;
public MyRecyclerAdapter(Context context, List<FeedItem> feedItemList) {
this.feedItemList = feedItemList;
this.mContext = context;
}
#Override
public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_row, null);
CustomViewHolder viewHolder = new CustomViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(CustomViewHolder customViewHolder, int i) {
FeedItem feedItem = feedItemList.get(i);
//Download image using picasso library
Picasso.with(mContext).load(feedItem.getThumbnail())
.error(R.drawable.placeholder)
.placeholder(R.drawable.placeholder)
.into(customViewHolder.imageView);
//Setting text view title
customViewHolder.textView.setText(Html.fromHtml(feedItem.getTitle()));
View.OnClickListener clickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
CustomViewHolder holder = (CustomViewHolder) view.getTag();
int position = holder.getPosition();
FeedItem feedItem = feedItemList.get(position);
Toast.makeText(mContext, feedItem.getTitle(), Toast.LENGTH_SHORT).show();
}
};
//Handle click event on both title and image click
customViewHolder.textView.setOnClickListener(clickListener);
customViewHolder.imageView.setOnClickListener(clickListener);
customViewHolder.textView.setTag(customViewHolder);
customViewHolder.imageView.setTag(customViewHolder);
}
#Override
public int getItemCount() {
return (null != feedItemList ? feedItemList.size() : 0);
}
public class CustomViewHolder extends RecyclerView.ViewHolder {
protected ImageView imageView;
protected TextView textView;
public CustomViewHolder(View view) {
super(view);
this.imageView = (ImageView) view.findViewById(R.id.thumbnail);
this.textView = (TextView) view.findViewById(R.id.title);
}
}
}
The approach I would use if I were in you, to accomplish what you posted in the picture is to override getItemViewType and have two different types of ViewHolder, once for the header, the one with the big image and the other adapter for the small rows. onBindViewHolder gets the type as parameter. In your case is int i.
I write a quickdemo like this:
private class MyCustomAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
private ArrayList mData = new ArrayList();
private LayoutInflater mInflater;
private TreeSet mSeparatorSet = new TreeSet();
public MyCustomAdapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(String item) {
mData.add(item);
notifyDataSetChanged();
}
public void addSeparator(String item) {
mData.add(item);
mSeparatorSet.add(mData.size() - 1);
notifyDataSetChanged();
}
#Override
public int getItemViewType(int position) {
return mSeparatorSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
#Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
#Override
public int getCount() {
return mData.size();
}
#Override
public Object getItem(int position) {
return mData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
System.out.println("getView " + position + " " + convertView + "type " + type);
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.item1, null);
holder.textView = (TextView) convertView.findViewById(R.id.text_view);
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.item2, null);
holder.textView = (TextView) convertView.findViewById(R.id.textSeparator);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(mData.get(position) + "");
return convertView;
}
}
public static class ViewHolder {
public TextView textView;
}
or if you want to use header and footer, create headerView layout:
View header = (View) getLayoutInflater().inflate(R.layout.headerView,null);
listView.addHeaderView(header);

Android: navigation drawer with custom adapter onItemClickListener

I've been trying set custom ListView adapter for navigation drawer it derived from BaseAdapter so when I set OnItemClickListener it doesn't work on either ListView.OnItemClickListener and AdapterView.OnItemClickListener.
Googled around but all examples derive from ArrayAdapter since BaseAdapter desired.
Can anyone help me on this?
NavigationDrawer fragment onCreateView
drawerItems = new ArrayList<DrawerItem>();
drawerItems.add(DrawerItem.create(1, getString(R.string.ideas), getDrawable(R.drawable.ic_action_diamond)));
drawerItems.add(DrawerItem.create(2, getString(R.string.categories), getDrawable(R.drawable.ic_action_categories)));
drawerItems.add(DrawerItem.create(3, getString(R.string.priorities), getDrawable(R.drawable.ic_action_priorities)));
mDrawerListAdapter = MenuAdapter.newInstance(getActivity(), drawerItems);
mDrawerListView.setItemsCanFocus(true);
mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d(TAG, String.valueOf(position));
selectItem(position);
}
});
mDrawerListView.setAdapter(mDrawerListAdapter);
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
My custom adapter
public class MenuAdapter extends BaseAdapter {
private static final String TAG = "AD:MENU";
private final LayoutInflater mLayoutInflater;
private List<DrawerItem> items;
private Context context;
public MenuAdapter(Context ctx, List<DrawerItem> items) {
this.items = items;
this.context = ctx;
this.mLayoutInflater = LayoutInflater.from(ctx);
}
public static MenuAdapter newInstance(Context ctx, List<DrawerItem> items) {
return new MenuAdapter(ctx, items);
}
#Override
public int getCount() {
return items == null ? 0 : items.size();
}
#Override
public Object getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View v, ViewGroup parent) {
DrawerItem item = (DrawerItem) getItem(position);
ViewHolder holder;
if (v == null) {
v = mLayoutInflater.inflate(R.layout.drawable_item, parent, false);
holder = new ViewHolder();
if (v != null) {
holder.title = (TextView) v.findViewById(R.id.menu_title);
holder.title.setTag(item.id);
v.setTag(holder);
}
} else {
holder = (ViewHolder) v.getTag();
}
holder.title.setText(item.title);
holder.title.setCompoundDrawablesWithIntrinsicBounds(item.icon, null, null, null);
Log.d(TAG, item.title);
return v;
}
public class ViewHolder {
TextView title;
}
}

Get ListItem position in custom SimpleLayout

I have a custom ListView with a TextView and a CheckBox. I also have a custom SimpleAdapter in which I override the getView() method and am able to retrieve clicks on the TextView and CheckBox changes. My problem is that I don't know how to get the correct clicked ListItem or CheckBox inside the OnCheckedChanged or OnClick.
UPDATE added whole CustomAdapter class:
public class CustomAdapter extends SimpleAdapter {
private LayoutInflater mInflater;
private List<HashMap<String, String>> mItems = null;
private Context mContext;
private int mPosicion;
public CustomAdapter(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) {
super(context, items, resource, from, to);
mInflater = LayoutInflater.from(context);
mItems = items;
mContext = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
mPosicion = position;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_row_view, null);
holder = new ViewHolder();
holder.chkbxEstado = (CheckBox) convertView.findViewById(R.id.chkbxCompletado);
holder.txtTextoAgenda = (TextView) convertView.findViewById(R.id.txtTextoLista);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtTextoAgenda.setText(mItems.get(position).get("descripcion"));
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.i("Posicion",""+mPosicion);//I don't know how to retrieve clicked position
}
});
holder.chkbxEstado.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.i("Posicion",""+mPosicion);//Or checked
}
});
return convertView;
}
private static class ViewHolder {
TextView txtTextoAgenda;
CheckBox chkbxEstado;
}
}
Any help is appreciated.
I found a solution. If anybody knows a better one, please let me know. Working CustomAdapter class:
public class CustomAdapter extends SimpleAdapter {
private LayoutInflater mInflater;
private List<HashMap<String, String>> mItems = null;
private Context mContext;
private OnClickListener mClick;
private OnCheckedChangeListener mChecked;
public CustomAdapter(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) {
super(context, items, resource, from, to);
mInflater = LayoutInflater.from(context);
mItems = items;
mContext = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_row_view, null);
holder = new ViewHolder();
holder.chkbxEstado = (CheckBox) convertView.findViewById(R.id.chkbxCompletado);
holder.txtTextoAgenda = (TextView) convertView.findViewById(R.id.txtTextoLista);
holder.posicion = position; //Add the new position into the holder for each row.
convertView.setTag(holder);
mClick = new OnClickListener() {
#Override
public void onClick(View v) {
ViewHolder viewHolder = getViewHolder(v); //Get the ViewHolder for the clicked row.
Log.i("Posicion",""+v.posicion);
}
};
mChecked = new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
ViewHolder viewHolder = getViewHolder(buttonView); //Get the ViewHolder for the clicked CheckBox
Log.i("Posicion",""+viewHolder.posicion);
}
};
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtTextoAgenda.setText(mItems.get(position).get("descripcion"));
convertView.setOnClickListener(mClick);
holder.chkbxEstado.setOnCheckedChangeListener(mChecked);
return convertView;
}
public ViewHolder getViewHolder(View v){ //This method returns the ViewHolder stored in the tag if available, if not it recursively checks the parent's tag.
if(v.getTag() == null){
return getViewHolder((View)v.getParent());
}
return (ViewHolder)v.getTag();
}
private static class ViewHolder {
TextView txtTextoAgenda;
CheckBox chkbxEstado;
int posicion; //Added position attribute to ViewHolder class.
}
}
Edited to re-use onClickListener() and onCheckedChangedListener() instances.
The OP's solution works. But if you are extending a CursorAdapter, the position parameter does not exist in newView() and bindView(). What to do?
They do supply a Cursor. Use cursor.getPosition(). It does the same thing.

Categories