首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用BaseAdapter从sqlite中提取BaseAdapter?

如何用BaseAdapter从sqlite中提取BaseAdapter?
EN

Stack Overflow用户
提问于 2014-03-06 07:08:25
回答 2查看 912关注 0票数 3

我有sqlite列的定义如下:

代码语言:javascript
复制
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"; 

我想从一个sqlite类获得getItemID,其中一个适配器类扩展了BaseAdapter。这是我的代码:

代码语言:javascript
复制
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列值?

提前感谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-03-06 07:56:19

Cursor.move()按相对数量移动光标。您想要一个绝对位置,请使用moveToPosition()代替。

票数 1
EN

Stack Overflow用户

发布于 2014-03-06 07:25:01

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

将您的参数名称放入query.But --我插入了它。

代码语言:javascript
复制
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到数组列表中,并在任何地方使用它。

谢谢

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22217520

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档