Domanda

I am currently working on an Activity which requires a ListView with custom NumberPickers. I created the number picker as suggested here.

The problem which I am facing is quite frustrating; when I click on the + button of the number picker, the text view keeps displaying 0. I have tried all these suggestions but nothing has been changed.

@Override
public View getView(int position, View convertView, ViewGroup parent)
{

    View vi = convertView;

    //ViewHolder holder = new ViewHolder();

    if(vi == null)
    {
        holder = new ViewHolder();

        vi = inflater.inflate(R.layout.materialdialogcontent, null);

        //Initialize Buttons and TextViews.

        holder.num.setText("0");

        holder.add.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) 
            {
                counter++;
                holder.num.setText("" + counter);
            }
        });

        holder.sub.setOnClickListener(new View.OnClickListener()
        {   
            @Override
            public void onClick(View v) 
            {
                counter--;
                holder.num.setText("" + counter);
            }
        });


        //holder.pk = (NumberPicker)vi.findViewById(R.id.npMaterialAmount);

        vi.setTag(holder);
    }
    else
    {
    holder = (ViewHolder)vi.getTag();
    }

    holder.txt.setText(data.get(position).getName());
    //holder.pk.setMaxValue(20);
    //holder.pk.setMinValue(0);

    return vi;
}

public static class ViewHolder
{
    TextView txt;
    Button add;
    Button sub;
    TextView num;
    //NumberPicker pk;
}

The above code is found in my ListView Adapter.

The following is what I have already tried:

When I debug the above code, the counter increments, and the debugger variable shows that the TextView's text is actually being set to the number present in the counter, however the number displayed is still 0.

I have also tried setting an OnItemClickListener() to the ListView, and nothing happens still. I then removed the Button click listeners from the ListView Adapter class and placed them inside the ListView's onItemClickListener(). When debugging however, the Button click listeners are skipped by the debugger.

Do you have any other suggestions?

I would gratefully appreciate all help possible.

Updated:

holder.add.setTag(position);
    holder.add.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v) 
        {
            int position = (Integer)v.getTag();
            int temp = numPickerValues.get(position);
            temp += 1;
            numPickerValues.set(position, temp);
            notifyDataSetChanged();
        }
    });
    holder.num.setText(String.valueOf(numPickerValues.get(position)));
    return convertView;
È stato utile?

Soluzione

Right now you're probably updating other views, also the counter variable use it's not ok. The right way of handling those row number pickers would be to save their current values in some data structure and set/update the current value from that list:

View vi = convertView;
ViewHolder holder;
if(vi == null) {
    holder = new ViewHolder();
    vi = inflater.inflate(R.layout.materialdialogcontent, null);
    //Initialize Buttons and TextViews.
    vi.setTag(holder);
} else {
    holder = (ViewHolder)vi.getTag();
}
holder.txt.setText(data.get(position).getName());
holder.add.setTag(position); // so we have the proper position in the onClick method
holder.add.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
       // there's no need for counter
      int position = v.getTag();
      // numberpickervalues is the array/list of int values representing the values from all the row
      // update the proper value  
      int temp = numPickerValues.get(position);
      temp += 1; // incremment the value
      numPickerValues.set(position, temp);
      notifyDataSetChanged(); // let the list know about this change
}
});
holder.num.setText(String.valueOf(numberPickerValues.get(position)));

Altri suggerimenti

You need call notifyDataSetChanged ().

This from Android API

public void notifyDataSetChanged ()

Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.

Once you have incremented i suppose you need to refresh the listview. And to do so follow the link below:

Update ListView

As I already suggested days ago: the ViewHolder needs to be a static class.

private static class ViewHolder {
    TextView txt;
    Button add;
    Button sub;
    TextView num;
    //NumberPicker pk;
}

One benefit of using static inner class, is that the inner class can be accessed from static methods, without having an instance of the outer class.

Also I cannot see the reason why u are setting the text for the button when decrementing:

 holder.sub.setText("" + counter);

shouldn't it be?

 holder.num.setText("" + counter);

I suppose when you increment with your Buttons, nothing happens, but when you scroll to another position in the ListView and then back to the position where you incremented, the incremented value will be displayed. This is because the ListView is refreshen upon scrolling. Simply call

notifyDataSetChanged()

to refresh the ListViews Views.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top