Wrong position in Custom Adapter - java

I have created a Custom Adapter to manage some Buttons inside each ListView item. If I scroll-down the list, and I click a button, the adapter take a wrong position.
LISTVIEW
item 1
item 2
item 3
item 4
item 5
item 6
item 7
POSITION AFTER CLICK
0
1
2
3
4
5
0
This is my adapter:
public DoubleListAdapter(Context context, ArrayList<DoubleListItems> items,
String Tag) {
super(context, 0, items);
this.Tag = Tag;
this.context = context;
this.items = items;
vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
final int Position = position;
if (convertView == null) {
convertView = vi.inflate(R.layout.double_list_item, null);
holder = new ViewHolder();
final DoubleListItems i = (DoubleListItems) items.get(position);
holder.item = i;
holder.Icon = (ImageView) convertView
.findViewById(R.id.IconaDoubleList);
holder.Title = (TextView) convertView
.findViewById(R.id.TitoloDoubleList);
holder.SecondLineDesc = (TextView) convertView
.findViewById(R.id.SecondaLineaDesc);
holder.SecondLineValue = (TextView) convertView
.findViewById(R.id.SecondaLineaValue);
holder.ThirdLineDesc = (TextView) convertView
.findViewById(R.id.TerzaLineaDesc);
holder.ThirdLineValue = (TextView) convertView
.findViewById(R.id.TerzaLineaValue);
holder.Delete = (Button) convertView
.findViewById(R.id.ButtonDeleteItem);
holder.Set = (Button) convertView.findViewById(R.id.ButtonSetiItem);
holder.Delete.setTag(holder);
holder.Delete.setOnClickListener(new OnClickListener() {
holder.Set.setTag(holder);
holder.Set.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Log.e("DOUBLELIST ADAPTER ", "Position: " + Position);
String Code = i.getKey();
// String Code = holder.item.getKey();
Intent intent;
Costanti.curLocale = Costanti.LocaliLavorati.Item("L"
+ Code);
intent = new Intent(context, IncassiTab.class);
if (Costanti.curLocale != null) {
context.startActivity(intent);
} else {
Toast.makeText(context, "Locale nullo",
Toast.LENGTH_SHORT);
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final DoubleListItems i = holder.item;
if (i != null) {
holder.Set.setVisibility(View.GONE);
holder.Delete.setVisibility(View.GONE);
holder.Icon.setImageResource(i.getIcon());
holder.Title.setText(i.getTitle());
holder.SecondLineDesc.setText(i.getSecondLineDesc());
if (i.getType().equals("N")) {
holder.SecondLineDesc.setTextColor(context.getResources()
.getColor(R.color.grigio));
}
if (!i.getSecondLineValue().equals("")) {
holder.SecondLineValue.setVisibility(View.VISIBLE);
holder.SecondLineDesc.setTextColor(context.getResources()
.getColor(R.color.orange));
holder.Icon.setImageResource(i.getIcon());
holder.Title.setText(i.getTitle());
holder.SecondLineDesc.setText(i.getSecondLineDesc());
holder.SecondLineValue.setText(i.getSecondLineValue());
if (!i.getThirdLineDesc().equals("")) {
holder.Set.setVisibility(View.VISIBLE);
holder.Delete.setVisibility(View.VISIBLE);
holder.ThirdLineDesc.setVisibility(View.VISIBLE);
holder.ThirdLineValue.setVisibility(View.VISIBLE);
holder.ThirdLineDesc.setText(i.getThirdLineDesc());
holder.ThirdLineValue.setText(i.getThirdLineValue());
holder.Delete.setVisibility(View.VISIBLE);
holder.Set.setVisibility(View.VISIBLE);
} else {
holder.ThirdLineDesc.setVisibility(View.GONE);
holder.ThirdLineDesc.setVisibility(View.GONE);
}
} else {
holder.SecondLineValue.setVisibility(View.GONE);
holder.ThirdLineDesc.setVisibility(View.GONE);
holder.ThirdLineValue.setVisibility(View.GONE);
holder.Set.setVisibility(View.GONE);
holder.Delete.setVisibility(View.GONE);
}
}
return convertView;
}
private class ViewHolder {
public ViewHolder() {
}
protected DoubleListItems item;
protected Button Delete;
protected Button Set;
protected ImageView Icon;
protected TextView Title;
protected TextView SecondLineDesc;
protected TextView SecondLineValue;
protected TextView ThirdLineDesc;
protected TextView ThirdLineValue;
}
This is the solution:
if (convertView == null) {
Your code
}else{
Your code
}
holder.Set.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Your code
}
}

Related

When I scroll the listview the textview get its default value 0

I have two button increase and decrease and one textview. When I click on increase button the value in the textview is increases and vice versa but when I scroll the listview its get its default value 0. How can I resolve this?
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.cart_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.mImgItem = (ImageView) convertView.findViewById(R.id.cart_image);
viewHolder.mTvItemName = (TextView) convertView.findViewById(R.id.tv_item_name);
viewHolder.mTvItemPrice = (TextView) convertView.findViewById(R.id.tv_item_price);
viewHolder.mTvNumber = (TextView) convertView.findViewById(R.id.tv_number);
viewHolder.mBtnAdd = (Button) convertView.findViewById(R.id.btn_add);
viewHolder.mBtnMinus = (Button) convertView.findViewById(R.id.btn_sub);
viewHolder.mImgDelete = (ImageView) convertView.findViewById(R.id.img_del);
viewHolder.mUniqueKey = String.valueOf(position);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.mBtnAdd.setTag(viewHolder);
viewHolder.mBtnMinus.setTag(viewHolder);
viewHolder.mTvNumber.setText("0");
viewHolder.mBtnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ViewHolder tagHolder = (ViewHolder) v.getTag();
int mValue = Integer.parseInt(tagHolder.mTvNumber.getText().toString().trim());
mValue++;
tagHolder.mTvNumber.setText("" + mValue);
});
viewHolder.mBtnMinus.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
ViewHolder tagHolder = (ViewHolder) v.getTag();
int mValue = Integer.parseInt(tagHolder.mTvNumber.getText().toString());
if (mValue > 0) {
mValue--;
tagHolder.mTvNumber.setText("" + mValue);
}
});
convertView.setTag(viewHolder);
PojoCart pojoCart = (PojoCart) getItem(position);
viewHolder.mTvItemName.setText(pojoCart.getmItemName());
viewHolder.mTvItemPrice.setText(pojoCart.getmItemPrice());
return convertView;
}
Save the value to PojoCart class and retrieve value from it every time just as you are getting itemName and itemPrice. So create an integer quantity in PojoCart class and in the adapter's getView method :
PojoCart pojoCart = (PojoCart) getItem(position);
viewHolder.mBtnAdd.setTag(pojoCart);
viewHolder.mBtnMinus.setTag(pojoCart);
if(pojoCart.getQuantity() > 0)
{
viewHolder.mTvNumber.setText(""+pojoCart.getQuantity());
}
else
{
viewHolder.mTvNumber.setText(""+0);
}
viewHolder.mBtnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
PojoCart pojoCart = (PojoCart) v.getTag();
int mValue = pojoCart.getQuantity();
mValue++;
tagHolder.mTvNumber.setText("" + mValue);
pojoCart.setQuantity(mValue)
notifyDataSetChanged();
};
You have to save value of textview.
As listview recreate views on scroll it takes default
Just take model class and save values by position
Use adapter.notifyDataSetChanged() to update values in listview when data changes
You need to store the number in each list item for example use this function to change the value of the list item's number:
List<Integer> mList = new ArrayList<>(LIST_SIZE);
int changeNumber(int position, int value){
mList.get(position) += value;
return mList.get(position);
}
u can use this function like:
viewHolder.mBtnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ViewHolder tagHolder = (ViewHolder) v.getTag();
int mValue = Integer.parseInt(tagHolder.mTvNumber.getText().toString().trim());
tagHolder.mTvNumber.setText("" + changeNumber(position,1);
});
viewHolder.mBtnMinus.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
ViewHolder tagHolder = (ViewHolder) v.getTag();
int mValue = Integer.parseInt(tagHolder.mTvNumber.getText().toString());
if (mValue > 0) {
tagHolder.mTvNumber.setText("" + changeNumber(position,-1);
}
});

How can I change text within a listview, using a adapter

Can you help me out, I'm learning! ;-)
I have two buttons "+" and "-" and I want them to increase or decrease the amount by one up or one down. How do I make sure that it will only have effect on the right textview using an adapter.
Now all the buttons only effects the first one. I know I have to get the position/id of the array. But I don't know how.
btw
Merk = Brand
Aantal = Amount
public class ListViewDemo2 extends Activity {
private ArrayList<String> data = new ArrayList<String>();
int aantal = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view_demo2);
ListView lv = (ListView) findViewById(R.id.bestel_lijst);
lv.setAdapter(new MyListAdapter(this, R.layout.list_item, data));
generateListContent();
}
private void generateListContent(){
for (int i =0; i < 55; i++){
data.add("this is row number: " + i);
}
}
#Override
public boolean onCreateOptionsMenu (Menu menu){
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings){
return true;
}
return super.onOptionsItemSelected(item);
}
private class MyListAdapter extends ArrayAdapter<String>{
private int layout;
public MyListAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
layout = resource;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder mainViewholder = null;
if(convertView == null){
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(layout, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.Merk = (TextView) convertView.findViewById(R.id.Merk);
viewHolder.Aantal = (TextView) convertView.findViewById(R.id.Aantal);
viewHolder.btnup = (Button) convertView.findViewById(R.id.Bup);
viewHolder.btndown = (Button) convertView.findViewById(R.id.Bdown);
viewHolder.btndown.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
aantal = aantal-1;
displayAantal(aantal);
}
});
viewHolder.btnup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
aantal = aantal+1;
displayAantal(aantal);
}
});
convertView.setTag(viewHolder);
}
else {
mainViewholder = (ViewHolder)convertView.getTag();
mainViewholder.Merk.setText(getItem(position));
}
return convertView;
}
}
public class ViewHolder {
TextView Merk;
TextView Aantal;
Button btnup;
Button btndown;
}
public void displayAantal(int aantal) {
TextView aantalView = (TextView) findViewById(R.id.Aantal);
aantalView.setText(String.valueOf(aantal));
}
}
your problem seems to be in your displayAantal(int) method, in particular, in this line:
TextView aantalView = (TextView) findViewById(R.id.Aantal);
You do not need to re-initialize that field here, since it is already initialized inside getView():
viewHolder.Aantal = (TextView) convertView.findViewById(R.id.Aantal);
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder mainViewholder = null;
if(convertView == null){
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(layout, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.Merk = (TextView) convertView.findViewById(R.id.Merk);
viewHolder.Aantal = (TextView) convertView.findViewById(R.id.Aantal);
viewHolder.btnup = (Button) convertView.findViewById(R.id.Bup);
viewHolder.btndown = (Button) convertView.findViewById(R.id.Bdown);
convertView.setTag(viewHolder);
}
else {
mainViewholder = (ViewHolder)convertView.getTag();
}
String str = getItem(position);
mainViewholder.Merk.setText(str);
viewHolder.btndown.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
//just handle the data ,then call adapter.notifyDataSetChanged();
}
});
viewHolder.btnup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
//just handle the data ,then call adapter.notifyDataSetChanged();
}
});
return convertView;
}

Get values of edittext in listview

I want get values of edittext in listview.
This is my CustomAdapter.java
public class CustomAdapter extends BaseAdapter {
Context context;
List<RowItem> rowItem;
ImageView imgIcon, add_cart;
TextView txtTitle;
EditText colli, prezzo, quantita;
String colliStr, prezzoStr, quantitaStr;
ArrayList<String> ArrayPrezzo = new ArrayList<String>();
ArrayList<String> ArrayQuantita = new ArrayList<String>();
ArrayList<String> ArrayColli = new ArrayList<String>();
CustomAdapter(Context context, List<RowItem> rowItem) {
this.context = context;
this.rowItem = rowItem;
}
#Override
public int getCount() {
return rowItem.size();
}
#Override
public Object getItem(int position) {
return rowItem.get(position);
}
#Override
public long getItemId(int position) {
return rowItem.indexOf(getItem(position));
}
#Override
public boolean areAllItemsEnabled() {
return false;
}
#Override
public boolean isEnabled(int position) {
return false;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.single_row, null);
}
imgIcon = (ImageView) convertView.findViewById(R.id.icon);
txtTitle = (TextView) convertView.findViewById(R.id.title);
add_cart = (ImageView) convertView.findViewById(R.id.icon);
colli = (EditText) convertView.findViewById(R.id.editText3);
prezzo = (EditText) convertView.findViewById(R.id.editText2);
quantita = (EditText) convertView.findViewById(R.id.editText);
add_cart.setFocusable(true);
add_cart.setClickable(true);
final RowItem row_pos = rowItem.get(position);
// setting the image resource and title
imgIcon.setImageResource(R.drawable.product);
txtTitle.setText(row_pos.getTitle());
add_cart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// HERE I WANT GET THE VALUES OF EDITTEXT;
}
});
return convertView;
}
can someone help me please? In ly listview there are:
textview | edittext1 | edittext2 | edittext3 | imageview
I don't know how to get values of edittext at position current when i click the button.
Can you make a example with my code?
Thnank you
UPDATE:
I have edited the listener:
add_cart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
prezzoStr = prezzo.getText().toString();
Toast.makeText(context, prezzoStr, Toast.LENGTH_SHORT)
.show();
}
});
but prezzoStr is empty. Toast doesn't show nothing.
I think need use row_pos variable for getting the value of a determinate edittext.
Help please
It's not difficult
add_cart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String colliValue = colli.getText().toString());
String prezzoValue = prezzo.getText().toString());
String quantitaValue = quantita.getText().toString());
}
});
You have to get parent view and then find text view by id:
add_cart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView prezzo = (TextView) findByIdRecursively(v, R.id.editText2);
String prezzoStr = prezzo.getText().toString();
}
});
...
public View findByIdRecursively(View view, int targetId) {
View result = view.findViewById(targetId);
if (result != null) {
return result;
}
View parent = (View) view.getParent();
if (parent == null) {
return null;
}
return findByIdRecursively(parent, targetId);
}
The view in onClick is the view that was clicked (add_cart in this case)
... or you can do:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
...
add_cart = (ImageView) convertView.findViewById(R.id.icon);
add_cart.setTag(convertView);
add_cart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView prezzo = (TextView) ((View)v.getTag()).findViewById(R.id.editText2);
String prezzoStr = prezzo.getText().toString();
}
});

how to change particularly one row textview value while click the button in custome listview?

I have a custom list view with base adapter. i have tried to increment and decrement particularly one row textview value. it is working but all the row textview value is changed while click the button. how can i changed particularly one row textview value. plese suggest me.
public class BreakfastListAdapter extends BaseAdapter {
private Context context;
private String[] number;
private int[] imageid;
ImageView plus1, minus1;
TextView value1;
int a = 0;
public BreakfastListAdapter(Context c, String[] number, int[] imageid) {
context = c;
this.imageid = imageid;
this.number = number;
}
#Override
public int getCount() {
return number.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
// convertView = new View(context);
convertView = inflater.inflate(R.layout.secondadapter, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.text);
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);
plus1 = (ImageView) convertView.findViewById(R.id.plus);
minus1 = (ImageView) convertView.findViewById(R.id.minus);
value1 = (TextView) convertView.findViewById(R.id.value);
textView.setText(number[position]);
imageView.setImageResource(imageid[position]);
value1.setText(String.valueOf(a));
System.out.println(a + "dddddddddddddd");
plus1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
a = a + 1;
value1.setText(String.valueOf(a));
Toast.makeText(context, String.valueOf(a), 1000).show();
System.out.println("######################");
System.out.println(a);
notifyDataSetChanged();
}
});
return convertView;
}
}
You need to keep the value for each row and not one for all the rows.
Change your int a in an int[] a and keep each value of each row.
You will have the following listener:
#Override
public void onClick(View v) {
a[position] = a[position] + 1;
value1.setText(String.valueOf(a[position]));
Toast.makeText(context, String.valueOf(a[position]), 1000).show();
System.out.println("######################");
System.out.println(a[position]);
notifyDataSetChanged();
}
Here is the complete code:
public class BreakfastListAdapter extends BaseAdapter {
private Context context;
private String[] number;
private int[] imageid;
ImageView plus1, minus1;
TextView value1;
int a[];
public BreakfastListAdapter(Context c, String[] number, int[] imageid) {
context = c;
this.imageid = imageid;
this.number = number;
this.a = new int[number.length];
}
#Override
public int getCount() {
return number.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
// convertView = new View(context);
convertView = inflater.inflate(R.layout.secondadapter, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.text);
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);
plus1 = (ImageView) convertView.findViewById(R.id.plus);
minus1 = (ImageView) convertView.findViewById(R.id.minus);
value1 = (TextView) convertView.findViewById(R.id.value);
textView.setText(number[position]);
imageView.setImageResource(imageid[position]);
value1.setText(String.valueOf(a[position]));
System.out.println(a[position] + "dddddddddddddd");
plus1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
a[position] = a[position] + 1;
value1.setText(String.valueOf(a[position]));
Toast.makeText(context, String.valueOf(a[position]), 1000).show();
System.out.println("######################");
System.out.println(a[position]);
notifyDataSetChanged();
}
});
return convertView;
}
}
You should setText string to perticular row TextView.
Every row is referenced through convertView so we will use it
Modification is :
plus1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
a = a + 1;
// this is change
convertView.value1.setText(String.valueOf(a));
Toast.makeText(context, String.valueOf(a), 1000).show();
System.out.println("######################");
System.out.println(a);
notifyDataSetChanged();
}
We need to set a value to 0 at last :
// reset a value at end of getView() method
a = 0;
So that when next row is displayed, a value is 0 and it starts increment from 0
Try this one remove
a = a + 1;//because this is a global variable in the class
change value1.setText(""+(a+1));
if this's not the problem declare an array/arraylist with size similar to listview size and populate arraylist inside the constructer with all values set to 0.Then inside getview() method
int currentposition=position;
plus1.setTag(currentposition);
value1.setText(arraylist.get(currentposition));
plus1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int btnvalue=Integer.parseInt(plus1.getTag().toString());
arraylist.set(btnvalue,(Integer.parseInt(arraylist.get(btnvalue)))+1);
notifydatasetchanged();
}
});
Implement ViewHolder Class to maintain a separate copy of their widgets for example
ViewHolder{
TextView t1;
Imageview i1,i2;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.secondadapter, null);
holder = new ViewHolder();
holder.t1 = (TextView) convertView.findViewById(R.id.text);
holder.i1 =convertView.findViewById(R.id.imageView1);
holder.i2 =convertView.findViewById(R.id.imageView1);
convertView.setTag(holder);
}else
{
ViewHolder holder = (ViewHolder) convertView.getTag();
}
holder.t1.setText(number[position]);
holder.i1.setImageResource(imageid[position]);
holder.t1.setText(String.valueOf(a));
System.out.println(a + "dddddddddddddd");
holdert1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
a = a + 1;
holder.t1.setText(String.valueOf(a));
Toast.makeText(context, String.valueOf(a), 1000).show();
System.out.println("######################");
System.out.println(a);
notifyDataSetChanged();
}
});
return convertView
}

Button in ListView Item

I have a problem.
In my app, i've got a custom ListView with two Button for each Row.
I've created a class extends ArrayAdapter where i try to manage the OnClickListner of the Button, but it Doesn't work. If i press a buttton, nothing happens.
This is my class:
public class AdapterMovimentazioni extends ArrayAdapter<ItemWithIdMov> {
private Context context;
private ArrayList<ItemWithIdMov> items;
private LayoutInflater vi;
public AdapterMovimentazioni(Context context, ArrayList<ItemWithIdMov> items) {
super(context, 0, items);
this.context = context;
this.items = items;
vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
final ItemWithIdMov i = items.get(position);
if (convertView == null) {
convertView = vi.inflate(R.layout.item_movimentazioni, null);
holder = new ViewHolder();
holder.icon = (ImageView) convertView.findViewById(R.id.imageMov);
holder.data = (TextView) convertView.findViewById(R.id.DataMov);
holder.importo = (TextView) convertView.findViewById(R.id.ImportoMov);
holder.locale = (TextView) convertView.findViewById(R.id.localeMov);
holder.delete = (Button) convertView.findViewById(R.id.ButtonDeleteMov);
holder.set = (Button) convertView.findViewById(R.id.ButtonSetMov);
holder.delete.setTag(holder);
holder.delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "DELETEEEEEE", Toast.LENGTH_LONG);
}
});
holder.set.setTag(holder);
holder.set.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "SET", Toast.LENGTH_LONG);
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (i != null) {
holder.icon.setImageResource(i.getIcon());
holder.data.setText(i.getData());
holder.importo.setText(i.getImporto());
holder.locale.setText(i.getLocName());
}
return convertView;
}
private class ViewHolder {
public ViewHolder() {
}
protected Button delete;
protected Button set;
protected ImageView icon;
protected TextView data;
protected TextView importo;
protected TextView locale;
}
Can you help me??
Thank you
use Toast Like
Toast.makeText(context, "SET", Toast.LENGTH_LONG).show();
change your getview method after createing views decleration onClicklistener;
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
final ItemWithIdMov i = items.get(position);
if (convertView == null) {
convertView = vi.inflate(R.layout.item_movimentazioni, null);
holder = new ViewHolder();
holder.icon = (ImageView) convertView.findViewById(R.id.imageMov);
holder.data = (TextView) convertView.findViewById(R.id.DataMov);
holder.importo = (TextView) convertView.findViewById(R.id.ImportoMov);
holder.locale = (TextView) convertView.findViewById(R.id.localeMov);
holder.delete = (Button) convertView.findViewById(R.id.ButtonDeleteMov);
holder.set = (Button) convertView.findViewById(R.id.ButtonSetMov);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "DELETEEEEEE", Toast.LENGTH_LONG);
}
});
holder.set.setTag(holder);
holder.set.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "SET", Toast.LENGTH_LONG);
}
});
if (i != null) {
holder.icon.setImageResource(i.getIcon());
holder.data.setText(i.getData());
holder.importo.setText(i.getImporto());
holder.locale.setText(i.getLocName());
}
return convertView;
}

Categories

Resources