Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customized ListView when scrolling changes randomly button text or edittext

I have searched for this one but to no avail I haven't get the exact answer to the issue.

I have a button inside a ListView, when that button is clicked it shows an alert dialog to choose a date and after choosing date, the selected date will be the text of the button. The code works fine but when i scroll down and up the date will be randomly change here is my code for the getView() method of my adapter.This also happens to the EditText when i type to any of the EditText works fine but when i start scrolling the values also change.

Here is my get view method

public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder viewHolder;
    if(convertView == null){
        convertView = layoutInflater.inflate(R.layout.repeat_entry_listview,null);

        viewHolder = new ViewHolder();
        viewHolder.btnDate = (Button) convertView.findViewById(R.id.rpbtnDate);
        viewHolder.txtNotes  = (EditText) convertView.findViewById(R.id.rpNotes);
        convertView.setTag(viewHolder);
    }else{
        viewHolder = (ViewHolder) convertView.getTag();
    }


    RepeatEntryList repeatEntryList = listData.get(position);
    viewHolder.btnDate.setText(repeatEntryList.getDate());
    viewHolder.btnDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          //EVEN IF I REMOVE THE CODE FOR SETTING THE BUTTON TEXT TO JUST LIKE THE BELOW CODE. SCROLLING ISSUES STILL PERSIST

          viewHolder.btnDate.setText("just sample date text");

            //**NOTE UPDATED**
            //MY EDITTEXT ALSO HAVE THE SAME BEHAVIOR
        }
    });
    viewHolder.txtNotes.setHint(contx.getResources().getString(R.string.placeholder_txt));
    return convertView;
}

Note Let's say i have 20 buttons and 20 EditText inside the ListView. When i type something to all of the EditText and change the text of the button by clicking it ,then scroll down and up the ListView ,the text or values of EditText and button will change or interchange and sometimes gone.

With regards to my problem can someone explain to me why ListView is behaving like this and how to avoid this certain behavior.

like image 448
jameshwart lopez Avatar asked Dec 04 '22 03:12

jameshwart lopez


2 Answers

make changes in your code remove if(convertView == null){ }else{} condition and directly use as follow

convertView = layoutInflater.inflate(R.layout.repeat_entry_listview,null);
Button btnDate = (Button) convertView.findViewById(R.id.rpbtnDate);
TextView txtNotes  = (EditText) convertView.findViewById(R.id.rpNotes);
like image 64
Piyush Malaviya Avatar answered Mar 16 '23 00:03

Piyush Malaviya


Just add below line, repeatEntryList.setDate("Selected Date")

datePickerTo.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                                String strdate = (pickerTo.getMonth() + 1) + "-" + pickerTo.getDayOfMonth() + "-" + pickerTo.getYear();

                                viewHolder.btnDate.setText(mainActivity.pickerStringTodate(strdate));

//Here is your new line
repeatEntryList.setDate(mainActivity.pickerStringTodate(strdate))


                                try {
                                    Date date1 = mainActivity.dateFormat.parse(viewHolder.btnDate.getText().toString());
                                    Date date2 = mainActivity.dateFormat.parse(mainActivity.dateFormat.format(new Date()));
                                    if (date1.before(date2)) {
                                        Toast.makeText(contx, "You cannot select past date", Toast.LENGTH_LONG).show();
                                        viewHolder.btnDate.setText(mainActivity.dateFormat.format(new Date()));
                                    }
                                } catch (ParseException e) {
                                    e.printStackTrace();
                                }
                            }
                        }).setNegativeButton("Cancel", null);
                        datePickerTo.show();
like image 45
Sangharatna Avatar answered Mar 16 '23 01:03

Sangharatna