开发者

Android ListView not working like I intend

I am new to Android and also it's been a while since I worked with Java, and I'm just trying to create something on my own. Basically I ended up copying the code from this Android Custom Listview Tutorial and I'm trying to modify it so I can create a calendar view with some special behaviour. At this point, I just want it to display the current month and show Sundays and the current day using a different background colour. Here's my version of the adapter class:

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.util.Log;
import android.view.LayoutInflater;

import java.util.Calendar;

public class MonthViewAdapter extends BaseAdapter {

public MonthViewAdapter(Context c) {
    mInflater = LayoutInflater.from(c);
    now = Calendar.getInstance();
}

@Override
public int getCount() {
    int numDays = now.getActualMaximum(Calendar.DAY_OF_MONTH);
    Log.i(this.toString(), "getCount -- numDays = " + numDays);
    return numDays;
}

@Override
public Object getItem(int arg0) {
    return arg0;
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.main_list_element, null);

        holder = new ViewHolder();
        holder.dateText = (TextView)  convertView.findViewById(R.id.dateText);          
        holder.eventText = (TextView) convertView.fin开发者_如何学运维dViewById(R.id.eventText);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();

    }

    holder.dateText.setText(String.valueOf(position + 1));
    Log.i(this.toString(), "getView -- dateText = " + holder.dateText.getText());
    holder.eventText.setText("events" + String.valueOf(position + 1));

    Calendar refCal = Calendar.getInstance(); 
    refCal.set(Calendar.DAY_OF_MONTH, position+1);
    if(now.get(Calendar.DAY_OF_MONTH) == position+1)
        convertView.setBackgroundColor(Color.GRAY);
    else if(refCal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
        convertView.setBackgroundColor(Color.DKGRAY);

    return convertView;
}

static class ViewHolder {
    TextView dateText;
    TextView eventText;
}

private Context mContext;
private LayoutInflater mInflater;
private Calendar now;

}

It appears to work at first, but when I start scrolling the view, it starts screwing up the intended background colours. My first question is why this is happening and the second question is why it doesn't screw up the text fields consistently with the colours, since both should be reset using the value of the position argument.


http://developer.android.com/reference/android/widget/AbsListView.html#attr_android:cacheColorHint set this attribute in your xml declaration of the listview. Set the desired background.

android:cacheColorHint="@color/bmy_background"


You need to also provide a default case in your color-setting if statements:

if(now.get(Calendar.DAY_OF_MONTH) == position+1) {
    convertView.setBackgroundColor(Color.GRAY);
} else if(refCal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
    convertView.setBackgroundColor(Color.DKGRAY);
} else {
    convertView.setBackgroundColor(Color.BLACK);
}

Otherwise, you aren't resetting the backgrounds of your recycled views. Your TextView setters worked correctly because they are triggered unconditionally.


You're right to want to understand what is happening here.

It is explained by Romain Guy on the Android website and is an optimisation.

To use transparent, set android:cacheColorHint="#00000000" in your XML file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜