Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I need help to fix this problem, i had a listview which datas keep change when i scrolling listview, here is my code
Java
public class ListViewAdapterTeam extends ArrayAdapter<rekapabsensiclass> {

        public List<rekapabsensiclass> team;
        private Context context;

        public ListViewAdapterTeam(List<rekapabsensiclass> team, Context context) {
            super(context, R.layout.list_item_rekapabsensi, team);
            this.team = team;
            this.context = context;
        }


Java
@SuppressLint("NewApi")
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater = LayoutInflater.from(context);

            final View listViewItem = inflater.inflate(R.layout.list_item_rekapabsensi, null, true);

            listViewItem.setBackground(getContext().getDrawable(R.drawable.border));
            TextView nikbaruteam = listViewItem.findViewById(R.id.nik);
            TextView nama = listViewItem.findViewById(R.id.nama);
            final TextView telat = listViewItem.findViewById(R.id.telat);
            final TextView alfa = listViewItem.findViewById(R.id.alfa);
            final TextView sick = listViewItem.findViewById(R.id.sakit);
            final TextView hadir = listViewItem.findViewById(R.id.hadir);
            final TextView f1 = listViewItem.findViewById(R.id.F1);
            final TextView f4 = listViewItem.findViewById(R.id.F4);

            rekapabsensiclass movieItem = team.get(position);
            System.out.println("position =" + position);

            nikbaruteam.setText(movieItem.getNik_baru());
            nama.setText(movieItem.getNama_karyawan_struktur());


Java
Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String firstdate = simpleDateFormat.format(calendar.getTime());
            System.out.println("Test tanggal 1 =" + firstdate);

            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
            String lastdate = sdf2.format(new Date());
            System.out.println("Test tanggal 2 =" + lastdate);

            String nik_baru = getItem(position).getNik_baru();
            System.out.println("nik222 =" + nik_baru);

            StringRequest stringRequest = new StringRequest(Request.Method.GET, "http://example.co.id/rest_server/api/absensi/index?shift_day="+ firstdate +"&shift_day_2="+ lastdate +"&badgenumber="+ nik_baru,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            try {
                                JSONObject obj = new JSONObject(response);
                                JSONArray movieArray = obj.getJSONArray("data");
                                int number = 0;
                                int number1 = 0;
                                int number2 = 0;
                                int number3 = 0;
                                int number4 = 0;
                                int number5 = 0;

                                for (int i = 0; i < movieArray.length(); i++) {

                                    JSONObject movieObject = movieArray.getJSONObject(i);

                                    movieObject.getString("shift_day");
                                    movieObject.getString("F1");
                                    movieObject.getString("depo_f1");
                                    movieObject.getString("F4");
                                    movieObject.getString("depo_f4");
                                    movieObject.getString("ket_absensi");

                                    if (movieObject.getString("ket_absensi").contains("TL"))
                                        number1++; {
                                        telat.setText(String.valueOf(number1));
                                    }

                                    if (movieObject.getString("ket_absensi").contains("AL"))
                                        number2++; {
                                        alfa.setText(String.valueOf(number2));
                                    }

                                    if (movieObject.getString("ket_absensi").contains("SA"))
                                        number3++; {
                                        sick.setText(String.valueOf(number3));
                                    }

                                    if (movieObject.getString("ket_absensi").contains("HD"))
                                        number++;  {
                                        hadir.setText(String.valueOf(number));
                                    }
                                    if (movieObject.getString("ket_absensi").contains("TD F1"))
                                        number4++; {
                                        f1.setText(String.valueOf(number4));
                                    }
                                    if (movieObject.getString("ket_absensi").contains("TD F4"))
                                        number5++; {
                                        f4.setText(String.valueOf(number5));
                                    }
                                    hideDialog();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {

                        }
                    });
            mQueue.add(stringRequest);

            return listViewItem;
        }
    }


What I have tried:

The data keeps change whe scrolling to zero ("0")
and the position count is not full to down

example :
020-11-26 10:54:22.310 23221-23221/com.tvip.eis2 I/System.out: position =0
2020-11-26 10:54:22.319 23221-23221/com.tvip.eis2 I/System.out: position =1
2020-11-26 10:54:22.329 23221-23221/com.tvip.eis2 I/System.out: position =2
2020-11-26 10:54:22.336 23221-23221/com.tvip.eis2 I/System.out: position =3
2020-11-26 10:54:22.343 23221-23221/com.tvip.eis2 I/System.out: position =4
2020-11-26 10:54:22.352 23221-23221/com.tvip.eis2 I/System.out: position =5
2020-11-26 10:54:22.358 23221-23221/com.tvip.eis2 I/System.out: position =6
2020-11-26 10:54:22.365 23221-23221/com.tvip.eis2 I/System.out: position =7

when i scrolling down, the position is :
2020-11-26 10:59:52.794 23221-23221/com.tvip.eis2 I/System.out: position =8
2020-11-26 10:59:52.806 23221-23221/com.tvip.eis2 I/System.out: position =9
2020-11-26 10:59:52.850 23221-23221/com.tvip.eis2 I/System.out: position =10
2020-11-26 10:59:52.886 23221-23221/com.tvip.eis2 I/System.out: position =11
2020-11-26 10:59:52.901 23221-23221/com.tvip.eis2 I/System.out: position =12
2020-11-26 10:59:52.919 23221-23221/com.tvip.eis2 I/System.out: position =13
2020-11-26 10:59:52.949 23221-23221/com.tvip.eis2 I/System.out: position =14
2020-11-26 10:59:52.965 23221-23221/com.tvip.eis2 I/System.out: position =15
2020-11-26 10:59:52.996 23221-23221/com.tvip.eis2 I/System.out: position =16
Posted
Updated 25-Nov-20 17:25pm
v2
Comments
[no name] 27-Nov-20 12:58pm    
It may mean something to you, but what you've said and shown is pretty well meaningless to everyone else. Can't even tell what the data types are.
David Crow 28-Nov-20 11:15am    
How many items are in the adapter? How many rows is the ListView able to display at once? What does "scrolling to zero" mean?
Komang Putra 28-Nov-20 11:29am    
I mean like this...
In 1st position the data equals 4,
Then i scroll it to bottom, after that i scroll to top and the data im 1st position change to 0
David Crow 28-Nov-20 19:43pm    
Scrolling a ListView will not change the contents of the underlying adapter. The position of a particular item in the ListView should be of no concern. You use it in getView() but usually for the sole purpose of indexing into the adapter's list.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900