使用游标适配器保存视图状态

时间:2015-08-28 06:02:25

标签: android android-contentprovider android-cursoradapter savestate

我有一个listview,其中我用光标适配器和内容提供者加载数据,数据加载和正确滚动除了背景,视图背景无法保存其状态....所以我尝试了我的每一个保存视图背景的方法,但我失败了...任何帮助将不胜感激。

包papuu.items;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import papu.R;
import papu.db.Order;

public class CustomCursorAdapter extends CursorAdapter {

    public CustomCursorAdapter(Context context, Cursor cursor) {
        super(context, cursor, 0);
    }

    // The newView method is used to inflate a new view and return it,
    // you don't bind any data to the view at this point.
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View vView = LayoutInflater.from(context).inflate(R.layout.listview_item_row, parent, false);
        vView.setTag(new ViewHolder(vView));
        // no need to bind data here. you do in later
        return vView;// **EDITED:**need to return the view
    }

    // The bindView method is used to bind all data to a given view
    // such as setting the text on a TextView.
    @Override
    public void bindView(View row, Context context, Cursor cursor) {
        Order cPerson = new Order(cursor);

//here the row background is not able to save its background state, as i scroll the row background gets shuffled
        if (cPerson.status == 3)
            row.setBackgroundColor(Color.parseColor("#8FCC85"));  //Green Accepted
        else if (cPerson.status == 0)
            row.setBackgroundColor(Color.parseColor("#B0BEC5"));    //gray Picked Up

        ViewHolder vh = (ViewHolder) row.getTag();

        vh.restaurentName.setText(cPerson.restaurantName);
        vh.address.setText(cPerson.customerAddress);
        DateFormat df = new SimpleDateFormat("yyyy'-'mm'-'dd'T'hh':'mm':'SS'Z'");
        df.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date date;
        int mins = 0;
        int hrs = 0;
        try {
            date = df.parse(cPerson.pickupTime);

            hrs = (int) ((date.getTime() / (1000 * 60 * 60)) % 24);
            mins = (int) ((date.getTime() / (1000 * 60)) % 60);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        // Log.e(cPerson.status + "",mins+"");
        vh.pickupTime.setText(hrs + ":" + mins);
        vh.price.setText("Tk. " + cPerson.status);
    }

    public class ViewHolder {
        TextView restaurentName;
        TextView address;
        TextView pickupTime;
        TextView price;

        ViewHolder(View row) {
            restaurentName = (TextView) row.findViewById(R.id.tv_restaurent_name);
            address = (TextView) row.findViewById(R.id.tv_address);
            pickupTime = (TextView) row.findViewById(R.id.tv_pickuptime);
            price = (TextView) row.findViewById(R.id.tv_price);
        }
    }
}

0 个答案:

没有答案