无法获得从游标适配器数据 [英] can't getting data from cursor adapter

查看:132
本文介绍了无法获得从游标适配器数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的ListView (在活动,而不是在一个 ListActivity ),它利用自定义光标适配器从我的本地数据库获取一些数据并显示它们中的的ListView

lv.setOnItemClickListener(新OnItemClickListener()
{
   @覆盖
   公共无效onItemClick(适配器视图<>为arg0,观景,INT位置,长的ID)
      {
            Log.i(标签,位置=+位置);
        Log.i(标签,ID为:+ ID));
      }
});

假设我的数据库列如下:


  

ID,姓名,出生日期,身高,性别,placeOfBirth,maritalStatus。


不过,我显示我的列表视图(row.xml)只有名称和姓。
但每当用户点击某行中的列表,我要检索的数据的其余部分太,例如,在ID或该行的两性点击

问题是,我不显示所有从DB行信息在我的名单,但是,当列表中的用户presses我需要找回一些该列表中的数据。我怎么能做到这一点吗?

下面的方法不起作用,因为我不是一个ListActivity,只是活动。

公共无效onListItemClick(ListView中升,视图V,INT位置,长的ID)
{
super.onListItemClick(L,V,位置ID);光标C =((SimpleCursorAdapter)l.getAdapter())getCursor();
c.moveToPosition(位置);
//获取数据...
}


解决方案

OnItemClickListener <的将arg0 参数STRONG>是的的ListView 。但是,你可以做

  lv.setOnItemClickListener(新OnItemClickListener(){
   @覆盖
   公共无效onItemClick(适配器视图&LT;&GT;为arg0,观景,INT位置,长的id){
            //这将让从适配器光标已经移动到位置
            光标光标=(光标)mCursorAdapter.getItem(位置)
            //获取数据...
      }
});

I have a ListView (in an Activity and not in a ListActivity) that utilizes a custom cursor adapter to get some data from my local db and show them inside the ListView.

lv.setOnItemClickListener(new OnItemClickListener()
{
   @Override
   public void onItemClick(AdapterView<?> arg0, View view, int position, long id)
      {
            Log.i(tag, "position = " + position);
        Log.i(tag, "id is : " + id));
      }
});

Assume my database columns are the following :

id, name, surname, dob, height, gender, placeOfBirth, maritalStatus.

However, I am showing in my listview (row.xml) only the name and surname. But whenever the user clicks on a certain row in the list, I want to retrieve the rest of the data too, for example, the id, or the gender of the row clicked.

The issue is that, I am not showing all the info from the db row in my list, however, when the user presses on the list I need to retrieve some of the data for that list. How can I do that here ?

The below method does not work, because I am not in a ListActivity, and just Activity.

public void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position,  id);

Cursor c = ((SimpleCursorAdapter)l.getAdapter()).getCursor();
c.moveToPosition(position);
//get the data...
}

解决方案

The arg0 parameter in your OnItemClickListener is the ListView. However, you can just do

lv.setOnItemClickListener(new OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
            // This will get the cursor from the adapter, already moved to position
            Cursor cursor = (Cursor) mCursorAdapter.getItem(position)
            //get the data...
      }
});

这篇关于无法获得从游标适配器数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆