How can I delete my list row by clicking on button in each row of the LISTVIEW

天涯浪子 提交于 2019-12-24 11:36:36

问题


I have a list view with custom array adapter. I want to get delete the item when delete button clicked.But I am not able to fix it out. Even my app getting die when I click on delete button. I am not getting any idea. My codes are as follows-

Code:-

package com.abc_fragment;

import java.util.ArrayList;

import com.abc.R;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class Fragment_ListviewContactAdapter extends BaseAdapter
{
private static ArrayList listDetail;
private LayoutInflater mInflater;
Context context;
public Fragment_ListviewContactAdapter(Context Fragment, ArrayList results)
{
listDetail = results;
mInflater = LayoutInflater.from(Fragment);

}




@Override
public int getCount()
{
// TODO Auto-generated method stub
return listDetail.size();
}

@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return listDetail.get(arg0);
}

@Override
public long getItemId(int arg0)
{
// TODO Auto-generated method stub
return arg0;
}

@SuppressWarnings("unused")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if(convertView == null)
{

convertView = mInflater.inflate(R.layout.fragment_listitem, null);
holder = new ViewHolder(); //Atomholderpayment
// holder.ListviewDashBoard = listDetail.get(position);


holder.orderno = (TextView) convertView.findViewById(R.id.OrderNo_text);
holder.dispatchTo = (TextView) convertView.findViewById(R.id.dispatchTo_text);
holder.dealerN = (TextView) convertView.findViewById(R.id.dealerName_text);
holder.orderT = (TextView) convertView.findViewById(R.id.order_text);
holder.amountT = (TextView) convertView.findViewById(R.id.Amount_text);
holder.removeButton = (Button)convertView.findViewById(R.id.button_delete);
//holder.removeButton.setTag(holder.ListviewDashBoard);
//holder.removeButton.setOnClickListener((OnClickListener) this);
//convertView.setOnClickListener(new OnItemClickListener(position));
convertView.setTag(holder);
/* holder.removeButton.setOnClickListener(new OnClickListener()
{

@Override
public void onClick(View v)
{
int pos = (Integer) v.getTag();
listDetail.remove(pos);
Fragment_ListviewContactAdapter.this.notifyDataSetChanged();
}
// TODO Auto-generated method stub

});*/


} else
{
holder = (ViewHolder) convertView.getTag();
}
Fragment_listViewDashboard ListviewDashBoard =listDetail.get(position);
holder.orderno.setText(listDetail.get(position).getOrderno());
holder.dispatchTo.setText(listDetail.get(position).getDispatchTo());
holder.dealerN.setText(listDetail.get(position).getDealerN());
holder.orderT.setText(listDetail.get(position).getOrderT());
holder.amountT.setText(listDetail.get(position).getAmountT());
holder.removeButton.setOnClickListener(new OnClickListener()
{

@Override
public void onClick(View v)
{
// TODO Auto-generated method stub

Log.i("Delete Button Clicked", "*************************************************");
Toast.makeText(context, "Delete button Clicked",
Toast.LENGTH_LONG).show();
}
});

return convertView;
}

static class ViewHolder
{
TextView orderno, dispatchTo, dealerN,orderT, amountT ;
Button removeButton;


}

}

回答1:


Try this way,hope this will help you to solve your problem.

First define position as final in getView() parameter then try to remove list item from you list data holder listDetail using remove method and notify your adapter using notifyDataSetChanged method like :

holder.removeButton.setOnClickListener(new OnClickListener()
{ 
    @Override
    public void onClick(View v)
    {
      listDetail.remove(position);
      notifyDataSetChanged();
      Log.i("Delete Button Clicked",*************************************************");
      Toast.makeText(context, "Delete button Clicked",Toast.LENGTH_LONG).show();
    }
});



回答2:


When you click on delete button implement below code:

YOURLISTVIEW.removeViewAt(position);
YOURARRAY.remove(position);
YOURADAPTER.notifyDataSetChanged();



回答3:


OnClick of delete button first you have to remove position from your arraylist and then notify to your adapter like:

holder.removeButton.setOnClickListener(new OnClickListener(){ 
@Override
    public void onClick(View v){
      _arrayList.remove(position);
       notifyDataSetChanged();
  }
});

Or you can make a method to reset adapter into your list like:

public void _resetAdapter(ArrayList<String> _list){
  // Set your adapter here and pass _list in your adapter
}

And call this method like:

  holder.removeButton.setOnClickListener(new OnClickListener(){ 
   @Override
    public void onClick(View v){
      _arrayList.remove(position);
       _resetAdapter(_arrayList);
  }
});



回答4:


When you click on delete button....you should perform these things 1. Remove Item from your array list or array or database.... 2. Then refresh your layout screen by displaying the list again....(put your all display list code in a method and then call it again)...

Hope this help you.....



来源:https://stackoverflow.com/questions/26623385/how-can-i-delete-my-list-row-by-clicking-on-button-in-each-row-of-the-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!