当前位置: 代码迷 >> Android >> 突出显示列表视图中的特定位置
  详细解决方案

突出显示列表视图中的特定位置

热度:64   发布时间:2023-08-04 12:00:36.0

很抱歉,如果这个问题似乎比正常情况长或难以理解。在我的任务中,我必须显示一个列表视图 ,该列表视图将日期和标题显示为字符串值。我正在使用Arraylist ,它使用模型类将数据传递给适配器。

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

凡出生日期是DateValue将,这对于changed.The值VaccShdleModelArrayList通过模型类传递

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

listview中显示的日期将使用传递的值DateOfBirth和mNumberOfMonths计算。该计算将在Custom Adapter类中执行

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;
            }
        }
    }
}

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

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

在对VaccinationScheduleAppoinmentModel进行了compareto()排序之后,可以维护将来的日期位置数组。 您可以查看此链接,以了解如何[ 。 应用排序逻辑并从该排序列表中维护将来的日期位置数组。 您可以将listview选择模式设置为多个,并为所需位置设置选择状态。

正如阿基尔(Akhil)所说,

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

完成此操作后,您需要将要设置的项目设置为高出itemView.setSelected(true),我相信它将是一个变量,用于判断该项目是否将被高出。 如果仍然无法解决,请考虑选择器文件的项目视图背景是否正确。

  相关解决方案