基础Loader数据更改时Listview不会刷新

时间:2014-08-11 01:49:20

标签: android listview android-listview loader

首先,我将问题的前提是我没有使用CursorLoader

我从SQLlite数据库中提取数据以填充ListFragment中的列表视图。初始加载效果很好,但是一旦操作了数据(即对列表进行了添加),listview就不会刷新以显示新数据。我正在实现像这样的Loader回调:

public class BillListingFragment extends ListFragment implements LoaderManager.LoaderCallbacks<List<Bill>> {

private billListAdapter mAdapter;
private static final int LOADER_ID = 1;
private SQLiteDatabase mDatabase;
private BillsDataSource mDataSource;
private BillsStoreDatabaseHelper mDbHelper;
/**
 * The fragment argument representing the fragment type (archive or outstanding)
 */
private static final String ARG_FRAGMENT_TYPE = "fragment_type";

/**
 * Returns a new instance of this fragment based on type
 */
public static BillListingFragment newInstance(String type) {
    // TODO: Make the fragment type an enum
    BillListingFragment fragment = new BillListingFragment();
    Bundle args = new Bundle();
    args.putString(ARG_FRAGMENT_TYPE, type);
    fragment.setArguments(args);
    return fragment;
}

public BillListingFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.bill_view_layout, container, false);
    return rootView;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    mDbHelper = new BillsStoreDatabaseHelper(getActivity());
    mDatabase = mDbHelper.getWritableDatabase();
    mDataSource = new BillsDataSource(mDatabase);

    mAdapter = new billListAdapter(getActivity(), R.layout.bill_row_layout);
    setListAdapter(mAdapter);
    getLoaderManager().initLoader(LOADER_ID, null, this);
}

@Override
public Loader<List<Bill>> onCreateLoader(int id, Bundle args) {
    BillDataLoader loader = new BillDataLoader(getActivity(), mDataSource);
    return loader;
}

@Override
public void onLoadFinished(Loader<List<Bill>> loader, List<Bill> data) {
    for(Bill bill: data){
        mAdapter.add(bill);
    }
    setListAdapter(mAdapter);
}

@Override
public void onLoaderReset(Loader<List<Bill>> loader) {
    mAdapter.clear();
}

@Override
public void onDestroy() {
    super.onDestroy();
    mDbHelper.close();
    mDatabase.close();
    mDataSource = null;
    mDbHelper = null;
    mDatabase = null;
}

public void reload(){
    getLoaderManager().restartLoader(LOADER_ID, null, this);
}

private class billListAdapter extends ArrayAdapter<Bill> {
    Context context;

    public billListAdapter(Context context, int resourceID){
        super(context, resourceID);
        this.context = context;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = getActivity().getLayoutInflater().inflate(R.layout.bill_row_layout, parent, false);
        }
        TextView payToField = (TextView) convertView.findViewById(R.id.nameField);
        TextView dueDateField = (TextView) convertView.findViewById(R.id.overdueField);

        payToField.setText(getItem(position).getPayTo());

        // calculate days until due
        Bill bill = getItem(position);

        // TODO: Add how many days until bill in overdue field + add color
        JodaTimeAndroid.init(getActivity());
        DateTime dueDateDt = new DateTime(bill.getDateDue());
        DateTime currentDt = new DateTime();
        int daysDifference = Days.daysBetween(currentDt.toLocalDate(), dueDateDt.toLocalDate()).getDays();

        // depending on what that differential looks like set text / color
        if (daysDifference > 1) {
            dueDateField.setText(Integer.toString(daysDifference) + " Days");
        } else {
            if (daysDifference == 0) {
                dueDateField.setText("DUE TODAY");
            } else {
                if (daysDifference < 0) {

                }
            }
        }


        return convertView;
    }
}

}

我调试了我的代码,所以我知道在操作数据之后正在进行onLoadFinished回调。我也知道适配器包含此时更新的数据。我已经尝试通过setListAdapter(mAdatper)和我可以找到的每个类似notifyDataChanged的方法重置适配器,但无济于事。这里发生了什么,我怎样才能让listview更新?

0 个答案:

没有答案