我如何可以通过点击按钮列表视图的每一行中删除我的列表行

我如何可以通过点击按钮列表视图的每一行中删除我的列表行

本文介绍了我如何可以通过点击按钮列表视图的每一行中删除我的列表行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

code: -

 包com.abc_fragment;进口的java.util.ArrayList;进口com.abc.R;进口android.content.Context;
进口android.util.Log;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.view.ViewGroup;
进口android.widget.BaseAdapter;
进口android.widget.Button;
进口android.widget.ImageButton;
进口android.widget.TextView;
进口android.widget.Toast;公共类Fragment_ListviewContactAdapter延伸BaseAdapter
{
私有静态的ArrayList listDetail;
私人LayoutInflater mInflater;
上下文语境;
公共Fragment_ListviewContactAdapter(上下文片段,ArrayList的结果)
{
listDetail =结果;
mInflater = LayoutInflater.from(片段);}
@覆盖
公众诠释getCount将()
{
// TODO自动生成方法存根
返回listDetail.size();
}@覆盖
公共对象的getItem(INT为arg0){
// TODO自动生成方法存根
返回listDetail.get(为arg0);
}@覆盖
众长getItemId(INT为arg0)
{
// TODO自动生成方法存根
返回将arg0;
}@燮pressWarnings(未使用)
@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
// TODO自动生成方法存根
ViewHolder持有人;
如果(convertView == NULL)
{convertView = mInflater.inflate(R.layout.fragment_listitem,NULL);
持有人=新ViewHolder(); // Atomholderpayment
// holder.ListviewDashBoard = listDetail.get(位置);
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 =(按钮)convertView.findViewById(R.id.button_delete);
//holder.removeButton.setTag(holder.ListviewDashBoard);
//holder.removeButton.setOnClickListener((OnClickListener)本);
//convertView.setOnClickListener(new OnItemClickListener(位置));
convertView.setTag(保持器);
/ * holder.removeButton.setOnClickListener(新OnClickListener()
{@覆盖
公共无效的onClick(视图v)
{
INT POS =(整数)v.getTag();
listDetail.remove(POS)
Fragment_ListviewContactAdapter.this.notifyDataSetChanged();
}
// TODO自动生成方法存根}); * /
}其他
{
支架=(ViewHolder)convertView.getTag();
}
Fragment_listViewDashboard ListviewDashBoard = listDetail.get(位置);
holder.orderno.setText(listDetail.get(位置).getOrderno());
holder.dispatchTo.setText(listDetail.get(位置).getDispatchTo());
holder.dealerN.setText(listDetail.get(位置).getDealerN());
holder.orderT.setText(listDetail.get(位置).getOrderT());
holder.amountT.setText(listDetail.get(位置).getAmountT());
holder.removeButton.setOnClickListener(新OnClickListener()
{@覆盖
公共无效的onClick(视图v)
{
// TODO自动生成方法存根Log.i(删除单击按钮,************ **********);
Toast.makeText(背景下,删除单击按钮
Toast.LENGTH_LONG).show();
}
});返回convertView;
}静态类ViewHolder
{
TextView的orderno,dispatchTo,Dealern的,orderT,amountT;
按钮removeButton;
}}


解决方案

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

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

  holder.removeButton.setOnClickListener(新OnClickListener()
{
    @覆盖
    公共无效的onClick(视图v)
    {
      listDetail.remove(位置);
      notifyDataSetChanged();
      Log.i(删除单击按钮,************ *********);
      Toast.makeText(背景下,删除单击按钮,Toast.LENGTH_LONG).show();
    }
});

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;


}

}
解决方案

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

这篇关于我如何可以通过点击按钮列表视图的每一行中删除我的列表行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 13:35