设置onClickListener到自定义适配器 [英] Set onClickListener into custom adapter

查看:147
本文介绍了设置onClickListener到自定义适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好所有社区,
我该适配器与2 按钮 2 的TextView

Hello at all the community, i've this adapter with 2 button and 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;
}

现在我的问题是:如果我想,当添加按钮,用户preSS设置的文字的TextView 用(例如)5哪能做这个?如果我投入了的onclick

Now my question is: if I want that when the user press on add button set the text of the textview with (for example) 5 how can i do this? If I put into the onCLick method

holder.result.setText(我的文字)设置的最后的TextView的文本,而不是选定行项目的对应TextView的(我禁用了点击与列表视图):

holder.result.setText("My text") set the text of the last textview and not the correspond textview of the selected row item (I disabled the click on the listview with):

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

有没有我的问题的任何解决方案?

Is there any solution for my problem?

推荐答案

您应该把你的code在适配器中的 getView 方法,并记得使用引用到目前按钮 / TextVeiw 让每个按钮会对应于特定的TextView

You should put your code in the getView method inside the adapter and remember to use references to the current Button/TextVeiw so that each Button would correspond to that specific TextView

P.S。我发现你的code检的东西了这一点:

P.S. I found something with your code check this out:

@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;
}

这篇关于设置onClickListener到自定义适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆