Todays date is 05/01/2017 but it is showing on 29/01/2017.Which method is good for me using gridview

67 Views Asked by At

[1]Here is my adapter page

 if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.programlist, null);
            //holder.tv=(TextView) convertView.findViewById(R.id.txtdate);

        }
        Calendar cal = Calendar.getInstance();
        int month = cal.get(Calendar.MONTH);
        int year = cal.get(Calendar.YEAR);
        int dayofmonth = cal.get(Calendar.DAY_OF_MONTH);

        ImageView imgTv = (ImageView) convertView.findViewById(R.id.imageView1);
        Toast.makeText(context, ""+month, Toast.LENGTH_SHORT).show();

        imgTv.setImageResource(imageId[position]);
         if(currentFragmentMonth != -1 && month==currentFragmentMonth && year==2017) {

             if (dayofmonth == position+1 ) {
          imgTv.setBackgroundColor(Color.BLUE);
                 Toast.makeText(context, "You selected at position " + position, Toast.LENGTH_LONG).show();

        }


        }



        return convertView;
    }

    public void setCurrentFragmentMonth(int month)
    {
        this.currentFragmentMonth = month;
    }

}

Here is my month code

    Context context;
    ArrayList prgmName;
    GridView gv;
    TextView textView_date;
        int month,day,year;
    int currentFragmentMonth = -1;


    public static int [] prgmImages={R.drawable.one,R.drawable.eight,R.drawable.fifteen,R.drawable.twentytwo,R.drawable.twentynine,R.drawable.two ,R.drawable.nine,R.drawable.sixteen,R.drawable.twentythree,R.drawable.thirty,R.drawable.three,R.drawable.ten,R.drawable.seventin,R.drawable.twentyfour,R.drawable.thirtyone,R.drawable.four,R.drawable.eleven,R.drawable.eighteen,R.drawable.twentyfive,R.drawable.blank,R.drawable.five,R.drawable.twelve,R.drawable.nineteen,R.drawable.twentysix,R.drawable.blank,R.drawable.six,R.drawable.thirteen,R.drawable.twenty,R.drawable.twentyseven,R.drawable.blank,R.drawable.seven,R.drawable.fourteen,R.drawable.twentyone,R.drawable.twentyeight,R.drawable.blank};



    public January_Fragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_january_, null);
        gv = (GridView) view.findViewById(R.id.gridView1);
//        textView_date= (TextView) view.findViewById(R.id.txtdate);
//        String TextView_Date=textView_date.getText().toString();
//        int Text_Date= Integer.parseInt(TextView_Date);
        CustomAdapter customAdapter=new CustomAdapter(getActivity(),prgmImages);
        customAdapter.setCurrentFragmentMonth(0);
        // gv.setAdapter(new CustomAdapter(getActivity(), prgmImages));
        gv.setAdapter(customAdapter);

Design [I want to show back ground color blue( border color) particular date wise but my border color not showing current position and current date.Todays date is 05/01/2017 but it is showing on 29/01/2017.Which method is good for me.Thank You][2]

enter image description here

1

There are 1 best solutions below

5
On BEST ANSWER

I think problem is that you have sorted your array according to a calendar so that it will display the dates vertically but while highlighting the current date your considering position.

For e.g. current date is 5 which is at position no. 20 in your image array and at position 5 you have an image of 29 that's why it is getting highlighted.

One solution could be, you need to save the respective positions of image somewhere you can use hashmap for it. So the keys in your hashmap will be the dates of the month and value will be the image position in an array.

So your hashmap will look like

    +------------+------------------------+
    |    Date    | Position in img array  |
    +------------+------------------------+
    | 1          | 0                      |
    | 2          | 5                      |
    | 3          | 9                      |
    +------------+------------------------+

Now get today's date and compare this with hashmap keys and get the value of the position to be highlighted.

Check below code, According to your image array create hashmap.

public static int[] prgmImages = {R.drawable.one, R.drawable.eight, R.drawable.fifteen, R.drawable.twentytwo, R.drawable.twentynine,
                R.drawable.two, R.drawable.nine, R.drawable.sixteen, R.drawable.twentythree, R.drawable.thirty,
                R.drawable.three, R.drawable.ten, R.drawable.seventin, R.drawable.twentyfour, R.drawable.thirtyone,
                R.drawable.four, R.drawable.eleven, R.drawable.eighteen, R.drawable.twentyfive, R.drawable.blank,
                R.drawable.five, R.drawable.twelve, R.drawable.nineteen, R.drawable.twentysix, R.drawable.blank,
                R.drawable.six, R.drawable.thirteen, R.drawable.twenty, R.drawable.twentyseven, R.drawable.blank,
                R.drawable.seven, R.drawable.fourteen, R.drawable.twentyone, R.drawable.twentyeight, R.drawable.blank};

    HashMap<Integer, Integer> map = new HashMap<>();//Hashmap<DatesInMonth,ImagePositionInArray>
    map.put(1, 0);
    map.put(2, 5);
    map.put(3, 10);
    map.put(4, 15);
    map.put(5, 20);
    map.put(6, 25);
    map.put(7, 30);
    map.put(8, 1);
    map.put(9, 6);
    map.put(10, 11);
    map.put(11, 16);
    map.put(12, 21);
    map.put(13, 26);
    map.put(14, 31);
    map.put(15, 2);
    map.put(16, 7);
    map.put(17, 12);
    map.put(18, 17);
    map.put(19, 22);
    map.put(20, 27);
    map.put(21, 32);
    map.put(22, 3);
    map.put(23, 8);
    map.put(24, 13);
    map.put(25, 18);
    map.put(26, 23);
    map.put(27, 28);
    map.put(28, 33);
    map.put(29, 4);
    map.put(30, 9);
    map.put(31, 14);

    imgTv.setImageResource(imageId[position]);
    if (currentFragmentMonth != -1 && month == currentFragmentMonth && year == 2017) {

        int mapPosition = map.get(dayofmonth);

        if (mapPosition == position) {
            imgTv.setBackgroundColor(Color.BLUE);
        }

    }