OnScroll也在其他listitem中选择了日期显示

时间:2016-03-11 08:28:14

标签: android android-alertdialog baseadapter android-datepicker

我有适用于listview的适配器,当我点击public class CustomAdapterPooja extends BaseAdapter { private final LayoutInflater inflater; private final Context context; private List<ModelPooja> listData; public CustomAdapterPooja(Context mainActivity, List<ModelPooja> listData) { context = mainActivity; this.listData = listData; inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { return listData.size(); } @Override public Object getItem(int position) { return listData.get(position); } @Override public long getItemId(int position) { return 0; } @Override public View getView(final int position, View convertView, ViewGroup parent) { final ViewHolder holder; dtlist=new ArrayList<String>(); if (convertView == null) { holder = new ViewHolder(); convertView = inflater.inflate(R.layout.list_item_poojaselection, null); holder.tv = (TextView) convertView.findViewById(R.id.list_item_poojaname); holder.showsdates = (TextView) convertView.findViewById(R.id.selecteddatess); holder.checks = (CheckBox) convertView.findViewById(R.id.list_item_poojacheck); convertView.setTag(holder); }else { holder = (ViewHolder) convertView.getTag(); } holder.checks.setOnCheckedChangeListener(null); holder.checks.setFocusable(false); if (listData.get(position).isselected) { holder.checks.setChecked(true); } else { holder.checks.setChecked(false); } holder.checks.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton cb, boolean b) { System.out.println("hello" + listData.get(position).getPOOJA_LISTING_AMOUNT()); dialog = new Dialog(PoojaSelection.this); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setContentView(R.layout.custom_dialog); TextView text = (TextView) dialog.findViewById(R.id.dialog_name); ImageView cal = (ImageView) dialog.findViewById(R.id.imgcalndr); final TextView textcal = (TextView) dialog.findViewById(R.id.dialog_dt); TextView textdays = (TextView) dialog.findViewById(R.id.dialog_days); String amt = aList.get(position).getPOOJA_LISTING_AMOUNT(); String pojjaday = aList.get(position).getPOOJA_LISTING_DAYS(); pojjaid = aList.get(position).getPOOJA_LISTING_ID(); String pojjanms = aList.get(position).getPOOJA_LISTING_NAME(); Calendar newCalendar = Calendar.getInstance(); datePickerDialog = new DatePickerDialog(PoojaSelection.this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { sYear = year; sMonth = monthOfYear; sDate = dayOfMonth; textcal.setText(new StringBuilder() // Month is 0 based, just add 1 .append(sYear).append("-").append(sMonth + 1).append("-").append(sDate)); selecdts = textcal.getText().toString(); } }, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH)); datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000); cal.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { datePickerDialog.show(); } }); textdays.setText("Available Days " + pojjaday); text.setText(pojjanms); TextView dialogButton = (TextView) dialog.findViewById(R.id.dialog_wishafriend); dialogButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { holder.showsdates.setText(selecdts); if (selecdts != null) { new LOADPOINTS().execute(); } else { Toast.makeText(PoojaSelection.this, "Date Not Selected", Toast.LENGTH_SHORT).show(); } } }); dialog.show(); if (checkMaxLimit()) { if (listData.get(position).isselected && b) { holder.checks.setChecked(false); listData.get(position).isselected = false; } else { holder.checks.setChecked(false); listData.get(position).isselected = false; Toast.makeText(context, "Max limit reached", Toast.LENGTH_SHORT).show(); } } else { if (b) { listData.get(position).isselected = true; } else { listData.get(position).isselected = false; } } } }); holder.tv.setText(listData.get(position).getPOOJA_LISTING_NAME()); return convertView; } public boolean checkMaxLimit() { int countermax = 0; for(ModelPooja item : listData){ if(item.isselected){ countermax++; } } return countermax >= 5; } public class ViewHolder { TextView tv; public CheckBox checks; TextView showsdates; } } 我打开警报dailog时,在我的警报dailog我有datepicker,现在我选择的任何日期我需要在listitem的textview中显示,我能够显示文字,但问题是如果我选择第一个listitem的日期然后我滚动我可以弄清楚,在我的第6个listitem它也被选中

Mybaseadapter

{{1}}

1 个答案:

答案 0 :(得分:0)

像这样修改你的getViewMethod()(我只在代码中添加2行,请参阅评论)

public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    dtlist=new ArrayList<String>();
    if (convertView == null) {
        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.list_item_poojaselection, null);
        holder.tv = (TextView) convertView.findViewById(R.id.list_item_poojaname);
        holder.showsdates = (TextView) convertView.findViewById(R.id.selecteddatess);
        holder.checks = (CheckBox) convertView.findViewById(R.id.list_item_poojacheck);
        convertView.setTag(holder);
    }else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.checks.setOnCheckedChangeListener(null);
    holder.checks.setFocusable(false);

    if (listData.get(position).isselected) {
        holder.checks.setChecked(true);
    } else {
        holder.checks.setChecked(false);
    }

    holder.checks.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton cb, boolean b) {
            System.out.println("hello" + listData.get(position).getPOOJA_LISTING_AMOUNT());
            dialog = new Dialog(PoojaSelection.this);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(R.layout.custom_dialog);
            TextView text = (TextView) dialog.findViewById(R.id.dialog_name);
            ImageView cal = (ImageView) dialog.findViewById(R.id.imgcalndr);
            final TextView textcal = (TextView) dialog.findViewById(R.id.dialog_dt);
            TextView textdays = (TextView) dialog.findViewById(R.id.dialog_days);
            String amt = listData.get(position).getPOOJA_LISTING_AMOUNT();
            String pojjaday = listData.get(position).getPOOJA_LISTING_DAYS();
            pojjaid = listData.get(position).getPOOJA_LISTING_ID();
            String pojjanms = listData.get(position).getPOOJA_LISTING_NAME();
            Calendar newCalendar = Calendar.getInstance();
            datePickerDialog = new DatePickerDialog(PoojaSelection.this, new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    sYear = year;
                    sMonth = monthOfYear;
                    sDate = dayOfMonth;
                    textcal.setText(new StringBuilder()
                            .append(sYear).append("-").append(sMonth + 1).append("-").append(sDate));
                    selecdts = textcal.getText().toString();
                }
            }, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
            datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
            cal.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    datePickerDialog.show();
                }
            });

            textdays.setText("Available Days " + pojjaday);
            text.setText(pojjanms);
            TextView dialogButton = (TextView) dialog.findViewById(R.id.dialog_wishafriend);
            dialogButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listData.get(position).setSelectedDate(selecdts); // I ADD THIS
                    holder.showsdates.setText(selecdts);
                    if (selecdts != null) {

                        new LOADPOINTS().execute();
                    } else {
                        Toast.makeText(PoojaSelection.this, "Date Not Selected", Toast.LENGTH_SHORT).show();
                    }
                }
            });
            dialog.show();

            if (checkMaxLimit()) {

                if (listData.get(position).isselected && b) {
                    holder.checks.setChecked(false);
                    listData.get(position).isselected = false;

                } else {
                    holder.checks.setChecked(false);
                    listData.get(position).isselected = false;
                    Toast.makeText(context, "Max limit reached", Toast.LENGTH_SHORT).show();
                }
            } else {
                if (b) {
                    listData.get(position).isselected = true;
                } else {
                    listData.get(position).isselected = false;
                }
            }
        }
    });


    holder.showsdates.setText(listData.get(position).getSelectedDate()); // I ADD THIS
    holder.tv.setText(listData.get(position).getPOOJA_LISTING_NAME());
    Log.i("TAG",position+" - "+listData.get(position).getPOOJA_LISTING_NAME());
    return convertView;
}

还在selectedDate

中创建字段ModelPooja
public class ModelPooja {

...
    private String selectedDate;

    public void setSelectedDate(String selectedDate) {
        this.selectedDate = selectedDate;
    }

    public String getSelectedDate() {
        return selectedDate;
    }
...
}

有效。希望这个帮助