Not able to remove item at particular index from array adapter - android

i am not able to remove item from arraylist at particular index.
When i am trying to remove item then it is remove from bottom only.
private ArrayList<myproduct> values;
public View getView(final int position, final View convertView,
ViewGroup parent) {
final ViewHolder holder;
View v = convertView;
if (v == null) {
final LayoutInflater vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row_favourite, parent, false);
holder = new ViewHolder();
holder.btnaddcollection = (Button) v
.findViewById(R.id.btnaddcollection);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
homeproduct mhomeproduct = values.get(position);
holder.btnaddcollection.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
values.remove(position);
}
});
return v;
}
static class ViewHolder {
public Button btnaddcollection;
}
From above code when i am trying to remove then it is start from bottom to remove .
How can i start to remove item from top with particular position.?

Call notifyDataSetChanged() after you remove the item.
This 'will say' to adapter: "Eh!! data was changed!"

Related

Update specific Items in ListView

I want to update (and delete) specific rows in my ListView knowing the attributes (id and name) of the object contained in the ListView. I don't know the position, therefore I can't use
View v = mListView.getChildAt()
I'm using an adapter for the List View, which is extending an ArrayAdapter of my model class.
How can I override the method getPosition ?
Here my ListViewAdapter
public class ListViewAdapter extends ArrayAdapter<ModelTimer> {
public List<ModelTimer> mListOfTimers;
private Context mContext;
public ListViewAdapter(Context context, List<ModelTimer> modelTimerList) {
super(context, R.layout.item_active_timer, modelTimerList);
this.mListOfTimers = modelTimerList;
this.mContext = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
ModelTimer modelTimer = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder holder;// view lookup cache stored in tag
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.item_active_timer, parent, false);
holder.tvTime = (TextView) convertView.findViewById(R.id.active_timer_timeLeft);
holder.tvTitle = (TextView) convertView.findViewById(R.id.active_timer_name);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// Populate the data into the template view using the data object
holder.tvTime.setText(modelTimer.expirationTime);
holder.tvTitle.setText(modelTimer.name);
// Return the completed view to render on screen
return convertView;
}
#Override
public int getPosition(ModelTimer item) {
return super.getPosition(item);
}
// View lookup cache
public class ViewHolder {
TextView tvTitle;
TextView tvTime;
}
}
Not the best way to do it, but look this code:
#Override
public int getPosition(ModelTimer item) {
int pos = -1;
for(int i = 0; i < mListOfTimers.size(); i++ )
if(mListOfTimers.name.equals(item.name) // and check id
pos = i;
return pos;
}

Change background of item in Adapter

I want to change the background of a selected list item in my adapter.
The list adapter
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final Card card = getItem(position);
final ViewHolder viewHolder;
if (convertView != null) {
viewHolder = (ViewHolder) convertView.getTag();
} else {
convertView = from(context).inflate(R.layout.card_item, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
}
viewHolder.select.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewHolder.cardLayout.setBackgroundColor(ContextCompat.getColor(context, R.color.md_grey_600));
}
});
return convertView;
}
private class ViewHolder {
Button select;
RelativeLayout cardLayout;
public ViewHolder(View view) {
select = (Button) view.findViewById(R.id.carditem_btn_upvote);
cardLayout = (RelativeLayout) view.findViewById(R.id.cardlist_item);
}
}
The problem is that when I select for ex. the first item of the list, the 7th item is also selected. I have noticed that there is a kind of a patron. When I select the second item, the 8th item is also selected and so on.
Let' say you have 10 item in list and initially you will store all your item color as white .
HashMap<Integer,Integer> mhashColorselected=new HashMap();
HashMap<Integer,Integer> mHashBtnVisibility=new HashMap();
for(i=0;i<10;i++){
//Put Default Color of your All list item will be here
mhashColorselected.put(i,R.color.white)
mHashBtnVisibility.put(i,View.Invisible);
}
Now in your adapter getView();
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Card card = getItem(position);
final ViewHolder viewHolder;
if (convertView != null) {
viewHolder = (ViewHolder) convertView.getTag();
} else {
convertView = from(context).inflate(R.layout.card_item, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
}
viewHolder.select.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewHolder.cardLayout.setBackgroundColor(ContextCompat.getColor(context, R.color.md_grey_600));
//Here we are storing Selected item By User
mhashColorselected.put(position,R.color.md_grey_600);
mHashBtnVisibility.put(i,View.Visible);
notifitydataSetChange();
}
});
//Here it's will take fault value or else it will set color as selected item
viewHolder.cardLayout.setBackgroundColor(ContextCompat.getColor(context, mhashColorselected.getValue(position)));
if(View.VISIBLE==mHashBtnVisibility.get(position)){
viewHolder.select.setVisibility(View.VISIBLE)
}else{
viewHolder.select.setVisibility(View.INVISIBLE)
}
return convertView;
}

Delete a row from listview xamarin

I have a activity in which there are some buttons and a listview.each rows of the listview has a delete button which i'm intend to delete the row whenever the user click the delete button.
i'm using a custom adapter so here is my getview method:
public override View GetView(int position, View convertView, ViewGroup parent)
{
convertView = menuActivity.LayoutInflater.Inflate(Resource.Layout.OrderItemLayout, null);
ImageButton btn = convertView.FindViewById<ImageButton>(Resource.Id.btn_del);
btn.Tag = position;
btn.Click += btn_Click;
return convertView;
}
void btn_Click(object sender, EventArgs e)
{
_lstOrder.RemoveAt((int)((sender as ImageButton)).Tag);
NotifyDataSetChanged();
}
but the problem is that when i click the delete button the listview doesn't refresh so the row that was deleted is still there but it will gone if i go back and then come to the activity again.
how can I solve this problem so after i delete a row the listview refresh?
thank you
Creating a common arraylist
ArrayList<GeterSetterClass> comn_arylist = new ArrayList<GeterSetterClass>();
Customized addapter setting
custdet_adapter adapter = new custdet_adapter(this,comn_arylist);
listview.setAdapter(adapter);
listview.invalidateViews();
Customized Adapter class
public static class custdet_adapter extends BaseAdapter {
private ArrayList<GeterSetterClass> cust_arrayList;
private LayoutInflater l_Inflater;
public custdet_adapter(Context context,
ArrayList<GeterSetterClass> list_details) {
cust_arrayList = list_details;
l_Inflater = LayoutInflater.from(context);
}
public int getCount() {
return cust_arrayList.size();
}
public Object getItem(int position) {
return cust_arrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView,
ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = l_Inflater.inflate(R.layout.layoutname_struct,null);
holder = new ViewHolder();
holder.txt_custname = (TextView) convertView
.findViewById(R.id.textView1);
holder.txt_mobno = (TextView) convertView
.findViewById(R.id.textView2);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txt_custname.setText(cust_arrayList.get(position).getName());
holder.txt_mobno.setText(cust_arrayList.get(position).getunitcd());
holder.txt_custname.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
comn_arylist.remove(position);
listview.invalidateViews();
}
});
return convertView;
}
class ViewHolder {
TextView txt_custname;
TextView txt_mobno;
}
}

Add a new Entry to ArrayAdapter

What i am trying to do here is to have a button to add a new entry to my custom listView,something like add to card concept.
However, everything working fine, it is just that when i click the button only one entry is added, and when i click it for the second time nothing changed.
My Fragment
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView=inflater.inflate(R.layout.fragment_port_group, container,false);
injectViews();
mGroupPortAdapter=new GroupPortLazyAdapter(getActivity());
addToList.setOnClickListener(new OnAddToListClickLinsten());
return rootView;
}
public class OnAddToListClickLinsten implements OnClickListener{
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mGroupPortAdapter.add(new GroupPortInModel("23424",12321324,"0177889062"));
portGroupListView.setAdapter(mGroupPortAdapter);
}
}
This is the lazy Adapter
public class GroupPortLazyAdapter extends ArrayAdapter {
private int[] colors = new int[] { 0x30bebebe, 0x30FFFFFF };
public GroupPortLazyAdapter(Context context) {
super(context, -1);
}
#Override
public View getView(int position, View convertView, ViewGroup arg2) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.port_group_custom_list_view, null);
holder = new ViewHolder();
holder.acountId = (TextView) convertView
.findViewById(R.id.port_account_id);
holder.serialNo = (TextView) convertView
.findViewById(R.id.port_serial_no);
holder.phoneNumber = (TextView) convertView
.findViewById(R.id.port_phone_number);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.acountId.setText(getItem(position).getAcountId());
holder.serialNo
.setText(String.valueOf(getItem(position).getSerialNo()));
holder.phoneNumber.setText(getItem(position).getPhone());
int colorPos = position % colors.length;
convertView.setBackgroundColor(colors[colorPos]);
return convertView;
}
static class ViewHolder {
TextView acountId;
TextView serialNo;
TextView phoneNumber;
int position;
}
}
Have you tried using mGroupPortAdapter.notifyDatasetChanged() instead of portGroupListView.setAdapter(mGroupPortAdapter)?
Also, your try using this as your getView().
#Override
public View getView(int position, View convertView, ViewGroup arg2) {
GroupPortInModel item = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.port_group_custom_list_view, null);
}
((TextView) convertView.findViewById(R.id.port_account_id))
.setText(item.getAcountId());
((TextView) convertView.findViewById(R.id.port_serial_no))
.setText(item.getSerialNo());
((TextView) convertView.findViewById(R.id.port_phone_number))
.setText(item.getPhone());
int colorPos = position % colors.length;
convertView.setBackgroundColor(colors[colorPos]);
return convertView;
}

Get the position after click on button in a list view

I've that in my Adapter :
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if(convertView==null)
{
convertView = myInflater.inflate(R.layout.list, null);
holder = new ViewHolder();
holder.text01 = (TextView) convertView.findViewById(R.id.Text01);
((Button)convertView.findViewById(R.id.ListButonPlus)).setOnClickListener(this);
convertView.setTag(holder);
}else
{
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
And that :
public void onClick(View v)
{
Toast.makeText(v.getContext(), "pouette",1000).show();
}
And I try t get the position of the item who contains my button.
How Can I pass the position variable present in the getView method to my onClick Method?
I will have several button In my View (Item view)
Thank you
Ok I found, I pass my position to the ViewHolder
and I get the ViewHolder from the tag of the view or parrent view by a recursive function.
Is there any different method?
public static class ViewHolder
{
private TextView text01;
public int position;
}
public ViewHolder getViewHolder(View v)
{
if(v.getTag() == null)
{
return getViewHolder((View)v.getParent());
}
return (ViewHolder)v.getTag();
}
public void onClick(View v) {
ViewHolder vh = getViewHolder(v);
vh.position // Here I get position
}
I know this post is old, but this could help some others:
In the getView of my adapeter:
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
ContactHolder holder = null;
final ContactRow ContactRow = ContactRowList.get(position);
if(row == null)
{
...
holder.delete = (ImageButton)row.findViewById(R.id.btn_contact_delete); // my button inside the item
holder.delete.setTag(position);
...
}
This is my listener for this button
holder.delete.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
int position=(Integer)v.getTag();
((MainActivity)context).deleteContact(position,true); // call the delete function form the main activity
}
});
My holder
static class ContactHolder
{
...
ImageButton delete;
}

Categories

Resources