【问题标题】:Highlight a particulatr position in List View在列表视图中突出显示特定位置
【发布时间】:2016-01-25 03:44:00
【问题描述】:

对不起,如果问题似乎比正常时间长或难以理解。在我的任务中,我必须显示一个 listview,它将日期和标题显示为字符串值。我正在使用 Arraylist 使用模型类将数据传递给适配器。

vaccinationScheduleAdapter=new VaccinationScheduleAdapter(mActivity,DateOfBirth,VaccShdleModelArrayList);
        lstMenus.setAdapter(vaccinationScheduleAdapter);

其中 DateOfBirth 是 DateValue,可以更改。VaccShdleModelArrayList 的值是通过模型类传递的

public String getmDescription() {
    return mDescription;
}
public String getmNumberOfMonths() {
    return mNumberOfMonths;
}

列表视图中显示的日期将使用传递的值 DateOfBirth 和 mNumberOfMonths 计算。计算将在自定义适配器类中执行

NumberOfMonths = Integer.parseInt(atrNumberOfMonths);
        Calendar cal = Calendar.getInstance();
        cal.setTime(DateOfBirth);
        cal.add(Calendar.MONTH, NumberOfMonths);

例如 getmNumberOfMonths 将返回 1,3,5,7,.. 然后它将添加到传递的月份 DateOfBirth。如果我传递了 01/08/2015 计算后的结果将是 01/09/2015,01/10/2015,01/12/2015,01/03/2015... 如果它不包含当前日期,我想突出显示下一个未来日期。 我设法通过对当前日期之后的日期进行排序来找到需要突出显示的位置,但问题是排序和查找仅在适配器的位置为最后时发生。请帮我这样做。卡在这个问题上很久了。

这是适配器类,

    public class VaccinationScheduleAdapter extends BaseAdapter {
    private ArrayList<VaccinationScheduleModel> mListItems;
    private Activity mActivity;
    private LayoutInflater mInflater;
    private Integer[] mListIcons;
    private Date DateOfBirth;
    private int NumberOfMonths;
    private ArrayList<VaccinationScheduleAppoinmentModel> SottList = new ArrayList<>();
    VaccinationScheduleAppoinmentModel ap;
    CalenderModel calModel = new CalenderModel();
    private int maxarraySize;
    public static int nextvalue;

    public VaccinationScheduleAdapter(Activity mActivity, Date DateOfBirth, ArrayList<VaccinationScheduleModel> mListItems) {
//        super();
        this.maxarraySize = mListItems.size();
        this.mActivity = mActivity;
        this.mListItems = mListItems;
        this.DateOfBirth = DateOfBirth;
        mInflater = (LayoutInflater) mActivity.getLayoutInflater();
    }

    @Override
    public int getCount() {
        return mListItems.size();
    }

    @Override
    public Object getItem(int position) {
        return mListItems.get(position);
    }

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.custom_lv_vacc_schedule,
                    parent, false);
        } else {
            convertView = convertView;
        }

        String atrNumberOfMonths = mListItems.get(position).getmNumberOfMonths();
        NumberOfMonths = Integer.parseInt(atrNumberOfMonths);
        Calendar cal = Calendar.getInstance();
        cal.setTime(DateOfBirth);
        cal.add(Calendar.MONTH, NumberOfMonths);
        Date result = cal.getTime();

        String dayOfTheWeek = (String) android.text.format.DateFormat.format("EEEE", result);//Thursday
        String stringMonth = (String) android.text.format.DateFormat.format("MMM", result); //Jun
        String intMonth = (String) android.text.format.DateFormat.format("MM", result); //06
        String year = (String) android.text.format.DateFormat.format("yyyy", result); //2013
        String day = (String) android.text.format.DateFormat.format("dd", result); //20
        // System.out.println("19102015:VaccinationScheduleAdapter" + result);
        TextView txtVaccTitle = (TextView) convertView.findViewById(R.id.txtVaccTitle);
        TextView txtVaccMonth = (TextView) convertView.findViewById(R.id.txtVaccMonth);
        TextView txtVaccDay = (TextView) convertView.findViewById(R.id.txtVaccDay);
        TextView txtVaccYear = (TextView) convertView.findViewById(R.id.txtVaccYear);
        RelativeLayout relrow = (RelativeLayout) convertView.findViewById(R.id.relrow);
        ImageView imgIcon = (ImageView) convertView.findViewById(R.id.imgIcon);
        imgIcon.setBackgroundResource(R.drawable.calendar_icon);
        txtVaccTitle.setText(mListItems.get(position).getmDescription());
        txtVaccMonth.setText(stringMonth);
        txtVaccDay.setText(day);
        txtVaccYear.setText(year);
        Calendar cal2 = Calendar.getInstance();
        int comresult = cal.compareTo(cal2);

        System.out.println("27102015 cal1 Added date:"+cal);
        System.out.println("27102015 cal1 Current date:"+cal2);
//        System.out.println("22102015:maxarraySize" + maxarraySize);
//        System.out.println("22102015:position" + position);
        System.out.println("22102015:comresult"+comresult);

        if (position == (maxarraySize - 1)) {

            Collections.sort(SottList, new ScheduleDateComp());
            System.out.println("Sorted list entries: ");
            for (VaccinationScheduleAppoinmentModel e : SottList) {
                System.out.println(e);
            }
            if (SottList.size() != 0) {
                int aortlistposition = SottList.get(0).getposition();
                System.out.println("26102015:highlight position" + aortlistposition);
                System.out.println("26102015:current position" + position);
                nextvalue = aortlistposition;
                if (position == aortlistposition) {
                    relrow.setBackgroundResource(R.color.listview_blueback);
                }
            } else {

            }
        }
        //  System.out.println("comresult" + comresult);
        if (isSameDay(cal, cal2)) {
//            convertView.setBackgroundColor(Color.BLACK);
//            calModel.mCaldate = cal;
//            calModel.mPosition = position;
            System.out.println("isSameDay");
//            convertView.setBackgroundResource(R.drawable.list_selector_highlight);
//            txtVaccTitle.setTextColor(Color.WHITE);
//            txtVaccDay.setTextColor(Color.WHITE);
//            txtVaccMonth.setTextColor(Color.WHITE);
//            txtVaccYear.setTextColor(Color.WHITE);
        }
        else if (comresult < 0) {

            convertView.setBackgroundColor(Color.TRANSPARENT);
            txtVaccTitle.setTextColor(mActivity.getResources().getColor(R.color.listview_textcolor));
            txtVaccDay.setTextColor(mActivity.getResources().getColor(R.color.listview_textcolor));
            txtVaccMonth.setTextColor(mActivity.getResources().getColor(R.color.listview_textcolor));
            txtVaccYear.setTextColor(mActivity.getResources().getColor(R.color.listview_textcolor));
//            txtVaccTitle.setTextColor(Color.WHITE);
//            txtVaccDay.setTextColor(Color.WHITE);
//            txtVaccMonth.setTextColor(Color.WHITE);
//            txtVaccYear.setTextColor(Color.WHITE);


        }
       else {
            if ((comresult > 0)) {
                ap = new VaccinationScheduleAppoinmentModel(result, position);
                SottList.add(ap);
                // convertView.setBackgroundColor(Color.BLACK);
                convertView.setBackgroundResource(R.drawable.list_selector_highlight);

                Collections.sort(SottList, new ScheduleDateComp());


                if ((position == SottList.get(0).getposition()) ) {//&& !isSameDay(cal, cal2)

                    convertView.setBackgroundResource(R.drawable.list_selector_highlight);
                    txtVaccTitle.setTextColor(Color.WHITE);
                    txtVaccDay.setTextColor(Color.WHITE);
                    txtVaccMonth.setTextColor(Color.WHITE);
                    txtVaccYear.setTextColor(Color.WHITE);

                } else {
                    convertView.setBackgroundColor(Color.TRANSPARENT);
                    txtVaccTitle.setTextColor(mActivity.getResources().getColor(R.color.listview_textcolor));
                    txtVaccDay.setTextColor(mActivity.getResources().getColor(R.color.listview_textcolor));
                    txtVaccMonth.setTextColor(mActivity.getResources().getColor(R.color.listview_textcolor));
                    txtVaccYear.setTextColor(mActivity.getResources().getColor(R.color.listview_textcolor));

                }
                //  checkNextDay();

            }
        }


        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


            }
        });

        return convertView;
    }


    public int nextday() {
        return nextvalue;
    }

    public boolean isSameDay(Calendar cal1, Calendar cal2) {
        if (cal1 == null || cal2 == null)
            return false;
        return (cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA)
                && cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
                && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR));
    }


    class ScheduleDateComp implements Comparator<VaccinationScheduleAppoinmentModel> {


        @Override
        public int compare(VaccinationScheduleAppoinmentModel ap1, VaccinationScheduleAppoinmentModel ap2) {
            if ((ap1.getDate()).after((ap2.getDate())))

            {
                return 1;
            } else {
                return -1;
            }
        }
    }
}

如果有人可以帮助我,我将非常感激。提前致谢。

【问题讨论】:

    标签: java android date android-listview baseadapter


    【解决方案1】:

    为了突出显示列表位置您需要设置listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 然后listview.setSelection(int position);

    【讨论】:

    • 我有这个 lstMenus.setSelection(vaccinationScheduleAdapter.nextday());但是之后该怎么办?
    【解决方案2】:

    您可以在对 VaccinationScheduleAppoinmentModel 进行排序后维护一个未来日期位置数组,实现 compareto()。您可以查看此链接以了解 [Sort ArrayList of custom Objects by property.应用您的排序逻辑并从此排序列表中维护一组未来日期位置。您可以将列表视图选择模式设置为多个,并为您需要的位置设置选择状态。

    【讨论】:

      【解决方案3】:

      正如阿基尔所说,

      listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE); then      
      listview.setSelection(int position);
      

      在你这样做之后,你需要设置你想要高的项目 itemView.setSelected(true),我相信这将是一个变量来判断项目是否会被高。 如果仍然无法解决,请考虑您的项目视图的背景是否与选择器文件正确。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-14
        • 2012-06-10
        • 1970-01-01
        • 2012-11-21
        • 2015-03-19
        • 2011-10-06
        • 2013-12-22
        • 2012-09-26
        相关资源
        最近更新 更多