ListView showing incorrect data in views - java

If I scroll through my ListView one TextView (expiresIn) in an listview item gets copied into others randomly. I'm guessing this has to do with the ViewHolder but I really can't see where i'm going wrong, and its only the expiresIn TextView I have the problem with:
public class MySyncListAdapter extends CouchbaseViewListAdapter {
protected MainActivity parent;
private CouchDbConnector db;
private static final String TAG = "MySyncListAdapter";
public MySyncListAdapter(MainActivity parent, CouchDbConnector couchDbConnector, ViewQuery viewQuery) {
super(couchDbConnector, viewQuery, true);
this.parent = parent;
this.db = couchDbConnector;
Log.d(TAG, "in constructor");
}
private static class ViewHolder {
ImageView img;
TextView label;
TextView dateAdded;
TextView expiresIn;
}
#Override
public View getView(int position, View itemView, ViewGroup parent) {
Log.d(TAG, "in getView");
View v = itemView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_item, null);
ViewHolder vh = new ViewHolder();
vh.label = (TextView) v.findViewById(R.id.label);
vh.img = (ImageView) v.findViewById(R.id.img);
vh.dateAdded = (TextView) v.findViewById(R.id.tvDateAdded);
vh.expiresIn = (TextView) v.findViewById(R.id.tvExpiresInList);
v.setTag(vh);
}
TextView label = ((ViewHolder)v.getTag()).label;
TextView dateAdded = ((ViewHolder)v.getTag()).dateAdded;
TextView expiresIn = ((ViewHolder)v.getTag()).expiresIn;
ImageView img = ((ViewHolder)v.getTag()).img;
Row row = getRow(position);
JsonNode item = row.getValueAsNode();
JsonNode itemText = item.get("header");
JsonNode dateAddedText = item.get("dateAdded");
JsonNode expiryDateText = item.get("expiryDate");
JsonNode attachmentText = item.get("_attachments");
ViewHolder holder = (ViewHolder) v.getTag();
if(label != null) {
holder.label.setText(itemText.getTextValue());
}
if(dateAdded != null) {
Log.d(TAG, "in dateAdded getView");
if(CompareDate.isDateAddedToday(dateAddedText.getTextValue())){
holder.dateAdded.setText(R.string.today);
}
else if (CompareDate.isDateAddedYesterday(dateAddedText.getTextValue())){
holder.dateAdded.setText(R.string.yesterday);
}
else{
String age = String.valueOf(CompareDate.getAgeInDays(dateAddedText.getTextValue()));
holder.dateAdded.setText( age + " " + parent.getResources().getString(R.string.days_ago));
}
}
if(!expiryDateText.isNull() && expiresIn != null){
long noDays = CompareDate.getDaysUntil(expiryDateText.getTextValue());
if(noDays<6){
holder.expiresIn.setTextColor(Color.parseColor("#ff4444"));
}
holder.expiresIn.setText(String.valueOf(noDays) + " " + parent.getResources().getString(R.string.days_until_expiry));
}
if(img != null){
//bitmap stuff
holder.img.setImageBitmap(bitmap);
}
return v;
}
Update
Solved it by adding an else statement an specifically setting expiresIn to "". Would love an explaination however why if I don't specifically set it, it uses another item's textview.
if(!expiryDateText.isNull() && expiresIn != null){
long noDays = CompareDate.getDaysUntil(expiryDateText.getTextValue());
if(noDays<6){
holder.expiresIn.setTextColor(Color.parseColor("#ff4444"));
}
holder.expiresIn.setText(String.valueOf(noDays) + " " + parent.getResources().getString(R.string.days_until_expiry));
}
else {
holder.expiresIn.setText("");
}

Related

Listview scrolling issue on checkbox

I'm having issues with listview containing checkbox, when a checkbox is unchecked and when the view gets recycled, the checked checkbox becomes checked. Any help? I've seen lots of same topic but I can't seem to find an answer to my issue. Thank you.
Here's the code of getView.
public View getView(int i, View convertView, ViewGroup viewGroup) {
View mView = convertView;
String betid = mData.get(i).get("betid");
ViewHolder holder ;
ListView listview = findViewById(R.id.lvMain);
if (mView == null) {
Context context = viewGroup.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
mView = inflater.inflate(R.layout.row_layout, null,false);
holder = new ViewHolder();
holder.tx_number = (TextView) mView.findViewById(R.id.tx_number);
holder.tx_amount = (TextView) mView.findViewById(R.id.tx_amount);
holder.tx_counter = (TextView) mView.findViewById(R.id.tx_counter);
holder.checkBox = (CheckBox) mView.findViewById(R.id.checkmark);
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#RequiresApi(api = Build.VERSION_CODES.N)
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
String[] b;
if (buttonView.isChecked()) {
EditText xxx = (EditText)findViewById(R.id.editText);
String amtX = xxx.getText().toString();
int toMultiply;
if(amtX.equals("")){
toMultiply = 1;
}else{
toMultiply = Integer.parseInt(amtX);
}
System.out.println(toMultiply);
String yy = editText.getText().toString().trim();
checked.add((Integer) holder.checkBox.getTag());
String item = listview.getItemAtPosition(i).toString();
String[] a = item.split(", ");
b = a[1].split("=");
String[] sep = a[0].split("=");
String betnumber = sep[1];
String betamount= b[1];
final String sorted = betnumber.chars().sorted().mapToObj(c -> Character.valueOf((char)c).toString()).collect(Collectors.joining());
if (doubleChecker(sorted)){
answer = (Integer.parseInt(betamount) * toMultiply / 3);
holder.tx_counter.setText(valueOf(answer));
}else{
if(yy.equals("")){
answer = (Integer.parseInt(betamount) * toMultiply / 6);
holder.tx_counter.setText(valueOf(answer));
}else{
answer = ((Integer.parseInt(betamount) * toMultiply - Integer.parseInt(yy)) / 6);
holder.tx_counter.setText(valueOf(answer));
}
}
holder.tx_counter.setBackgroundColor(getResources().getColor(R.color.bluelim));
holder.tx_amount.setBackgroundColor(getResources().getColor(R.color.bluelim));
holder.tx_number.setBackgroundColor(getResources().getColor(R.color.bluelim));
}
else {
holder.tx_counter.setBackgroundColor(Color.WHITE);
holder.tx_amount.setBackgroundColor(Color.WHITE);
holder.tx_number.setBackgroundColor(Color.WHITE);
holder.tx_counter.setText("0");
checked.remove((Integer) holder.checkBox.getTag());
}
}
});
holder.checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
holder.checkBox.setChecked(true);
}else{
holder.checkBox.setChecked(false);
}
}
});
mView.setTag(holder);
holder.checkBox.setTag(i);
} else {
holder = (ViewHolder) mView.getTag();
((ViewHolder)mView.getTag()).checkBox.setTag(i);
}
if (betid != null) {
String betnumber = mData.get(i).get("betnumber");
String amountTarget = mData.get(i).get("amountTarget");
holder.tx_amount.setText(amountTarget);
holder.tx_number.setText(betnumber);
holder.tx_counter.setText("0");
}
ViewHolder holde2r = (ViewHolder) mView.getTag();
for (int k = 0; k < checked.size(); k++) {
if (checked.get(k) == i) {
holde2r.checkBox.setChecked(true);
}
else if (checked.get(k) != i) {
holde2r.checkBox.setChecked(false);
}
}
return mView;
}
private class ViewHolder {
TextView tx_number;
TextView tx_amount;
TextView tx_counter;
CheckBox checkBox;
}
}
Sorry for the long code, I just think that it is needed to place here because i dont knw what causes the issue.

Duplicates the Image in ListView when downloading

When the image has been downloaded, some of the images duplicates in the listview rows, even though some of the row has no ImageID, The adapter view duplicates the downloaded images, when i suddenly scroll the listview
My code in getView
public View getView(final int position, View convertView, ViewGroup parent) {
int anotherPosition = position;
if (inflater == null) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
final Holder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, null);
holder = new Holder();
holder.title = (TextView) convertView.findViewById(R.id.description);
holder.exp = (TextView) convertView.findViewById(R.id.expiration);
holder.someImages = (ImageView) convertView.findViewById(R.id.listview_image);
holder.isFavouriteImage = (ImageView) convertView.findViewById(R.id.isFavourite);
convertView.setTag(holder);
convertView.setTag(R.id.listview_image, holder.someImages);
convertView.setTag(R.id.description, holder.title);
convertView.setTag(R.id.expiration, holder.exp);
convertView.setTag(R.id.isFavourite, holder.isFavouriteImage);
} else {
holder = (Holder) convertView.getTag();
}
RowItemLoyalty rowItemLoyalty = data.get(position);
if(rowItemLoyalty != null) {
holder.someImages.setTag(position);
holder.someImages.setImageBitmap(null);
holder.title.setText(data.get(position).getDescription());
holder.exp.setText(data.get(position).getDateEnd());
Log.d("TrueOrFalse", String.valueOf(holder.someImages));
if(holder.someImages != null ) {
if (data.get(position).getImageId() != 0) {
data.get(position).setBitmap(email, password, data.get(position).getImageId(), data.get(position).getBitmap(), new RowItemLoyalty.RetrieveBitmapListener() {
#Override
public void onSuccess(Bitmap bitmap) {
Log.d("ImageID123", String.valueOf(data.get(position).getImageId()));
holder.someImages.setImageBitmap(null);
if (data.get(position).getBitmap() != null) {
Log.d("True", "True");
holder.someImages.setImageBitmap(bitmap);
}
}
});
}
} else if(holder.someImages == null ) {
Drawable placeholder = ContextCompat.getDrawable(context, R.drawable.placeholderwhite);
holder.someImages.setImageDrawable(placeholder);
Log.d("PlaceHolder2", String.valueOf(placeholder));
}
//----------- placeholder for imageview list -----------
//holder.someImages.setImageBitmap(null);
if (data.get(position).getBitmap() != null && holder.someImages != null) {
holder.someImages.setImageBitmap(data.get(position).getBitmap());
Log.d("PlaceHolder", "Implemented");
Log.d("PlaceHolder", String.valueOf(data.get(position)));
} else if (data.get(position).getBitmap() == null) {
Drawable placeholder = ContextCompat.getDrawable(context, R.drawable.placeholderwhite);
holder.someImages.setImageDrawable(placeholder);
Log.d("PlaceHolder2", String.valueOf(placeholder));
}
//------------ for favourite logo-------------
if (data.get(position).getIsFavorite() == false) {
Drawable placeholderIsNotFavourite = ContextCompat.getDrawable(context, R.drawable.ic_favourite_icon);
holder.isFavouriteImage.setImageDrawable(placeholderIsNotFavourite);
} else if (data.get(position).getIsFavorite() == true) {
Drawable favourited = ContextCompat.getDrawable(context, R.drawable.favourite_two);
holder.isFavouriteImage.setImageDrawable(favourited);
}
}
return convertView;
}
My Holder class
public static class Holder {
TextView title;
TextView exp;
TextView tokensFor;
ImageView promotionImages;
ImageView isFavouriteImage;
}
There are some useful libraries you can use to loading images like Glide
Also you can see this Picasso v/s Imageloader v/s Fresco vs Glide
Try using Universal Image Loader Universal Image Loader
to set image on ImageView
Remove checking convertView == null condition, like this:
convertView = inflater.inflate(R.layout.list_item, null);
holder = new Holder();
holder.title = (TextView) convertView.findViewById(R.id.description);
holder.exp = (TextView) convertView.findViewById(R.id.expiration);
holder.someImages = (ImageView) convertView.findViewById(R.id.listview_image);
holder.isFavouriteImage = (ImageView) convertView.findViewById(R.id.isFavourite);

List View loses values on scroll,and get worg position on item click

I have the below ListView with a custom adapter. I received an ArrayAdapter from service, but something wrong is happening on scroll and the values are lost.
public class AccountStatementArrayAdapter extends ArrayAdapter<ListaExtratos> {
public AccountStatementArrayAdapter(Context context, int textViewResourceId, List<ListaExtratos> listaExtratos) {
super(context, textViewResourceId, listaExtratos);
this.listaExtratos = listaExtratos;
}
#Override
public int getItemViewType(int position) {
return listaExtratos.get(position).getData() == null ? SECTION : ACCOUNT_STATEMENT_ITEM;
}
#Override
public int getViewTypeCount() {
return 2;
}
#Override
public int getCount() {
return listaExtratos.size();
}
#Override
public ListaExtratos getItem(int position) {
return listaExtratos.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.list_item_account_statement, parent, false);
}
ListaExtratos p = getItem(position);
if (p != null && position != 0) {
TextView simpleDescriptionTextView = (TextView) v.findViewById(R.id.list_item_account_statement_simple_description_field);
TextView txtDataExtrato = (TextView) v.findViewById(R.id.txtDataExtrato);
TextView simpleValueTextView = (TextView) v.findViewById(R.id.list_item_account_statement_simple_value_field);
TextView completeDescriptionTextView = (TextView) v.findViewById(R.id.list_item_account_statement_complete_description_field);
TextView completeDateTextView = (TextView) v.findViewById(R.id.list_item_account_statement_complete_date_field);
TextView completeValueTextView = (TextView) v.findViewById(R.id.list_item_account_statement_complete_value_field);
TextView completeDocumentTextView = (TextView) v.findViewById(R.id.list_item_account_statement_complete_document_field);
TextView completeBalanceTextView = (TextView) v.findViewById(R.id.list_item_account_statement_complete_balance_field);
if(p.getHistorico() != null){
simpleDescriptionTextView.setText(p.getHistorico());
}
if(p.getValor() != null){
if ((p.getValor() != null) && (Double.parseDouble(p.getValor()) < 0)) {
simpleValueTextView.setTextColor(Color.RED);
completeValueTextView.setTextColor(Color.RED);
completeValueTextView.setText(StringUtil.getStringValueFromBigDecimal(new BigDecimal(p.getValor())));
simpleValueTextView.setText(StringUtil.getStringValueFromBigDecimal(new BigDecimal(p.getValor())));
} else {
simpleValueTextView.setTextColor(Color.BLACK);
completeValueTextView.setTextColor(Color.BLACK);
completeValueTextView.setText(StringUtil.getStringValueFromBigDecimal(new BigDecimal(p.getValor())));
simpleValueTextView.setText(StringUtil.getStringValueFromBigDecimal(new BigDecimal(p.getValor())));
}
}
if(p.getHistorico() != null)
completeDescriptionTextView.setText(p.getHistorico());
if(p.getData() != null)
completeDateTextView.setText(p.getData());
if(p.getDocto() != null)
completeDocumentTextView.setText(p.getDocto());
completeBalanceTextView.setVisibility(View.GONE);
if (!datas.contains(p.getData())) {
txtDataExtrato.setVisibility(View.VISIBLE);
txtDataExtrato.setText(DateUtil.getDataPorExtenso(DateUtil.dateFromString(p.getData(), "dd/MM/yyyy")));
datas += p.getData() + ";";
} else {
txtDataExtrato.setVisibility(View.GONE);
}
}
return v;
}
}
There are some bugs in your adapter code.
Remove check for position != 0
Add a else check as well where you set a empty text for each view.
For eg. :
Change this
if(p.getHistorico() != null){
simpleDescriptionTextView.setText(p.getHistorico());
}
to
if(p.getHistorico() != null){
simpleDescriptionTextView.setText(p.getHistorico());
}
else {
simpleDescriptionTextView.setText("");
}
These changes should solve your issue.

Data fails to be updated in expandable list

I am calling three JSONRequests with Volley and this method is called at every response of the request. This method only is executed when all three responses are ready. But when I try to update the expandable list it fails to display on the screen. Could anyone see if I am doing something wrong?
private void updateWhenReady(){
System.out.println(validCurrent+ " " + validDaily + " " + validHourly );
if(validCurrent && validDaily && validHourly)
{
System.out.println("in");
for (WeatherCondition wc: dailyResponseList)
{
ArrayList<WeatherCondition> tempList = new ArrayList<>();
for(WeatherCondition w: hourlyResponseList)
{
if(w.getDate().equalsIgnoreCase(wc.getDate()))
{
tempList.add(w);
}
}
weatherList.put(wc,tempList);
}
for (WeatherCondition weatherCondition: weatherList.keySet())
{
System.out.println(" + " + weatherCondition.getDate());
}
weatherList = dbHelper.getWeatherConditionsHashMap();
hourlyList = new ArrayList<WeatherCondition>(weatherList.keySet());
adapter = new WeatherSearchListAdapter(getActivity().getApplicationContext(), weatherList, hourlyList);
expList.setAdapter(adapter);
adapter.notifyDataSetChanged();
System.out.println(validCurrent + " " + validDaily + " " + validHourly);
validCurrent = false;
validDaily = false;
validHourly = false;
}
}
and the following is the explist adapter
public class WeatherSearchListAdapter extends BaseExpandableListAdapter {
private Context ctx;
private HashMap<WeatherCondition, List<WeatherCondition>> weatherList;
private List<WeatherCondition> list;
public WeatherSearchListAdapter(Context ctx, HashMap<WeatherCondition, List<WeatherCondition>> parentList, List<WeatherCondition> list){
this.weatherList = parentList;
this.list = list;
this.ctx = ctx;
};
#Override
public int getGroupCount() {
return list.size();
}
#Override
public int getChildrenCount(int groupPosition) {
return weatherList.get(list.get(groupPosition)).size();
}
#Override
public Object getGroup(int groupPosition) {
return list.get(groupPosition);
}
#Override
public Object getChild(int parent, int child) {
return weatherList.get(list.get(parent)).get(child);
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public long getChildId(int parent, int child) {
return child;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
WeatherCondition groupWeatherCondition = (WeatherCondition) getGroup(groupPosition);
if(convertView == null)
{
LayoutInflater inflator = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflator.inflate(R.layout.fragment_daily_list_parent, parent, false);
}
ImageView weatherIconIdIV = (ImageView) convertView.findViewById(R.id.weatherIconIV);
ImageView windIconIdIV = (ImageView) convertView.findViewById(R.id.windDirectionIV);
TextView humidityTV = (TextView) convertView.findViewById(R.id.humitityListText);
TextView rainTV = (TextView) convertView.findViewById(R.id.rainListText);
TextView windDirectionTV = (TextView) convertView.findViewById(R.id.windDirectionText);
TextView windSpeedTV = (TextView) convertView.findViewById(R.id.windSpeedText);
TextView maxTempTV = (TextView) convertView.findViewById(R.id.maxTempListText);
TextView minTempTV = (TextView) convertView.findViewById(R.id.minTempListText);
TextView dateTV = (TextView) convertView.findViewById(R.id.dateListText);
String name = groupWeatherCondition.getWeatherIconId();
int weatherIconId = ctx.getResources().getIdentifier(name, "drawable", ctx.getPackageName());
weatherIconIdIV.setImageResource(weatherIconId);
String name1 = groupWeatherCondition.getWind().getWindIconId();
int windIconId = ctx.getResources().getIdentifier(name1, "drawable", ctx.getPackageName());
windIconIdIV.setImageResource(windIconId);
humidityTV.setText(groupWeatherCondition.getHumidity());
rainTV.setText(groupWeatherCondition.getRain());
windDirectionTV.setText(groupWeatherCondition.getWind().getWindDirection());
windSpeedTV.setText(groupWeatherCondition.getWind().getSpeed());
maxTempTV.setText(groupWeatherCondition.getMaxTemp());
minTempTV.setText(groupWeatherCondition.getMinTemp());
dateTV.setText(groupWeatherCondition.getDate());
convertView.setBackgroundColor(Color.parseColor("#5E5E5E"));
return convertView;
}
#Override
public View getChildView(int parent, int child, boolean isLastChild, View convertView, ViewGroup parentView) {
WeatherCondition childWeatherCondition = (WeatherCondition) getChild(parent, child);
if(convertView == null)
{
LayoutInflater inflator = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflator.inflate(R.layout.fragment_daily_list_parent, parentView, false);
}
ImageView weatherIconIdIV = (ImageView) convertView.findViewById(R.id.weatherIconIV);
ImageView windIconIdIV = (ImageView) convertView.findViewById(R.id.windDirectionIV);
TextView humidityTV = (TextView) convertView.findViewById(R.id.humitityListText);
TextView rainTV = (TextView) convertView.findViewById(R.id.rainListText);
TextView windDirectionTV = (TextView) convertView.findViewById(R.id.windDirectionText);
TextView windSpeedTV = (TextView) convertView.findViewById(R.id.windSpeedText);
TextView maxTempTV = (TextView) convertView.findViewById(R.id.maxTempListText);
TextView minTempTV = (TextView) convertView.findViewById(R.id.minTempListText);
TextView dateTV = (TextView) convertView.findViewById(R.id.dateListText);
String name = childWeatherCondition.getWeatherIconId();
int weatherIconId = ctx.getResources().getIdentifier(name, "drawable", ctx.getPackageName());
weatherIconIdIV.setImageResource(weatherIconId);
String name1 = childWeatherCondition.getWind().getWindIconId();
int windIconId = ctx.getResources().getIdentifier(name1, "drawable", ctx.getPackageName());
windIconIdIV.setImageResource(windIconId);
humidityTV.setText(childWeatherCondition.getHumidity());
rainTV.setText(childWeatherCondition.getRain());
windDirectionTV.setText(childWeatherCondition.getWind().getWindDirection());
windSpeedTV.setText(childWeatherCondition.getWind().getSpeed());
maxTempTV.setText(childWeatherCondition.getMaxTemp());
minTempTV.setText(childWeatherCondition.getMinTemp());
dateTV.setText(childWeatherCondition.getDate());
return convertView;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
Do the variable initializations before the logic starts -
private void updateWhenReady(){
System.out.println(validCurrent+ " " + validDaily + " " + validHourly );
weatherList = dbHelper.getWeatherConditionsHashMap();
hourlyList = new ArrayList<WeatherCondition>(weatherList.keySet());
adapter = new WeatherSearchListAdapter(getActivity().getApplicationContext(), weatherList, hourlyList);
System.out.println(validCurrent + " " + validDaily + " " + validHourly);
if(validCurrent && validDaily && validHourly)
{
System.out.println("in");
for (WeatherCondition wc: dailyResponseList)
{
ArrayList<WeatherCondition> tempList = new ArrayList<>();
for(WeatherCondition w: hourlyResponseList)
{
if(w.getDate().equalsIgnoreCase(wc.getDate()))
{
tempList.add(w);
}
}
weatherList.put(wc,tempList);
}
for (WeatherCondition weatherCondition: weatherList.keySet())
{
System.out.println(" + " + weatherCondition.getDate());
}
//Do you need to reset these? What if only two of the three were true?
validCurrent = false;
validDaily = false;
validHourly = false;
}
}
expList.setAdapter(adapter);
/* If you are setting adapter every time *updateWhenReady* is invoked, no need to notifyDataSetChanged, because its a new adapter and layout will be drawn fresh */
//adapter.notifyDataSetChanged();
}
In getChildView
inflate using fragment_daily_list_parent
copy-past error?

Text Views in Grid Views disapearing on grid view scroll

I am having issues with androids grid view. When I scroll, some text views disappear. I have a conditional statement that checks a value in the database and hides the text views based on that. But that conditional statement is only meant to be for the views in the grid that meet the criteria. However, when scrolling all views seem to change.
Here is my custom array adapter (I have also attached an image to show what I mean):
![public class ArrayAdapterHandler extends ArrayAdapter<Day> {
public Context context;
int layoutResourceId;
ArrayList<Day> days = new ArrayList<Day>();
public ArrayAdapterHandler(Context context, int dayId,
ArrayList<Day> days) {
super(context, dayId, days);
this.layoutResourceId = dayId;
this.context = context;
this.days = days;
}
/*private view holder class*/
private class ViewHolder {
TextView day_text;
TextView situps_text;
TextView crunches_text;
TextView legRaises_text;
TextView plank_text;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Day day = days.get(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.day_row, null);
if (position % 2 == 0) {
convertView.setBackgroundColor(0x30BFFF1E);
} else {
convertView.setBackgroundColor(0x30CCCCCC);
}
holder = new ViewHolder();
holder.day_text = (TextView) convertView.findViewById(R.id.day_text);
holder.situps_text = (TextView) convertView.findViewById(R.id.situps_text);
holder.crunches_text = (TextView) convertView.findViewById(R.id.crunches_text);
holder.legRaises_text = (TextView) convertView.findViewById(R.id.legRaise_text);
holder.plank_text = (TextView) convertView.findViewById(R.id.plank_text);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.day_text.setText(String.valueOf(day.getDay()));
if (day.isCompleted() == 1 || day.isRestDay() == 1) {
// Hide all the activity values which are 0 anyway.
holder.situps_text.setVisibility(4);
holder.legRaises_text.setVisibility(4);
holder.plank_text.setVisibility(4);
}
if (day.isRestDay() == 1) {
holder.crunches_text.setText("REST DAY");
} else if(day.isCompleted() == 1) {
holder.crunches_text.setText("COMPLETED!");
} else {
holder.situps_text.setText(String.valueOf(day.getSitups()) + " situps");
holder.crunches_text.setText(String.valueOf(day.getCrunches()) + " crunches");
holder.legRaises_text.setText(String.valueOf(day.getLegRaises()) + " leg raises");
holder.plank_text.setText(String.valueOf(day.getPlanks()) + "sec planks");
}
return convertView;
}
}
![Scrolling issue image]: http://i.stack.imgur.com/zB3uL.png
Thankyou.

Categories