如何将数据从sqlite缓存到listview?

时间:2012-05-06 19:42:01

标签: java android listview caching

大家好我有一个扩展ListActivity的活动,并显示一个从我的SQLite数据库表中填充的列表视图。

我的listview中的每个条目都有一个与我的GUI设计相同的背景文件。 问题是我的listview中的滚动非常滞后。我认为这是因为每个listview条目都是“动态”创建的,这导致了laggines。

请您查看我的代码并告诉我如何缓存它以获得更好的结果?或者可能有另一种解决方案?

CODE:

package android.GUI;


public class Shifts extends ListActivity implements OnClickListener,
    SimpleGestureListener {

private Typeface tf = Entry.tf, tf2 = Entry.tf2;
public static int count = 1;
int dbHourTime = 0;
private SimpleGestureFilter detector;
public static DBAdapter DB;
public static Cursor cursor;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.shifts);

    detector = new SimpleGestureFilter(this, this);

    DB = new DBAdapter(this);
    DB.open();

    String[] columns = new String[] { DB.KEY_DATE, DB.KEY_HOURS,
            DB.KEY_DAY, DB.KEY_ROWID, DB.KEY_START, DB.KEY_END };
    int[] to = new int[] { R.id.dateDisp, R.id.shiftDisp, R.id.day,
            R.id.rawId, R.id.start, R.id.finish };
    new cursorLoad().execute("this");

    TextView SF = (TextView) findViewById(R.id.total);
    SF.setTypeface(tf);

    TextView sum = (TextView) findViewById(R.id.sum);
    sum.setTypeface(tf);

    SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this,
            R.layout.list_entry, cursor, columns, to);
    this.setListAdapter(mAdapter);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.view_shifts_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.back:

        finish();
        return true;
    case R.id.clear:
        DBAdapter DB = new DBAdapter(this);
        DB.open();
        DB.deleteAll();
        startActivity(getIntent());
        finish();

        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}

@Override
public void onSwipe(int direction) {

    Intent intent = new Intent();

    switch (direction) {

    case SimpleGestureFilter.SWIPE_RIGHT:

        intent.setClass(this, Main.class);
        startActivity(intent);
        break;
    case SimpleGestureFilter.SWIPE_LEFT:

        intent.setClass(this, Entry.class);
        startActivity(intent);
        break;
    case SimpleGestureFilter.SWIPE_DOWN:

        break;
    case SimpleGestureFilter.SWIPE_UP:

        break;
    }

}

@Override
public boolean dispatchTouchEvent(MotionEvent me) {
    this.detector.onTouchEvent(me);
    return super.dispatchTouchEvent(me);
}

@Override
public void onDoubleTap() {
    // TODO Auto-generated method stub

}

public class cursorLoad extends AsyncTask<String, Integer, String> {

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub

        cursor = DB.getAllShifts();
        startManagingCursor(cursor);
        cursor.moveToLast();
        count = cursor.getPosition();
        int g = count;

        cursor.moveToNext();
        return null;
    }

}

 }

1 个答案:

答案 0 :(得分:0)

最后我需要的是ViewHolder类以及getView \ bindView。