如何将ListAdapter值

如何将ListAdapter值

我的应用程序上有正常运行的搜索界面。现在,我试图使每个搜索结果列表项都可单击并显示在新活动中。我已经使它们可单击,但是我不知道如何将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 ----

}


有任何想法吗?我是这里的菜鸟,所以如果您可以向我展示代码,那将是AWSOME!

最佳答案

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

           ...
        }
    });

10-06 06:56