2

我的应用程序上有一个功能正常的搜索界面。现在我正在尝试使每个搜索结果列表项都可单击并显示在新活动中。我让它们可以点击,但我不知道如何将ListAdapter值(记录)传递到onItemClick方法中。

到目前为止,这是我的代码:

private void showResults(String query) {
    Cursor cursor = DBHelper.searchDB(query);
    startManagingCursor(cursor);
    String[] searchFrom = new String[] { DBAdapter.KEY_MAKE,
            DBAdapter.KEY_YEAR, DBAdapter.KEY_MAKE,
            DBAdapter.KEY_MODEL };
    int[] displayHere = new int[] { R.id.rDateTV, R.id.rYearTV,
            R.id.rMakeTV, R.id.rModelTV };

    final SimpleCursorAdapter records = new SimpleCursorAdapter(this,
            R.layout.record_2, cursor, searchFrom, displayHere);

    setListAdapter(records);
    DBHelper.close();

    // --- Click on list item ---


    ListView clickList = getListView(); 
    clickList.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
        @Override 
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)        {
            //--- here's the problem: how do I pass the ListAdapter values into onItemClick. the below doesn't like .getText()
            String sYear = (view.findViewById(R.id.rYearTV)).getText().toString();
            String sMake = (view.findViewById(R.id.rMakeTV)).getText().toString();
            String sModel = (view.findViewById(R.id.rModelTV)).getText().toString();

            // -- then pass these strings to an intent to launch a new activity




        } 
    });


    // --- END click on list item ----

}

有任何想法吗?我是这里的菜鸟,所以如果你能给我看代码,那就太棒了!

4

1 回答 1

2

OnItemClickListener.onItemClick通话parent.getItemAtPosition(position)中。该方法返回设置到给定位置的光标。然后从该游标中获取您的数据。不要在OnItemClickListener.onItemClick.

例如:

ListView clickList = getListView(); 
    clickList.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
        @Override 
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)        {
            Cursor c = (Cursor)parent.getItemAtPosition(position)
            String sMake = c.getString(c.getColumnIndexOrThrow(DBAdapter.KEY_MAKE));//get data from cursor

           ...
        } 
    });
于 2012-09-25T20:30:51.690 回答