gpt4 book ai didi

android - 如何通过单击 LISTVIEW 每一行中的按钮来删除我的列表行

转载 作者:行者123 更新时间:2023-11-29 14:27:21 27 4
gpt4 key购买 nike

我有一个带有自定义数组适配器的 ListView 。我想在单击删除按钮时删除该项目。但我无法修复它。当我点击删除按钮时,甚至我的应用程序也会死掉。我不知道。我的代码如下-

代码:-

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;


}

}

最佳答案

尝试这种方式,希望这能帮助您解决问题。

首先在 getView() 参数中将 position 定义为 final 然后尝试使用 从列表数据持有者 listDetail 中删除列表项删除 方法并使用notifyDataSetChanged 方法通知您的适配器,例如:

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

关于android - 如何通过单击 LISTVIEW 每一行中的按钮来删除我的列表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26623385/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com