如何从ListView SimpleCursorAdaptor访问项目

时间:2012-06-13 04:32:48

标签: android android-listview

我正在使用SimpleCursorAdaptor加载包含2列的listview。代码是:

        mCursor = managedQuery(MyobiliseData.CONTENT_URI, projection, null, null, null);

    //3. Attach the data to the listview using an adaptor
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
            R.layout.list_item_custom_font, 
            mCursor, 
            new String[]{MyobiliseData.Columns.CUSTOMER_NAME,MyobiliseData.Columns.ADDRESS},
            new int[] {R.id.text1,R.id.text2}
            );
     listView1.setAdapter(adapter);

当选择列表中的项目时我试图访问数据,但我不确定使用什么类型的对象 - 当我使用HashMap对象时,它失败了,即

HashMap<String, String> item =  (HashMap<String,String>) listView1.getAdapter().getItem(position);

最值得赞赏的任何帮助。

谢谢

1 个答案:

答案 0 :(得分:2)

您可以从光标获取特定索引的项目,使用如下:

listView1.setOnItemClickListener(new OnItemClickListener()
{
    public void onItemClick(AdapterView adapterView, View view, int position, long id)
    {
         mCursor.moveToPosition(position);
         String customerName=mCursor.getString(mCursor.getColumnIndex(MyobiliseData.Columns.CUSTOMER_NAME));
         String address=mCursor.getString(mCursor.getColumnIndex(MyobiliseData.Columns.ADDRESS));
         //Use CustomerName and address
    }
});