将onClickListener设置为自定义适配器

时间:2014-03-23 13:53:30

标签: java android listview adapter

大家好, 我有2 button和2 textview

的适配器
@Override
public View getView(int position, View view, ViewGroup parent) {
    if(view == null) {
        holder = new Holder();
        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.my_adapter, null);
        holder.result = (TextView)view.findViewById(R.id.description);
        holder.value = (TextView)view.findViewById(R.id.value);
        holder.add = (Button)view.findViewById(R.id.add);
        holder.subtract = (Button)view.findViewById(R.id.subtract);         

        myObject = getItem(position);
        holder.result.setText(myObject.result);
        holder.value.setText(myObject.value);

        view.setTag(holder);
    } else {
        holder = (Holder)view.getTag();
    }

    holder.add.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            // TODO Auto-generated method stub  
        }           
    });

    return view;
}

现在我的问题是:如果我想要当用户按下添加按钮时设置textview的文本(例如)5我该怎么做?如果我加入onCLick方法

holder.result.setText("My text")设置最后一个textview的文本,而不是所选行项目的对应textview(我禁用了listview上的点击):

@Override
public boolean isEnabled(int position) {
    return false;
}

我的问题有解决办法吗?

2 个答案:

答案 0 :(得分:2)

您应该将代码放在适配器内的getView方法中,并记住使用对当前Button / TextVeiw的引用,以便每个Button对应于具体TextView

P.S。我发现你的代码中有一些东西检查出来:

@Override
public View getView(int position, View view, ViewGroup parent) {
    if(view == null) {
        holder = new Holder();
        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.my_adapter, null);
        holder.result = (TextView)view.findViewById(R.id.description);
        holder.value = (TextView)view.findViewById(R.id.value);
        holder.add = (Button)view.findViewById(R.id.add);
        holder.subtract = (Button)view.findViewById(R.id.subtract); 

        view.setTag(holder);
    } else {
        holder = (Holder)view.getTag();
    }       

    myObject = getItem(position);
    holder.result.setText(myObject.result);
    holder.value.setText(myObject.value);
    final TextView tv = holder.result;

    holder.add.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            tv.setText("bla bla");          
            }           
    });

    return view;
}

答案 1 :(得分:0)

你混淆了一些东西,getItem必须低于视图创建的东西。首先创建视图,然后设置值。

@Override
public View getView(int position, View view, ViewGroup parent) {
if(view == null) {
    holder = new Holder();
    inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = inflater.inflate(R.layout.my_adapter, null);
    holder.result = (TextView)view.findViewById(R.id.description);
    holder.value = (TextView)view.findViewById(R.id.value);
    holder.add = (Button)view.findViewById(R.id.add);
    holder.subtract = (Button)view.findViewById(R.id.subtract);         
    view.setTag(holder);
} else {
    holder = (Holder)view.getTag();
}

myObject = getItem(position);
holder.result.setText(myObject.result);
holder.value.setText(myObject.value);

holder.add.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View view) {

     //What should happen here?

    }           
});

return view;

}