I'm using ListView to show list of some elements for which I need to count things. My list view looks like this
The value "3" gets updated when I click on plus and minus button or reset button. This is how I achieve this in my CustomListViewAdapter
holder.plusButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ViewGroup parent = (ViewGroup) v.getParent();
TextView tv = (TextView) parent.findViewById(R.id.count);
int count = Integer.parseInt((String) tv.getText());
count++;
tv.setText(String.valueOf(count));
int currentVal = Integer.parseInt(totalCount.getText().toString());
totalCount.setText(String.valueOf(currentVal + 1));
}
});
where I'm using ViewHolder pattern.
Now the problem is, after I update the value, it's not getting updated in adapter. i.e when I try to loop over adapter, I still get the value as 3 which I set initially rather than updated value. Am I doing it right way or is there a better way of doing it. how do I make sure that adapter has the updated value.
2nd Question: As the adapter is not getting updated, when I'm trying to retrieve the values from list to send it somewhere, I get nullpointer when I do this
view = listView.getChildAt(i).findViewById(R.id.count);
for views in list which are not visible on screen. i.e if I have 10 such rows, only 5 will be visible on screen and when I try to access 6th, I get null pointer for above code block. How should I be accessing the listview values then?
EDIT: My getView method source code is
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Counter rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.counter_list, null);
holder = new ViewHolder();
convertView.findViewById(R.id.parentLayoutRow).setBackgroundColor(Color.parseColor(rowItem.getPrimaryColor()));
holder.resetButton = (Button) convertView.findViewById(R.id.buttonReset);
holder.counterName = (TextView) convertView.findViewById(R.id.counterName);
holder.counterDesc = (TextView) convertView.findViewById(R.id.counterDesc);
holder.count = (TextView) convertView.findViewById(R.id.count);
holder.minusButton = (Button) convertView.findViewById(R.id.buttonMinus);
holder.plusButton = (Button) convertView.findViewById(R.id.buttonPlus);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.resetButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ViewGroup parent = (ViewGroup) v.getParent();
TextView tv = (TextView) parent.findViewById(R.id.count);
int currentVal = Integer.parseInt(totalCount.getText().toString());
totalCount.setText(String.valueOf(currentVal - Integer.parseInt(tv.getText().toString())));
tv.setText(String.valueOf(0));
}
});
holder.counterName.setText(rowItem.getName());
holder.counterDesc.setText(rowItem.getDesc());
holder.count.setText(String.valueOf(rowItem.getCount()));
holder.resetButton.setBackgroundColor(Color.parseColor(rowItem.getButtonColor()));
holder.minusButton.setBackgroundColor(Color.parseColor(rowItem.getButtonColor()));
holder.minusButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ViewGroup parent = (ViewGroup) v.getParent();
TextView tv = (TextView) parent.findViewById(R.id.count);
int count = Integer.parseInt((String) tv.getText());
if (count > 0) {
count--;
tv.setText(String.valueOf(count));
int currentVal = Integer.parseInt(totalCount.getText().toString());
totalCount.setText(String.valueOf(currentVal - 1));
}
}
});
holder.plusButton.setBackgroundColor(Color.parseColor(rowItem.getButtonColor()));
holder.plusButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ViewGroup parent = (ViewGroup) v.getParent();
TextView tv = (TextView) parent.findViewById(R.id.count);
int count = Integer.parseInt((String) tv.getText());
count++;
tv.setText(String.valueOf(count));
int currentVal = Integer.parseInt(totalCount.getText().toString());
totalCount.setText(String.valueOf(currentVal + 1));
}
});
return convertView;
}
now how do I update the adapter value in this function holder.minusButton.setOnClickListener(new OnClickListener()