2

以下代码完美运行。但是,我尝试加载 10,000 条记录,而 simplecursoradapter 假设在滚动时加载记录,而不是一次加载整个 10,000 条记录。我试图弄清楚如何制作它,因此它一次只能加载大约 10-15 条记录,并继续加载滚动。知道怎么做吗?

package com.example.scoredata;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.ListFragment;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ScoreBoard extends ListFragment {

    public DatabaseHelper db;
    private Cursor cursor;
    public Result r;
    public SimpleCursorAdapter adapter;
    public static String[] FROM = {DatabaseHelper.KEY_POS, DatabaseHelper.KEY_MISTAKES, DatabaseHelper.KEY_DATE, DatabaseHelper.KEY_TIME, DatabaseHelper.KEY_SCORE};
    public static int[] TO = {R.id.score_Pos, R.id.score_Mistakes, R.id.score_Date, R.id.score_Time, R.id.score_Score}; 
    public ListView list;

    public ScoreBoard() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.scoreboard, null);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {       
        super.onCreate(savedInstanceState);
    }


    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {

        list = (ListView) getActivity().findViewById(android.R.id.list); 
        db = new DatabaseHelper(getActivity());
        r = new Result(4, 3, 21, "19-01-2013", "2:42", 59);
        db.addResult(r);
        cursor = db.getRow();
        adapter = new SimpleCursorAdapter(getActivity(), R.layout.scorerow, cursor, FROM, TO, 0);
        list.setAdapter(adapter);

        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        if(db != null) {
            db.close();
        }
        super.onDestroyView();
    }
}



public class DatabaseHelper extends SQLiteOpenHelper {

    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "sudokuScores";

    // Contacts table name
    private static final String TABLE_RESULTS = "results";

    private DatabaseHelper databaseHelper;
    private SQLiteDatabase db;


    // Contacts Table Columns names
    public static final String KEY_ID = "_id";
    public static final String KEY_DIFF = "diff";
    public static final String KEY_POS = "pos";
    public static final String KEY_MISTAKES = "mistakes";
    public static final String KEY_DATE = "date";
    public static final String KEY_TIME = "time";
    public static final String KEY_SCORE = "score";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        databaseHelper = this;
        db = databaseHelper.getWritableDatabase();
    }

    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_RESULTS_TABLE = "CREATE TABLE " + TABLE_RESULTS + "("
                + KEY_ID + " INTEGER PRIMARY KEY, " + KEY_DIFF + " INTEGER, "
                + KEY_POS + " INTEGER, " + KEY_MISTAKES + " INTEGER, "
                + KEY_DATE + " TEXT, " + KEY_TIME + " TEXT, " + KEY_SCORE + " LONG)";
        db.execSQL(CREATE_RESULTS_TABLE);
    }

    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_RESULTS);

        // Create tables again
    }

   public void addResult(Result result) {

       ContentValues values = new ContentValues();
       values.put(KEY_DIFF, result.getDifficulty()); 
       values.put(KEY_POS, result.getPos());
       values.put(KEY_MISTAKES, result.getTotalMistakes());
       values.put(KEY_DATE, result.getDate());
       values.put(KEY_TIME, result.getTimeLeft());
       values.put(KEY_SCORE, result.getScore());    

       // Inserting Row
       db.insert(TABLE_RESULTS, null, values);
   }

   public Cursor getRow() {
       String selectQuery = "SELECT * FROM " + TABLE_RESULTS;
       //return db.rawQuery(selectQuery, null);
       return db.query(TABLE_RESULTS, null, null, null, null, null, null);
   }
}
4

0 回答 0