Set onClickListener to custom adapter

Hi all community, I have 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 clicks the add button, set the text textview

with (for example) 5, how can I do that? If I put the methodonCLick

holder.result.setText("My text")

set the text of the last text view, not the corresponding text representation of the selected row item (I disabled click on the list box):

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

      

Is there any solution for my problem?

0


source to share


2 answers


You should put your code in a method getView

inside the adapter and don't forget to use references to the current Button

/ TextVeiw

so each one Button

matches that specificTextView

PS I found something with your code to test this:



@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


source


You have mixed something, getItem should be below the created view. 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;

      



}

0


source







All Articles