简体   繁体   中英

Set onClickListener into custom adapter

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?

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

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

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;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM