代码之家  ›  专栏  ›  技术社区  ›  hermy

部分工作的ListView元素的动态显示

  •  1
  • hermy  · 技术社区  · 16 年前

    我正在制作一个时间表程序,用户在其中输入他的输入和输出冲头。我有一个从日历对象数组填充的ListView。我希望每一行显示日期和日期,然后在新行显示时间,但我只想显示日期和日期,如果它与前一个元素不同。

    目前,我正在使用position和position-1(用作数组的索引)进行比较,从而在BaseAdapter中设置可见性。只有当整个列表都显示在屏幕上时,这才有效。如果它扩展到屏幕之外,并且用户在屏幕上滚动,则结果是不可预测的。

    为了进一步混淆事情,我根据位置设置了时间的颜色,在绿色和红色(输入/输出)之间交替,它按预期工作,滚动或不滚动。

    Android如何处理滚动时的ListView位置,或者我可以做些什么来显示/隐藏日期和日期?

    public class TimeSheetActivity extends Activity {
    SQLiteDatabase timesDatabase;
    Cursor punchCursor;
    
    private static Calendar[] allPunches;
    
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.timesheet);
    } //end onCreate()
    
    
    @Override
    public void onResume() {
        super.onResume();
        //Open database
        timesDatabase = openOrCreateDatabase(
                "times_database.db",
                SQLiteDatabase.CREATE_IF_NECESSARY,
                null);
        timesDatabase.setLocale(Locale.getDefault());
        timesDatabase.setLockingEnabled(true);
        timesDatabase.setVersion(1);
        punchCursor = timesDatabase.query("Timepunches", null, null, null, null, null, "punch ASC;");
        updateTimeSheet();
    } //end onResume()
    
    
    @Override
    public void onPause() {
        super.onPause();
        timesDatabase.close();
    } //end onResume()
    
    
    private static class  EfficientAdapter extends BaseAdapter {
        private LayoutInflater mInflater;
    
        public EfficientAdapter(Context context) {
            mInflater = LayoutInflater.from(context);
        }
    
        public int getCount() {
            return allPunches.length;
        }
    
        public Object getItem(int position) {
            return position;
        }
    
        public long getItemId(int position) {
            return position;
        }
    
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.time_list_row, null);
                holder = new ViewHolder();
                holder.text1 = (TextView) convertView.findViewById(R.id.day_textview);
                holder.text2 = (TextView) convertView.findViewById(R.id.date_textview);
                holder.text3 = (TextView) convertView.findViewById(R.id.times_this_day_textview);
    
                convertView.setTag(holder);
            }
            else {
                holder = (ViewHolder) convertView.getTag();
            }
    
            String dayNames[] = new DateFormatSymbols().getWeekdays();
    
            //Initialize first list element
            if (position < 1) {
                holder.text1.setText(dayNames[allPunches[position].get(Calendar.DAY_OF_WEEK)]);
                holder.text2.setText(formatDate(allPunches[position]));
    
            }
            else {
                holder.text1.setText(dayNames[allPunches[position].get(Calendar.DAY_OF_WEEK)]);
                holder.text2.setText(formatDate(allPunches[position]));
                holder.text1.setVisibility(View.VISIBLE);
                holder.text2.setVisibility(View.VISIBLE);
    
                //Hide day and date if same as last
                if (formatDate(allPunches[position]).contentEquals(formatDate(allPunches[position-1]))) {
                    holder.text1.setVisibility(View.GONE);
                    holder.text2.setVisibility(View.GONE);
                }
            }
    
            holder.text3.setText(formatTime(allPunches[position], true) + " " + position);
    
            //Color in/out punches
            if (position%2 == 0) {
                holder.text3.setTextColor(Color.GREEN);
            }
            else {
                holder.text3.setTextColor(Color.RED);
            }
    
            return convertView;
        } //end getView()
    
        static class ViewHolder {
            public TextView text1;
            TextView text2;
            TextView text3;
        }
    } //end EfficientAdapter
    
    
    public void updateTimeSheet() {
        punchCursor = timesDatabase.query("Timepunches", null, null, null, null, null, "punch ASC;");
        allPunches = new Calendar[punchCursor.getCount()];
    
        int i = 0;                  //for indexing allPunches
        Calendar nextDay = Calendar.getInstance();
        nextDay.setLenient(true);
    
        //populate allPunches
        for (punchCursor.moveToFirst(); !punchCursor.isAfterLast(); punchCursor.moveToNext()) {
            allPunches[i] = Calendar.getInstance();
            allPunches[i].setTimeInMillis(punchCursor.getLong(0));
            ++i;
        } //end for
    
        final ListView timeSheetListView = (ListView)findViewById(R.id.timesheet_listview);
        timeSheetListView.setAdapter(new EfficientAdapter(this));
        timeSheetListView.setOnItemClickListener(new OnItemClickListener() {...}); //end click listener for list item
    } //end updateTimeSheet()
    
    
    public static String formatTime(Calendar thisTime, boolean showAMPM) {...}
    
    
    public static String formatDate(Calendar thisDate) {
        String formattedDate = "";
        formattedDate += thisDate.get(Calendar.MONTH) +"-"+ thisDate.get(Calendar.DAY_OF_MONTH) +"-"+ thisDate.get(Calendar.YEAR);
        return formattedDate;
    } //end formatDate()
    

    }//结束时间表活动

    2 回复  |  直到 16 年前
        1
  •  0
  •   Cheryl Simon    16 年前

    滚动时,ListView中的视图将重新显示。这可能会导致您看到的奇怪行为。重写getView时要记住的重要一点是每次都显式地设置行为。不要依赖于处于默认状态的视图,因为您可能正在重用已经更改的视图。

    在您的特定情况下,请确保始终将visibly显式设置为true或go。

    另外,您是否直接复制粘贴了此代码?我认为你的第二个else语句缺少一个结束括号。

        2
  •  0
  •   hermy    16 年前

    我好像放错地方了。下面是getView()的代码,似乎已经修复了!

        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.time_list_row, null);
                holder = new ViewHolder();
                holder.text1 = (TextView) convertView.findViewById(R.id.day_textview);
                holder.text2 = (TextView) convertView.findViewById(R.id.date_textview);
                holder.text3 = (TextView) convertView.findViewById(R.id.times_this_day_textview);
    
                convertView.setTag(holder);
            }
            else {
                holder = (ViewHolder) convertView.getTag();
            }
    
            String dayNames[] = new DateFormatSymbols().getWeekdays();
            holder.text1.setVisibility(View.VISIBLE);
            holder.text2.setVisibility(View.VISIBLE);
    
            //Initialize list
            if (position < 1) {
                holder.text1.setText(dayNames[allPunches[position].get(Calendar.DAY_OF_WEEK)]);
                holder.text2.setText(formatDate(allPunches[position]));
            }
            else {
                //Show day and date if not same as last
                holder.text1.setText(dayNames[allPunches[position].get(Calendar.DAY_OF_WEEK)]);
                holder.text2.setText(formatDate(allPunches[position]));
    
                if (formatDate(allPunches[position]).contentEquals(formatDate(allPunches[position-1]))) {
                    holder.text1.setVisibility(View.GONE);
                    holder.text2.setVisibility(View.GONE);
                }
            }
    
            holder.text3.setText(formatTime(allPunches[position], true));
    
            //Color in/out punches
            if (position%2 == 0) {
                holder.text3.setTextColor(Color.GREEN);
            }
            else {
                holder.text3.setTextColor(Color.RED);
            }
    
            return convertView;
        } //end getView()
    
        static class ViewHolder {
            public TextView text1;
            TextView text2;
            TextView text3;
        }
    } //end EfficientAdapter