ArrayAdapter getView doesn't return first item - android

In getView method when the view type is vTypeItem it should return all the item from my array list but it returns all except the first one,and because of this the ListView displays all item except the first one
public class DrawerAdapter extends ArrayAdapter<DrawerCustomList> {
private static final int vTypeHeader = 0;
private static final int vTypeItem = 1;
ArrayList<DrawerCustomList> mList = new ArrayList<>();
public DrawerAdapter(Context context, ArrayList<DrawerCustomList> list) {
super(context,0,list);
mList = list;
}
#Override
public int getItemViewType(int position) {
return (position==0)?vTypeHeader:vTypeItem;
}
#Override
public int getViewTypeCount() {
return 2;
}
#Override
public int getCount() {
return mList.size();
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
View v = convertView;
MyViewHolder mViewHolder = null;
if (v==null){
if(type==vTypeItem){
v = LayoutInflater.from(getContext()).inflate(R.layout.drawer_single_item,parent,false);
mViewHolder= new MyViewHolder(v,type);
DrawerCustomList val = mList.get(position);
mViewHolder.itemImage.setImageResource(val.getmListImage());
mViewHolder.itemName.setText(val.getmListName());
}else {
v = LayoutInflater.from(getContext()).inflate(R.layout.drawer_header_layout,parent,false);
mViewHolder= new MyViewHolder(v,type);
}
}
v.setTag(mViewHolder);
return v;
}
public static class MyViewHolder{
TextView itemName,headerName,headerEmail;
CircleImageView headerPicture;
ImageView itemImage;
public MyViewHolder(View v,int type){
if(type==vTypeHeader){
headerName = (TextView)v.findViewById(R.id.drawerHeaderName);
headerEmail = (TextView)v.findViewById(R.id.drawerHeaderEmail);
headerPicture = (CircleImageView)v.findViewById(R.id.drawerCircleImageView);
}else {
itemName = (TextView) v.findViewById(R.id.drawerItemName);
itemImage = (ImageView)v.findViewById(R.id.drawerItemImage);
}
}
}
}

Seems like you're not accounting for the header view in the getCount() method.
First change the latter to:
#Override
public int getCount() {
return mList.size() + 1; // accounting for the header
}
Now keep in mind that in the current version of your getView() method, if position == 0 you'll display the header, which is correct. If position == 1 though, you'll do
....
if(type==vTypeItem){
v = LayoutInflater.from(getContext()).inflate(R.layout.drawer_single_item,parent,false);
mViewHolder= new MyViewHolder(v,type);
DrawerCustomList val = mList.get(position);
Notice how mList.get(position) will actually get the second item in the list (with index 1), instead of the first one, which is what you need. So change this line to ...mList.get(position - 1) and your problem should be solved.

I changed your code as below, seems to be working fine. Here I have added drawer_single_item layout's drawerItemName,drawerItemImage control in drawer_header_layout.
Do the following changes in getView method.
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
View v = convertView;
MyViewHolder mViewHolder = null;
if(type==vTypeItem){
v = LayoutInflater.from(getContext()).inflate(R.layout.drawer_single_item,parent,false);
mViewHolder= new MyViewHolder(v,type);
DrawerCustomList val = mList.get(position);
mViewHolder.itemImage.setImageResource(val.getmListImage());
mViewHolder.itemName.setText(val.getmListName());
}else {
v = LayoutInflater.from(getContext()).inflate(R.layout.drawer_header_layout,parent,false);
mViewHolder= new MyViewHolder(v,type);
DrawerCustomList val = mList.get(position);
mViewHolder.itemImage.setImageResource(val.getmListImage());
mViewHolder.itemName.setText(val.getmListName());
}
v.setTag(mViewHolder);
return v;
}
Change your MyViewHolder to this:
public static class MyViewHolder{
TextView itemName,headerName,headerEmail;
CircleImageView headerPicture;
ImageView itemImage;
public MyViewHolder(View v,int type){
if(type==vTypeHeader){
headerName = (TextView)v.findViewById(R.id.drawerHeaderName);
headerEmail = (TextView)v.findViewById(R.id.drawerHeaderEmail);
headerPicture = (CircleImageView)v.findViewById(R.id.drawerCircleImageView);
itemName = (TextView) v.findViewById(R.id.drawerItemName);
itemImage = (ImageView)v.findViewById(R.id.drawerItemImage);
}else {
itemName = (TextView) v.findViewById(R.id.drawerItemName);
itemImage = (ImageView)v.findViewById(R.id.drawerItemImage);
}
}
}
Hope this helps.

Related

Because textview product counter is repeated in grid view using BaseAdapter

Because it repeats the product counter which is a TextView; I have the following scenario:
The user selects the product, and applies the quantity.
From the Activity and not from the Adapter I show the quantity of the selected product as seen in the image.
Everything works perfect, until under the screen and the counter magically passes to another product, I have implemented these methods by suggestion:
#Override
public long getItemId(int position)
{
return position;
}
#Override
public int getItemViewType(int position)
{
return position;
}
and still I still get this error. I have also tried to modify the counter from the Adapter but the same thing happens. I want that counter to stay fixed and not update when under the screen.
Adapter:
public class ProductoAdapter extends BaseAdapter
{
private Activity activity;
private LayoutInflater inflater;
private ArrayList<Producto> listadoProductos;
private ArrayList<Producto> productosPedidos;
Producto producto;
Empresas pedido;
public final int TYPE_NOTICIA=0;
public final int TYPE_LOAD=1;
private gestionSharedPreferences sharedPreferences;
private Context context;
/*
OnLoadMoreListener loadMoreListener;
*/
boolean isLoading=false, isMoreDataAvailable=true;
vars vars;
public static int contador;
public static int i;
public static int contadorGeneral;
private NumberFormat numberFormat;
ImageLoader imageLoader = ControllerSingleton.getInstance().getImageLoader();
public ProductoAdapter(Activity activity, ArrayList<Producto> listadoProductos)
{
this.activity=activity;
this.listadoProductos=listadoProductos;
productosPedidos=new ArrayList<Producto>();
vars=new vars();
sharedPreferences=new gestionSharedPreferences(this.activity);
contador=0;
contadorGeneral=0;
producto=new Producto();
/* LocalBroadcastManager.getInstance(context).registerReceiver(mMessageReceiver,
new IntentFilter("custom-message"));*/
}
/* public BroadcastReceiver mMessageReceiver = new BroadcastReceiver()
{
#Override
public void onReceive(Context context, Intent intent)
{
// Get extra data included in the Intent
producto = (Producto) intent.getSerializableExtra("producto");
Toast.makeText(context, producto.getNombreProducto()+producto.getNumeroDeProducto() ,Toast.LENGTH_SHORT).show();
}
};*/
#Override
public int getCount()
{
return listadoProductos.size();
}
#Override
public Producto getItem(int position)
{
return listadoProductos.get(position);
}
#Override
public long getItemId(int position)
{
return position;
}
#Override
public int getItemViewType(int position)
{
return position;
}
#Override
public View getView(int position, View view, ViewGroup viewGroup)
{
numberFormat = NumberFormat.getNumberInstance(Locale.GERMAN);
if (view == null)
{
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.producto_row_layout, viewGroup, false);
Log.i("martin","null");
}
else
{
Log.i("martin","llenoooooooooo");
}
final Producto producto = getItem(position);
ImageView imagenProducto = (ImageView) view.findViewById(R.id.imagenProducto);
TextView nombreProducto = (TextView) view.findViewById(R.id.nombreProducto);
TextView cantidadProducto = (TextView) view.findViewById(R.id.cantidadProducto);
TextView precioGeneralProducto = (TextView) view.findViewById(R.id.precioGeneralProducto);
TextView precioPideProducto = (TextView) view.findViewById(R.id.precioPideProducto);
TextView ahorroProducto = (TextView) view.findViewById(R.id.ahorroProducto);
if (producto.getImagenProducto().toString().equals("http://fasttrackcenter.com/pide/app/"))
{
imagenProducto.setImageResource(R.drawable.ic_not_image_found);
}
else
{
Glide.with(activity).
load(producto.getImagenProducto().toString()).
thumbnail(0.5f).into(imagenProducto);
}
nombreProducto.setText(producto.getNombreProducto()+" x "+producto.getCantidadProducto()+" "+producto.getUnidadProducto());
//cantidadProducto.setText(producto.getCantidadProducto());
precioGeneralProducto.setText("$"+numberFormat.format(Double.parseDouble(producto.getPrecioGeneralProducto())));
precioGeneralProducto.setPaintFlags(precioGeneralProducto.getPaintFlags()| Paint.STRIKE_THRU_TEXT_FLAG);
precioPideProducto.setText("$"+numberFormat.format(Double.parseDouble(producto.getPrecioPideProducto())));
int valorahorro=(Integer.parseInt(producto.getPrecioGeneralProducto()))-(Integer.parseInt(producto.getPrecioPideProducto()));
ahorroProducto.setText(""+"$"+numberFormat.format(Double.parseDouble(""+valorahorro)));
return view;
}
}
Activity:
I use a TextView as a counter which is initially invisible, but when it is selected, it is visible with the quantity of product selected.
From the activity I use this method:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Producto producto = (Producto) parent.getItemAtPosition(position);
final View contadorImagenProductoBolsa = Pedidos.this.findViewById(R.id.contadorProductoImagenBolsa);
TextView test1TextView = (TextView) view.findViewById(R.id.contadorProductoImagenBolsa);
mostrarDialogoDetalle(test1TextView,position,producto.getIdProducto(),producto.getImagenProducto(), producto.getNombreProducto(),
producto.getCantidadProducto(),producto.getUnidadProducto(), producto.getPrecioGeneralProducto(),producto.getPrecioPideProducto(),
producto.getAhorroProducto());
}
Has anyone had this happen? how did they solve it?
Here it is perfect, but when I go under the screen and I go back and up, observe how the counter happens to another product inexplicably:
I want an effective help and a possible snippet if it is the case of an incorrect adapter or activity.
Thank you.
I think You have to add ViewHolder For ProductoAdapter Like:
class ViewHolder
{
TextView nombreProducto,cantidadProducto
,precioGeneralProducto,precioPideProducto,ahorroProducto;
ImageView imagenProducto;
public ViewHolder(View view){
imagenProducto = (ImageView) view.findViewById(R.id.imagenProducto);
nombreProducto = (TextView) view.findViewById(R.id.nombreProducto);
cantidadProducto = (TextView) view.findViewById(R.id.cantidadProducto);
precioGeneralProducto = (TextView) view.findViewById(R.id.precioGeneralProducto);
precioPideProducto = (TextView) view.findViewById(R.id.precioPideProducto);
ahorroProducto = (TextView) view.findViewById(R.id.ahorroProducto);
}
}
Use setTag and getTag For ViewHolder Like:
ViewHolder holder;
if (view == null)
{
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.producto_row_layout, viewGroup, false);
holder = new ViewHolder();
Log.i("martin","null");
}
else
{ holder = (ViewHolder) view.getTag();
Log.i("martin","llenoooooooooo");
}
final Producto producto = getItem(position);
if (producto.getImagenProducto().toString().equals("http://fasttrackcenter.com/pide/app/"))
{
holder.imagenProducto.setImageResource(R.drawable.ic_not_image_found);
}
else
{
Glide.with(activity).
load(producto.getImagenProducto().toString()).
thumbnail(0.5f).into(holder.imagenProducto);
}
holder.nombreProducto.setText(producto.getNombreProducto()+" x "+producto.getCantidadProducto()+" "+producto.getUnidadProducto());
.
.
.
view.setTag(holder);
return view;
i hope this is help you

In GridView,item width and height is code control,but ImageView for first item no response OnClickListener

This is my use adapter,when I click first item‘s headImageView, I don't skip OtherUserActivity, I click second item’s headImageView, open many OtherUserActivity。I remove LayoutParams,click normal
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LoopDetailsDataList loopDetailsDataList = list.get(position);
ViewHolder holder = ViewHolder.get(context, convertView, parent, R.layout.item_loop_details, position);
convertView = holder.getConvertView();
int itemParams = screenWidth / 2 - Utils.newInstance(context).dip2px(10);
AbsListView.LayoutParams params = new AbsListView.LayoutParams(itemParams, itemParams);
convertView.setLayoutParams(params);
ImageView mainImageView = holder.getView(R.id.item_loop_details_image);
CircleImageView headImageView = holder.getView(R.id.item_loop_details_head_image);
TextView userName = holder.getView(R.id.item_loop_details_user_name);
TextView title = holder.getView(R.id.item_loop_details_title);
TextView praise = holder.getView(R.id.item_loop_details_praise);
TextView comments = holder.getView(R.id.item_loop_details_comments);
headImageView.setTag(position);
headImageView.setOnClickListener(new MyClickListener(position));
return convertView;
}
private class MyClickListener implements View.OnClickListener {
private int index;
private MyClickListener(int index) {
this.index = index;
}
#Override
public void onClick(View v) {
String userSign = list.get(index).getaCreateUserSign();
Log.i(TAG, "CreateUserSign:" + userSign);
Intent intent = new Intent(context, OtherUserActivity.class);
intent.putExtra("userSign", userSign);
context.startActivity(intent);
}
}
this is my ViewHolder
private ViewHolder(Context context, ViewGroup parent, int layoutId,
int position) {
this.mPosition = position;
this.mViews = new SparseArray<>();
mConvertView = LayoutInflater.from(context).inflate(layoutId, parent, false);
// setTag
mConvertView.setTag(this);
}
public static ViewHolder get(Context context, View convertView, ViewGroup parent, int layoutId, int position) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder(context, parent, layoutId, position);
} else {
holder = (ViewHolder) convertView.getTag();
holder.mPosition = position;
}
return holder;
}
public View getConvertView() {
return mConvertView;
}
#SuppressWarnings("unchecked")
public <T extends View> T getView(int viewId) {
View view = mViews.get(viewId);
if (view == null) {
view = mConvertView.findViewById(viewId);
mViews.put(viewId, view);
}
return (T) view;
}
public ViewHolder setText(int viewId, String text) {
TextView view = getView(viewId);
view.setText(text);
return this;
}
public ViewHolder setImageResource(int viewId, int drawableId) {
ImageView view = getView(viewId);
view.setImageResource(drawableId);
return this;
}
public ViewHolder setImageBitmap(int viewId, Bitmap bm) {
ImageView view = getView(viewId);
view.setImageBitmap(bm);
return this;
}
public ViewHolder setImageByUrl(int viewId, String url) {
ImageLoaderUtils.getInstance(3, ImageLoaderUtils.Type.LIFO).loadImage(url,
(ImageView) getView(viewId));
return this;
}
public int getPosition() {
return mPosition;
}
Well you shouldn't change the width of your view to half the screen because if you are using a gridview it will take care of that for you if you specify the the number of columns or it will set it to its default 2 columns. I think that there are some overlapping views due to resizing, so hopefully that will solve it.
Also you should do two other things to your code:
First you need to put holder = (ViewHolder) convertView.getTag();out side the if else because you do it no matter what, thats for convention sake.
Second you are not setting the mConvertView in your holder get method in the else block.

android setOnItemClickListener not working on one row but ok in other row, using 2 row layouts

Im using a listView that have 2 layouts for the rows on one row the
setOnItemClickListener
but on the other row it doesnt recognize the taps,
public void initItemTable()
{
listViewItem = (ListView) getView().findViewById(R.id.listViewItem);
listViewItem.setAdapter(new PhoneItemAdapter(new ItemPhoneDataSource().getItems()));
listViewItem.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Log.d("mensa", "item index :"+arg2);
}
});
}
//
private class PhoneItemAdapter extends BaseAdapter {
final List<RowPhone> rows;//row
//data source, style
PhoneItemAdapter(List<ItemPhone> animals) {
rows = new ArrayList<RowPhone>();//member variable
//choose cell! iterator
for (ItemPhone item : animals) {
if (item.getType().equals("item")) {
rows.add(new RowItemPhone(LayoutInflater.from(getActivity()), item));
} else {
rows.add(new RowFolderPhone(LayoutInflater.from(getActivity()), item)); //imageRow!
}
}
}
//delegate
#Override
public int getViewTypeCount() {
return RowTypePhone.values().length;
}
#Override
public int getItemViewType(int position) {
//con cast
return ((RowPhone) rows.get(position)).getViewType();
}
public int getCount() {
return rows.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
//cambiado con cast!
return ((RowPhone) rows.get(position)).getView(convertView);
}
}
So the tap is detected when "item.getType()" is folder but not in item,
. Shall I include the code for RowItemPhone.java and RowFolderPhone.java?
so how to fix this tap problem?
thanks!
edit 1. RowPhone.java
public interface RowPhone {
public View getView(View convertView);
public int getViewType();
}
edit 2. RowFolder.java .. the one that have the tap detecting fine:
public class RowItemPhone implements RowPhone{
private final ItemPhone item;
private final LayoutInflater inflater;
public RowItemPhone(LayoutInflater inflater, ItemPhone animal) {
this.item = animal;
this.inflater = inflater;
}
//text doble
public View getView(View convertView) {
ViewHolder holder;
View view = null;
if (convertView == null) {
//ViewGroup viewGroup = (ViewGroup)inflater.inflate(R.layout.row_item_phone, null);
ViewGroup viewGroup = (ViewGroup)inflater.inflate(R.layout.row_item_phone, (ViewGroup) view, false);
holder = new ViewHolder(
(ImageView)viewGroup.findViewById(R.id.image_item),
(TextView)viewGroup.findViewById(R.id.title),
(TextView)viewGroup.findViewById(R.id.description));
viewGroup.setTag(holder); //pa q y como usa tag!
view = viewGroup;
} else {
view = convertView;
holder = (ViewHolder)convertView.getTag();
}
//setup
holder.imageView.setImageResource(item.getImageId());
holder.descriptionView.setText(item.getDescription());
holder.titleView.setText(item.getName());
return view;
}
public int getViewType() {
return RowTypePhone.DESCRIPTION_ROW.ordinal();
}
private static class ViewHolder {
final ImageView imageView;
final TextView titleView;
final TextView descriptionView;
private ViewHolder(ImageView imageView, TextView titleView, TextView descriptionView) {
this.imageView = imageView;
this.titleView = titleView;
this.descriptionView = descriptionView;
}
}
}
And here the RowFolderPhone.java ... showing fine, but not detecting taps:
public class RowFolderPhone implements RowPhone {
private final ItemPhone item;
private final LayoutInflater inflater;
public RowFolderPhone(LayoutInflater inflater, ItemPhone animal) {
this.item = animal;
this.inflater = inflater;
}
public View getView(View convertView) {
ViewHolder holder;
View view = null;
//we have a don't have a converView so we'll have to create a new one
if (convertView == null) {
// ViewGroup viewGroup = (ViewGroup)inflater.inflate(R.layout.row_folder_phone, null);
ViewGroup viewGroup = (ViewGroup)inflater.inflate(R.layout.row_folder_phone, (ViewGroup) view, false);
//use the view holder pattern to save of already looked up subview
holder = new ViewHolder((ImageView)viewGroup.findViewById(R.id.image),
(TextView)viewGroup.findViewById(R.id.title));
viewGroup.setTag(holder);
view = viewGroup;
} else {
//get the holder back out
holder = (ViewHolder)convertView.getTag();
view = convertView;
}
//actually setup the view
holder.imageView.setImageResource(item.getImageId());
holder.titleView.setText(item.getName());
return view;
}
public int getViewType() {
return RowTypePhone.IMAGE_ROW.ordinal();
}
private static class ViewHolder {
final ImageView imageView;
final TextView titleView;
private ViewHolder(ImageView imageView, TextView titleView) {
this.imageView = imageView;
this.titleView = titleView;
}
}
}
There are something wrong with your getItem method on PhoneItemAdapter. getItem method should return list item not position. So you should replace your code to
public RowPhone getItem(int position) {
return rows.get(position);
}
the problem was solved by setting descendantFocusability
in : row_item_phone.xml
<RelativeLayout ...
android:descendantFocusability="blocksDescendants"

Android - Remove GridView item from inside getView()?

I need a way to remove an Item from the GridView but this must be done from within the getView() method inside my custom Adapter. Here is my GridView:
Activity:
...
String[] newList;
newList[0] = "Item 1";
newList[1] = "Item 2";
newList[2] = "Item 3";
...
GridView GV = (GridView) getActivity().findViewById(R.id.gv);
GV.setAdapter(new Adapter(getActivity(), newList));
GV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView arg0,
View arg1, int position, long arg3) {
...
}
});
Adapter
public class Adapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mInflator;
private String mEntries[];
public static class ViewHolder {
public TextView myTextView;
}
public Adapter (Context context, String[] entries) {
mContext = context;
mInflator = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mEntries = entries;
}
#Override
public int getCount() {
return mEntries.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null) {
convertView = mInflator.inflate(R.layout.gvitemlayout, parent, false);
holder = new ViewHolder();
holder.myTextView = (TextView) convertView.findViewById(R.list.removeItem);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
String string = mEntries[position];
String[] data = string.split("\\.");
if (data.length < 2) {
TextView itemName = (TextView) convertView.findViewById(R.list.itemName);
itemName.setText("");
TextView itemClass = (TextView) convertView.findViewById(R.favlist.itemClass);
itemClass.setText("");
holder.myTextView.setText("");
TextView itemNone = (TextView) convertView.findViewById(R.list.itemNone);
itemNone.setText("No Items");
} else {
TextView itemName = (TextView) convertView.findViewById(R.list.itemName);
itemName.setText(data[1]);
TextView itemClass = (TextView) convertView.findViewById(R.list.itemClass);
itemClass.setText(data[0]);
}
final int info = (Integer) getItem(position);
holder.myTextView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
SharedPreferences sP = mContext.getSharedPreferences("fav", mContext.MODE_PRIVATE);
Boolean b = sP.getBoolean(mEntries[info], false);
if (b == true) {
SharedPreferences.Editor editor = sP.edit();
editor.remove(mEntries[info]);
editor.commit();
// REMOVE ITEM CODE NEEDED HERE
}
}
});
return convertView;
}
}
Hope this makes it easier to understand what I need.
This is what I did, there was no need to initialise the 'adapter' variable. Inside the getView() simply do this:
Remove the Item from the list that was used as the data source.
So. if you want to remove Item 2 from the list
String mEntries[] = ITEM1, ITEM2, ITEM3, etc
Do this:
String newList[] = new String[mEntries.length - 1];
int count = 0;
for (int i = 0; i < mEntries.length; i++) {
if (mEntries.length - 1 > 0) {
if (mEntries[i] == mEntries[1]) { // mEntries[1] as the range starts from 0, so 1 would be ITEM2
// SKIP IF MATCHES THE ITEM YO WANT TO REMOVE
} else {
newList[count] = mEntries[i];
count++;
}
}
}
mEntries = newList; // save the new list into mEntries
notifyDataSetChanged(); // notify the changes and the listview/gridview will update
What you are asking for is very possible. You didn't give enough code for it to be clear exactly what's happening, but the code above you posted won't work unless the remove button is an entire view returned by getView in the adapter.
Your getView in your adapter will need to look something like this:
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.grid_view_item, null);
}
Button removeButton = (Button) convertView.findViewById(R.id.remove_button);
removeButton.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v) {
adapter.mThumbIdsList.remove(position);
adapter.notifyDataSetChanged();
}
});
return convertView;
}
Hope this helps. Good luck!

Change ImageView BackgroundResource in listview onItemSelected Android

I use an efficientAdapter to populate mylistview as shown below:
What you can do is use setTag() and getTag() to maintain the scrolling position and have a check that your ImageView of ListView row don't inter-change the resource.
Inside your Adapter's getView() class use
vi.setTag(R.id.btOnOFF, holder.btOnOFF);
And then fetch it inside onItemClick() using getTag()
ImageView imgview = (ImageView) view.getTag(R.id.btOnOFF);
imgview.setBackgroundResource(R.drawable.air_radio_button_rouge);
For further reference you can check my blog post
You can try something like this. I cleaned up and simplified your code.
public class EfficientAdapter extends BaseAdapter {
private Activity mActivity;
private ArrayList<SearchTracks> mSearchTracks;
private ResultatMultiple mResultatMultiple;
private int mSelectedPosition;
public EfficientAdapter(Activity activity, ArrayList<SearchTracks> searchTracks) {
mActivity = activity;
mSearchTracks = searchTracks;
}
public EfficientAdapter(ResultatMultiple resultatMultiple){
mResultatMultiple = resultatMultiple ;
}
public void setSelectedPosition(int position) {
mSelectedPosition = position;
}
#Override
public int getCount() {
return mSearchTracks.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder = null;
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater)mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.ecran_multiple_row, null);
holder = new ViewHolder();
holder.indexTextView = (TextView)view.findViewById(R.id.txIndex);
holder.titleTextView = (TextView)view.findViewById(R.id.txSTitle);
holder.buttonOnOffImageView = (ImageView)view.findViewById(R.id.btOnOFF);
view.setTag(holder);
}else {
holder = (ViewHolder)view.getTag();
}
int index = position + 1;
holder.indexTextView.setText((index <= 9 ? "0" : "") + Integer.toString(index));
holder.titleTextView.setText(mSearchTracks.get(position).getTitle());
if(position % 2 == 0) {
view.setBackgroundResource(R.drawable.listview_selector_odd);
}else {
view.setBackgroundResource(R.drawable.listview_selector_even);
}
if(mSearchTracks.size() == 1 || position == mSelectedPosition) {
holder.buttonOnOffImageView.setBackgroundResource(R.drawable.air_radio_button_rouge);
}else {
holder.buttonOnOffImageView.setBackgroundResource(R.drawable.air_deezer_check);
}
return view;
}
private class ViewHolder {
public TextView indexTextView;
public TextView titleTextView;
public ImageView buttonOnOffImageView;
}
}
Now in your onClick listener you can call listAdapter.setSelectedPosition(position); and then listAdapter.notifyDataSetChanged(); to reload your table.

Categories

Resources