Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 955701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:25:32+00:00 2026-05-16T00:25:32+00:00

I am making a time sheet program where a user inputs his in- and

  • 0

I am making a time sheet program where a user inputs his in- and out-punches. I have a ListView that I am populating from an array of calendar objects. I would like each row to show the day and date then on a new line the time, but I only want to display the day and date if it is different from the previous element.

Currently, I am setting visibility in the BaseAdapter based on comparisons using position vs position-1 (which are used as indices to the array). This only works if the whole list fits on the screen. If it extends beyond the screen and the user scrolls around the results are unpredictable.

To further confuse things, I am setting the color of the times, based on the position, to alternate between green and red (in/out) and it works as expected, scrolling or not.

How does Android handle the ListView position when scrolling or what could I do differently to show/hide the day and date?

public class TimeSheetActivity extends Activity {
SQLiteDatabase timesDatabase;
Cursor punchCursor;

private static Calendar[] allPunches;



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.timesheet);
} //end onCreate()


@Override
public void onResume() {
    super.onResume();
    //Open database
    timesDatabase = openOrCreateDatabase(
            "times_database.db",
            SQLiteDatabase.CREATE_IF_NECESSARY,
            null);
    timesDatabase.setLocale(Locale.getDefault());
    timesDatabase.setLockingEnabled(true);
    timesDatabase.setVersion(1);
    punchCursor = timesDatabase.query("Timepunches", null, null, null, null, null, "punch ASC;");
    updateTimeSheet();
} //end onResume()


@Override
public void onPause() {
    super.onPause();
    timesDatabase.close();
} //end onResume()


private static class  EfficientAdapter extends BaseAdapter {
    private LayoutInflater mInflater;

    public EfficientAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
    }

    public int getCount() {
        return allPunches.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.time_list_row, null);
            holder = new ViewHolder();
            holder.text1 = (TextView) convertView.findViewById(R.id.day_textview);
            holder.text2 = (TextView) convertView.findViewById(R.id.date_textview);
            holder.text3 = (TextView) convertView.findViewById(R.id.times_this_day_textview);

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

        String dayNames[] = new DateFormatSymbols().getWeekdays();

        //Initialize first list element
        if (position < 1) {
            holder.text1.setText(dayNames[allPunches[position].get(Calendar.DAY_OF_WEEK)]);
            holder.text2.setText(formatDate(allPunches[position]));

        }
        else {
            holder.text1.setText(dayNames[allPunches[position].get(Calendar.DAY_OF_WEEK)]);
            holder.text2.setText(formatDate(allPunches[position]));
            holder.text1.setVisibility(View.VISIBLE);
            holder.text2.setVisibility(View.VISIBLE);

            //Hide day and date if same as last
            if (formatDate(allPunches[position]).contentEquals(formatDate(allPunches[position-1]))) {
                holder.text1.setVisibility(View.GONE);
                holder.text2.setVisibility(View.GONE);
            }
        }

        holder.text3.setText(formatTime(allPunches[position], true) + " " + position);

        //Color in/out punches
        if (position%2 == 0) {
            holder.text3.setTextColor(Color.GREEN);
        }
        else {
            holder.text3.setTextColor(Color.RED);
        }

        return convertView;
    } //end getView()

    static class ViewHolder {
        public TextView text1;
        TextView text2;
        TextView text3;
    }
} //end EfficientAdapter


public void updateTimeSheet() {
    punchCursor = timesDatabase.query("Timepunches", null, null, null, null, null, "punch ASC;");
    allPunches = new Calendar[punchCursor.getCount()];

    int i = 0;                  //for indexing allPunches
    Calendar nextDay = Calendar.getInstance();
    nextDay.setLenient(true);

    //populate allPunches
    for (punchCursor.moveToFirst(); !punchCursor.isAfterLast(); punchCursor.moveToNext()) {
        allPunches[i] = Calendar.getInstance();
        allPunches[i].setTimeInMillis(punchCursor.getLong(0));
        ++i;
    } //end for

    final ListView timeSheetListView = (ListView)findViewById(R.id.timesheet_listview);
    timeSheetListView.setAdapter(new EfficientAdapter(this));
    timeSheetListView.setOnItemClickListener(new OnItemClickListener() {...}); //end click listener for list item
} //end updateTimeSheet()


public static String formatTime(Calendar thisTime, boolean showAMPM) {...}


public static String formatDate(Calendar thisDate) {
    String formattedDate = "";
    formattedDate += thisDate.get(Calendar.MONTH) +"-"+ thisDate.get(Calendar.DAY_OF_MONTH) +"-"+ thisDate.get(Calendar.YEAR);
    return formattedDate;
} //end formatDate()

} //end TimeSheet Activity

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:25:33+00:00Added an answer on May 16, 2026 at 12:25 am

    I seem to have been setting VISIBLE in the wrong place. Here is the code for getView() that seems to have it fixed!

        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.time_list_row, null);
                holder = new ViewHolder();
                holder.text1 = (TextView) convertView.findViewById(R.id.day_textview);
                holder.text2 = (TextView) convertView.findViewById(R.id.date_textview);
                holder.text3 = (TextView) convertView.findViewById(R.id.times_this_day_textview);
    
                convertView.setTag(holder);
            }
            else {
                holder = (ViewHolder) convertView.getTag();
            }
    
            String dayNames[] = new DateFormatSymbols().getWeekdays();
            holder.text1.setVisibility(View.VISIBLE);
            holder.text2.setVisibility(View.VISIBLE);
    
            //Initialize list
            if (position < 1) {
                holder.text1.setText(dayNames[allPunches[position].get(Calendar.DAY_OF_WEEK)]);
                holder.text2.setText(formatDate(allPunches[position]));
            }
            else {
                //Show day and date if not same as last
                holder.text1.setText(dayNames[allPunches[position].get(Calendar.DAY_OF_WEEK)]);
                holder.text2.setText(formatDate(allPunches[position]));
    
                if (formatDate(allPunches[position]).contentEquals(formatDate(allPunches[position-1]))) {
                    holder.text1.setVisibility(View.GONE);
                    holder.text2.setVisibility(View.GONE);
                }
            }
    
            holder.text3.setText(formatTime(allPunches[position], true));
    
            //Color in/out punches
            if (position%2 == 0) {
                holder.text3.setTextColor(Color.GREEN);
            }
            else {
                holder.text3.setTextColor(Color.RED);
            }
    
            return convertView;
        } //end getView()
    
        static class ViewHolder {
            public TextView text1;
            TextView text2;
            TextView text3;
        }
    } //end EfficientAdapter
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tough time making this design decision. I could go with traditional new
For some time i have been making more or less little games. One level
I am currently making an Android launcher and have spent a lot of time
I'm creating a time sheet for work to learn more about asp and making
I have been making a volunteer sign up sheet for our hockey club. I
Actually I am Making an Alarm app. In that When i set the Time
I have spent quite some time making a function and the last 15-20 minutes
I am having a hard time making the slug from Friendly_id in a nested
This is my first time making a mini-game from complete scratch. Google chrome gives
I am currently making a time-clock system that is inside of a custom-built application

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.