ListView项中的按钮

时间:2013-10-21 10:50:00

标签: java android listview adapter

我有问题。 在我的应用程序中,我有一个自定义ListView,每行有两个Button。 我已经创建了一个类extends ArrayAdapter,我尝试管理Button的OnClickListner,但它不起作用。如果我按下按钮,没有任何反应。 这是我的班级:

public class AdapterMovimentazioni extends ArrayAdapter<ItemWithIdMov> {

private Context context;
private ArrayList<ItemWithIdMov> items;
private LayoutInflater vi;

public AdapterMovimentazioni(Context context, ArrayList<ItemWithIdMov> items) {
    super(context, 0, items);
    this.context = context;
    this.items = items;
    vi = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    ViewHolder holder;
    final ItemWithIdMov i = items.get(position);
    if (convertView == null) {
        convertView = vi.inflate(R.layout.item_movimentazioni, null);
        holder = new ViewHolder();
        holder.icon = (ImageView) convertView.findViewById(R.id.imageMov);
        holder.data = (TextView) convertView.findViewById(R.id.DataMov);
        holder.importo = (TextView) convertView.findViewById(R.id.ImportoMov);
        holder.locale = (TextView) convertView.findViewById(R.id.localeMov);
        holder.delete = (Button) convertView.findViewById(R.id.ButtonDeleteMov);
        holder.set = (Button) convertView.findViewById(R.id.ButtonSetMov);

        holder.delete.setTag(holder);
        holder.delete.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(context, "DELETEEEEEE", Toast.LENGTH_LONG);
            }
        });

        holder.set.setTag(holder);
        holder.set.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(context, "SET", Toast.LENGTH_LONG);

            }
        });
        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    if (i != null) {
        holder.icon.setImageResource(i.getIcon());
        holder.data.setText(i.getData());
        holder.importo.setText(i.getImporto());
        holder.locale.setText(i.getLocName());
    }

    return convertView;
}

private class ViewHolder {

    public ViewHolder() {

    }

    protected Button delete;
    protected Button set;
    protected ImageView icon;
    protected TextView data;
    protected TextView importo;
    protected TextView locale;

}
你帮我吗? 谢谢

2 个答案:

答案 0 :(得分:2)

使用Toast Like

Toast.makeText(context, "SET", Toast.LENGTH_LONG).show();

答案 1 :(得分:0)

在创建视图声明onClicklistener后更改你的getview方法;

public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    ViewHolder holder;
    final ItemWithIdMov i = items.get(position);
    if (convertView == null) {
        convertView = vi.inflate(R.layout.item_movimentazioni, null);
        holder = new ViewHolder();
        holder.icon = (ImageView) convertView.findViewById(R.id.imageMov);
        holder.data = (TextView) convertView.findViewById(R.id.DataMov);
        holder.importo = (TextView) convertView.findViewById(R.id.ImportoMov);
        holder.locale = (TextView) convertView.findViewById(R.id.localeMov);
        holder.delete = (Button) convertView.findViewById(R.id.ButtonDeleteMov);
        holder.set = (Button) convertView.findViewById(R.id.ButtonSetMov);

        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }


        holder.delete.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(context, "DELETEEEEEE", Toast.LENGTH_LONG);
            }
        });

        holder.set.setTag(holder);
        holder.set.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(context, "SET", Toast.LENGTH_LONG);

            }
        });

    if (i != null) {
        holder.icon.setImageResource(i.getIcon());
        holder.data.setText(i.getData());
        holder.importo.setText(i.getImporto());
        holder.locale.setText(i.getLocName());
    }

    return convertView;
}