Set onClickListener into custom adapter

这一生的挚爱 提交于 2019-12-12 05:37:19

问题


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

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("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?


回答1:


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



回答2:


You mixed something up, the getItem has to be below the view creation stuff. First create the view, then set the values.

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

}



来源:https://stackoverflow.com/questions/22591793/set-onclicklistener-into-custom-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!