如何使用BaseAdapter从sqlite获取getItemID?

时间:2014-03-06 07:08:25

标签: android sqlite listview baseadapter

我的sqlite列定义如下:

public final static String S_ID = "_ID"; 
public final static String S_PHOTOPATH = "Photopath";
public final static String S_NAME = "Name"; 
public final static String S_POS = "Pos";
public final static String S_SCORE = "Score"; 
public final static String S_FIXED = "Fixed"; 

我想从一个带有适配器类扩展BaseAdapter的sqlite帮助器类获取getItemID。这是我的代码:

public class Page1ListAdapter extends BaseAdapter {

    private LayoutInflater adapterInflater;
    public Cursor mCursor;
    TagView tag;

    public Page1ListAdapter(Context context, Cursor cursor) {
        super();
        mCursor = cursor;

        adapterInflater=LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mCursor.getCount();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            tag = new TagView();
            convertView = adapterInflater.inflate(R.layout.page1listview_item, null);
            tag.imgV = (ImageView) convertView.findViewById(R.id.ItemImage); 
            tag.titleV = (TextView) convertView.findViewById(R.id.ItemTitle); 
            tag.descriptionV = (TextView) convertView.findViewById(R.id.ItemText);
            convertView.setTag(tag);
        } else {
            tag = (TagView) convertView.getTag();
        }
        mCursor.moveToPosition(position);
        Bitmap bmp = BitmapFactory.decodeFile(mCursor.getString(mCursor.getColumnIndex(DBHelper.S_PHOTOPATH)));
        BitmapDrawable bmpDrawable = new BitmapDrawable(getActivity().getResources(), bmp);
        tag.imgV.setBackground(bmpDrawable);
        tag.titleV.setText(mCursor.getString(mCursor.getColumnIndex(DBHelper.S_NAME)));         
        tag.descriptionV.setText(mCursor.getString(mCursor.getColumnIndex(DBHelper.S_POS)));

        return convertView;
    }

    @Override
    public Object getItem(int position) {
        return position;            
    }

    @Override
    public long getItemId(int position) {
        Log.i("mCursor.getCount() --->", String.valueOf(mCursor.getCount()));
        mCursor.move(position);     
        long id = mCursor.getLong(0); // <--- error here
        return id; 
    }

    public void setTag (Drawable dwb, String title, String description) {
        tag.imgV.setBackground(dwb);
        tag.titleV.setText(title);
        tag.descriptionV.setText(description);
    }
}

但是,getItemID()总是会出错或ID错误。

如何用光标获取_ID列值?

提前致谢!

2 个答案:

答案 0 :(得分:1)

Cursor.move()将光标移动相对数量。您想要绝对位置,请改用moveToPosition()

答案 1 :(得分:0)

首先初始化数据库obj,如下所示         SQLiteDatabase db = myDb.getReadableDatabase();

将参数名称放入查询中。但我插入了它。

try{
      Cursor cursor=db.query(DB_helper.YourTableName, new String[]          {"_ID","Photopath","Name","Pos","Score","Fixed"}, null,null, null, null, null);     
        if(cursor.getCount()>0){
         cursor.moveToFirst();
         do{
        //cursor.getString(0); Means get String from first column(_ID)          
        USER_ID =cursor.getString(0);
        USERNAME=cursor.getString(3);
        USERTYPE=cursor.getString(5);
        PLANTID =cursor.getString(4);
         }while (cursor.moveToNext());
        cursor.close();                  
           db.close();                 
        }     

    }catch(Exception e){
     e.printStackTrace();            
    }

您可以将值存储到String on the array list中,并在任何地方使用它。

由于