在android中获取光标处的所有项目

发布于 2024-08-02 05:21:03 字数 1582 浏览 5 评论 0原文

我使用此代码从光标获取项目,但它只返回列表中的一项。那么,如何将所有项目添加到我的列表中,这是我的代码?

class MyAdapter extends SimpleCursorAdapter
{
    private Context context;

    public MyAdapter(Context context, int layout, Cursor c, String[] from, int[] to)
    {
        super(context, layout, c, from, to);
        this.context = context;

    }
    public View getView(int position, View convertView, ViewGroup parent){
        Cursor cursor = getCursor();

        LayoutInflater inflater = ((Activity) context).getLayoutInflater();         
        View v = inflater.inflate(R.layout.sbooks_row, null);           
        TextView title = (TextView)findViewById(R.id.title);
        if(title != null){
            int index = cursor.getColumnIndex(SBooksDbAdapter.KEY_TITLE);
            String type = cursor.getString(index);
            title.setText(type);
        }

        TextView lyrics = (TextView)findViewById(R.id.lyrics);
        if(lyrics != null){
            int index = cursor.getColumnIndex(SBooksDbAdapter.KEY_LYRICS);
            String type = cursor.getString(index);
            lyrics.setText(type);
        }

        ImageView im = (ImageView)findViewById(R.id.icon);
        if(im!=null){
            int index = cursor.getColumnIndex(SBooksDbAdapter.KEY_FAVORITE);
            int type = cursor.getInt(index);
            if(type==1){
                im.setImageResource(android.R.drawable.btn_star_big_on);
            }
            else{
                im.setImageResource(android.R.drawable.btn_star_big_off);
            }
        }

        return v;
    }

I use this code to get the item from cursor, but it just return one item on my list. So, how can I get all items to my list, this is my code?

class MyAdapter extends SimpleCursorAdapter
{
    private Context context;

    public MyAdapter(Context context, int layout, Cursor c, String[] from, int[] to)
    {
        super(context, layout, c, from, to);
        this.context = context;

    }
    public View getView(int position, View convertView, ViewGroup parent){
        Cursor cursor = getCursor();

        LayoutInflater inflater = ((Activity) context).getLayoutInflater();         
        View v = inflater.inflate(R.layout.sbooks_row, null);           
        TextView title = (TextView)findViewById(R.id.title);
        if(title != null){
            int index = cursor.getColumnIndex(SBooksDbAdapter.KEY_TITLE);
            String type = cursor.getString(index);
            title.setText(type);
        }

        TextView lyrics = (TextView)findViewById(R.id.lyrics);
        if(lyrics != null){
            int index = cursor.getColumnIndex(SBooksDbAdapter.KEY_LYRICS);
            String type = cursor.getString(index);
            lyrics.setText(type);
        }

        ImageView im = (ImageView)findViewById(R.id.icon);
        if(im!=null){
            int index = cursor.getColumnIndex(SBooksDbAdapter.KEY_FAVORITE);
            int type = cursor.getInt(index);
            if(type==1){
                im.setImageResource(android.R.drawable.btn_star_big_on);
            }
            else{
                im.setImageResource(android.R.drawable.btn_star_big_off);
            }
        }

        return v;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你的他你的她 2024-08-09 05:21:03

CursorAdapter 的行为与其他列表适配器有点不同 - 这里的魔力不是在 getView() 中,而是在 newView() 和 bindView() 中,所以我认为 getView() 不是重写的正确方法。

您可能只会得到一个结果,因为创建第一行后,CursorAdapter 期望 bindView() 插入新数据并重用已经膨胀的行,并且您期望 getView() 执行此操作。

我建议您尝试将代码移至 newView() 来扩充视图,并移至 bindView() 来执行填充行的实际逻辑。

祝你好运,请随时向我们通报最新结果。

CursorAdapter behaves a little bit different than the other list adapters - instead of in getView(), the magic here happens in newView() and bindView(), so I think getView() isn't the right method to override.

You may get only one result, because after the first row is created, CursorAdapter expects bindView() to insert new data and reuse the already inflated row, and you expect getView() to do that.

I'll suggest you to try moving your code to newView() to inflate your view, and bindView() to execute the actual logic for populating the rows.

Good luck, and keep us updated on the results.

潦草背影 2024-08-09 05:21:03

我假设 getCursor() 方法返回的光标正确检索表的所有行,您必须在访问行的数据之前显式地将光标移动到某个位置,因此在 getView() 方法的开头你必须打电话。

cursor.moveToPosition(position);

I am presuming the cursor returned by the getCursor() method is correctly retrieving all the rows for your table, you have to explicitly move the cursor to a position before accessing the data for a row, so at the beginning of your getView() method you have to call.

cursor.moveToPosition(position);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文