我如何可以通过点击按钮列表视图的每一行中删除我的列表行(How can I delete my li

2019-10-21 05:15发布

我有一个自定义的数组适配器的列表视图。 我想删除的项目时,删除按钮clicked.But我不能修复它。 即使我的应用程序越来越模具当我点击删除按钮。 我没有得到任何想法。 我的代码如下 -

码:-

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;


}

}

Answer 1:

试试这个办法,希望这将帮助你解决你的问题。

首先在getView定义位置 最后 ()的参数,然后尝试删除列表项从您使用删除方法和使用方法notifyDataSetChanged像通知您的适配器列表数据持有人listDetail:

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();
    }
});


Answer 2:

当你点击删除按钮执行下面的代码:

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


Answer 3:

删除按钮的OnClick首先你必须从你的ArrayList中删除的位置,然后通知你的适配器,如:

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

或者你也可以做出适配器复位到你的列表等的方法:

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

而调用此方法,如:

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


Answer 4:

当你点击删除按钮....你应该执行这些东西1.从数组列表或数组或数据库中删除项目.... 2.然后再次显示列表刷新布局屏幕....(把你的在方法都显示列表的代码,然后再调用它)...

希望这有助于你.....



文章来源: How can I delete my list row by clicking on button in each row of the LISTVIEW