0

I am displaying some rows in my ListView and want to change only one of the row, by seeing if any of the date in the list matches today's date.

My partial code is:

DateFormat df = new SimpleDateFormat("EEEEE, LLLL d", Locale.US);
String[] suspendedDates = {
    "Tuesday, January 1",
    "Wednesday, April 16",
    "Monday, October 6",
    "Wednesday, December 25"
};

lvDisplay = (ListView) findViewById(R.id.lvDisp);

        for (int i = 0; i < suspendedDates.length; i ++) {
            sDate = suspendedDates[i];
            sReason = datesReason[i];
            if (Arrays.asList(suspendedDates).contains(df.format(Calendar.getInstance(Locale.US).getTime()))) {
                inIconShow = R.drawable.caliconpressed; //if today matches display a different drawable
                contents.add(new SetRows(inIconShow, sDate, sReason));
            }
            if (!Arrays.asList(suspendedDates).contains(df.format(Calendar.getInstance(Locale.US).getTime()))) {
                inIconShow = R.drawable.calicon; //if today doesn't match the array display the default drawable
                contents.add(new SetRows(inIconShow, sDate, sReason));
            }
        }
        // Now set your adapter.
        adapter = new SetRowsCustomAdapter(MainActivity.this, R.layout.listdates, contents);
        lvDisplay.setAdapter(adapter);

SetRowsCustomAdapter class:

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class SetRowsCustomAdapter extends ArrayAdapter<SetRows> {
    Context context;
   int layoutResourceId;
   ArrayList<SetRows> data=new ArrayList<SetRows>();
   public SetRowsCustomAdapter(Context context, int layoutResourceId, ArrayList<SetRows> data) {
       super(context, layoutResourceId, data);
       this.layoutResourceId = layoutResourceId;
       this.context = context;
       this.data = data;
   }

   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
       View row = convertView;
       ImageHolder holder = null;

       if(row == null)
       {
           LayoutInflater inflater = ((Activity)context).getLayoutInflater();
           row = inflater.inflate(layoutResourceId, parent, false);

           holder = new ImageHolder();
           holder.txtTitle = (TextView)row.findViewById(R.id.tvDateVal);
           //holder.txtTitle.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/robm.ttf"));
           holder.imgIcon = (ImageView)row.findViewById(R.id.ivIcon0);
           holder.txtID = (TextView)row.findViewById(R.id.tvReasonVal);
           //holder.txtID.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/robm.ttf"));
           row.setTag(holder);
       }
       else
       {
           holder = (ImageHolder)row.getTag();
       }

       SetRows myImage = data.get(position);
       holder.txtTitle.setText(myImage.name);
       holder.txtID.setText(myImage.id);
       int outImage=myImage.image;
       holder.imgIcon.setImageResource(outImage);
      return row;

   }

   static class ImageHolder
   {
       ImageView imgIcon;
       TextView txtTitle;
       TextView txtID;
   }
}

So the 4th row should have a different icon and the other 3 should have default icon. What is happening is if the date matches, every row has the different icon, otherwise every row has the default icon.

How do I fix it?

Si8
  • 9,141
  • 22
  • 109
  • 221

2 Answers2

1

if you want change the image you can do in getView method like bellow code:

@Override
   public View getView(int position, View convertView, ViewGroup parent) {
       View row = convertView;
       ImageHolder holder = null;

       if(row == null)
       {
           LayoutInflater inflater = ((Activity)context).getLayoutInflater();
           row = inflater.inflate(layoutResourceId, parent, false);

           holder = new ImageHolder();
           holder.txtTitle = (TextView)row.findViewById(R.id.tvDateVal);
           //holder.txtTitle.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/robm.ttf"));
           holder.imgIcon = (ImageView)row.findViewById(R.id.ivIcon0);
           holder.txtID = (TextView)row.findViewById(R.id.tvReasonVal);
           //holder.txtID.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/robm.ttf"));
           row.setTag(holder);
       }
       else
       {
           holder = (ImageHolder)row.getTag();
       }

       SetRows myImage = data.get(position);
       holder.txtTitle.setText(myImage.name);
       holder.txtID.setText(myImage.id);
       int outImage=myImage.image;
       if (data.getValue)
          holder.imgIcon.setImageResource(deferentIcon);
       else
          holder.imgIcon.setImageResource(defaultIcon);
      return row;

   }

and if you want change multi row icons you can send one list of your row and check in that, and for refresh you list use adapter.notifyDataSetChanged()

Edit

for your situation you can add on boolean value in your DS and if date equal set true otherwise set else that and in if statement check that

Shayan Pourvatan
  • 11,898
  • 4
  • 42
  • 63
  • It doesn't have to be the 4th position, I want to match the date only. If today's date matches any of the date mentioned in the `suspendedDates`, then display a different icon. – Si8 Dec 25 '13 at 16:52
  • you can check with every thing that you want, just set your condition in if statement in getView method – Shayan Pourvatan Dec 25 '13 at 16:55
  • See my Edit, add one boolean value to data DS or check date in getView Method – Shayan Pourvatan Dec 25 '13 at 16:58
  • I did the check inside the adapter as you recommended but a little differently and it worked!!! Thanks. – Si8 Dec 25 '13 at 17:00
  • @Shayanpourvatan one point worth noting listview recycles views. so change getView accordingly. so this may not work well. – Raghunandan Dec 25 '13 at 17:01
  • i dont get Your mean @Raghunandan, can you edit my post as you wish.thanks for useful comment. my english word is not good,sorry – Shayan Pourvatan Dec 25 '13 at 17:03
  • @Shayanpourvatan http://stackoverflow.com/questions/20611123/listview-subobject-clickable-confilct/20612237#20612237. here only those row buttons that are clicked are updated which is similar to op's case is what i meant. If it works for OP its fine – Raghunandan Dec 25 '13 at 17:05
  • @Shayanpourvatan so this `if (position == 4) holder.imgIcon.setImageResource(deferentIcon);` might not work well is what i wanted to point out. – Raghunandan Dec 25 '13 at 17:11
  • @Raghunandan i told to OP that create one boolean value in SetRows class and change that every time that want, i got your mean – Shayan Pourvatan Dec 25 '13 at 17:14
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/43888/discussion-between-shayan-pourvatan-and-raghunandan) – Shayan Pourvatan Dec 25 '13 at 17:17
0

Use viewHolder in adapter then use notifyDataSetChanged() in adapter; I already used and it will help.

Yogendra
  • 4,817
  • 1
  • 28
  • 21