1

伙计们,我是一个自学成才的 android 开发人员,很新,但我正在努力。我一直在寻找有关适配器和 ViewHolder 的帮助。我发现很难理解如何使用 ViewHolder 模式来缓存我的小部件然后显示它们。

基本上我有一个从我的 SQLite DB 读取数据的游标。我使用 SimpleCursorAdapter 将结果显示到列表视图,它工作正常。唯一的问题是滚动非常慢。

现在我正在尝试实现 ViewHolder 模式,但无法弄清楚如何将数据从我的数据库绑定到 ViewHolder,然后显示它。

拜托,请帮帮我!我找了2个月的答案!

代码(我在外面留下了不相关的代码以使其更简单):

    public class Shifts extends ListActivity implements OnClickListener,
    SimpleGestureListener {


private Typeface tf = Entry.tf, tf2 = Entry.tf2, roboto = Entry.roboto;
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();
    cursor = DB.getAllShifts();
    startManagingCursor(cursor);
    cursor.moveToLast();
    count = cursor.getPosition();
    int g = count;

    cursor.moveToNext();

    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 };

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

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

    TextView empty = (TextView) findViewById(android.R.id.empty);
    empty.setTypeface(roboto);

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

}

适配器的代码:

private static class EfficientAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    private Bitmap layoutBG;
    private Bitmap mIcon2;

    public EfficientAdapter(Context context) {
        mInflater = LayoutInflater.from(context);

        layoutBG = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.li2);

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return cursor.getCount();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return cursor.getPosition();
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return cursor.getPosition();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        ViewHolder holder;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_entry, null);

            holder = new ViewHolder();
            holder.LL = (LinearLayout) convertView
                    .findViewById(R.id.layoutBG);
            holder.rowID = (TextView) convertView.findViewById(R.id.rawId);
            holder.info = (TextView) convertView.findViewById(R.id.Info);
            holder.dateDisp = (TextView) convertView
                    .findViewById(R.id.dateDisp);
            holder.day = (TextView) convertView.findViewById(R.id.day);
            holder.finish = (TextView) convertView
                    .findViewById(R.id.finish);
            holder.hourMin = (TextView) convertView
                    .findViewById(R.id.hourMin);
            holder.shiftDisp = (TextView) convertView
                    .findViewById(R.id.shiftDisp);
            holder.start = (TextView) convertView.findViewById(R.id.start);
            holder.timestarted = (TextView) convertView
                    .findViewById(R.id.timestarted);

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

        return null;
    }

}

持有人类别代码:

    static class ViewHolder {

    TextView rowID;
    TextView info;
    TextView dateDisp;
    TextView day;
    TextView timestarted;
    TextView start;
    TextView finish;
    TextView hourMin;
    TextView shiftDisp;
    LinearLayout LL;

}
4

0 回答 0