0

对不起,标题太多了,我不得不删掉它,因为我超过了 150 个字符的限制。

我有一个AutoCompleteTextView(ACTV) 并且我正在使用一个SimpleCursorAdapter,因为普通的 ACTV 只在每个子字符串的开头搜索用户输入(子字符串由空格分隔),而不是在这些子字符串中。例如,具有AdiposeandBad Wolf和搜索的列表adAdipose仅显示 and not Bad Wolf。我已经制作了如下所示的适配器:

//create ACTV Here
AutoCompleteTextView search = (AutoCompleteTextView) findViewById(R.id.actvCatalogueSearch);
search.setThreshold(1);

String[] from = { "name" };
int[] to = { android.R.id.text1 };

SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, 
        android.R.layout.simple_dropdown_item_1line, null, from, to, 0);

cursorAdapter.setStringConversionColumn(1);

FilterQueryProvider provider = new FilterQueryProvider(){
    @Override
    public Cursor runQuery(CharSequence constraint) {
        // TODO Auto-generated method stub
        String constrain = (String) constraint;
        constrain = constrain.toUpperCase();
        Log.d("hi", "runQuery constraint: " + constraint);
        if (constraint == null) {
            return null;
        }
        String[] columnNames = { Columns._ID, "name" };
        MatrixCursor c = new MatrixCursor(columnNames);
        try {
            for (int i = 0; i < pdflist.length; i++) {
                if(pdflist[i].contains(constrain)){
                    Log.d("Hello","Match! pdflist item = " + pdflist[i]);
                    c.newRow().add(i).add(pdflist[i]);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return c;
    }
};

cursorAdapter.setFilterQueryProvider(provider);
search.setAdapter(cursorAdapter);

此代码使我能够显示包含来自用户输入的子字符串的其他列表项。

现在,我正在尝试使OnItemClickListener功能正确。这是我到目前为止所拥有的:

search.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        MatrixCursor matrix = (MatrixCursor)parent.getItemAtPosition(position);

        Log.d("hello", "matrix values is = " + matrix);

        String selection = matrix.getString(position);  
        Log.d("hallo","selection = " + selection);
        Log.d("hello","item id at position = " + parent.getItemIdAtPosition(position));

        int pos = (int) parent.getItemIdAtPosition(position);
        Log.d("sup", "position is = " + pos);
        String path = imagelist[pos].getAbsolutePath();
        openPdfIntent(path);
    }
});

在这里,我试图MatrixCursor在给定位置获取元素。用户选择前 2 个建议时效果很好。但是,当用户单击第 3 个建议时,应用程序会CursorIndexOutOfBoundsException Requested Column: 2, # of columns: 2在 logCat 行上引发 Clicking,将我指向代码String selection = matrix.getString(position);

我认为这样做matrix.getString(position)会导致错误,因为 getString 将请求列的值作为字符串返回,并且由于只有 2 列,因此在 ACTV 中选择一个建议,其位置(向用户显示的位置,而不是列表中的上述项目)大于 2 会导致代码出错。

我的问题是,鉴于我正在使用,是否有更好的方法来获取所选项目的字符串值SimpleCursorAdapter?我查看了 android 开发站点中 Matrix Cursor 的文档,但找不到基于位置获取行/元素的方法。

很感谢任何形式的帮助。

编辑:

这样使用matrix.moveToFirst();也无济于事:

search.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        MatrixCursor matrix = (MatrixCursor)parent.getItemAtPosition(position);
        if(matrix != null) {
            if(matrix.getCount() > 0) {
                matrix.moveToFirst();
                String selection = matrix.getString(position);  

                int pos = (int) parent.getItemIdAtPosition(position);
                String path = imagelist[pos].getAbsolutePath();
                openPdfIntent(path);
            }    
        }
    }
});

我仍然有例外:

android.database.CursorIndexOutOfBoundsException: Requested column: 4, # of columns: 2

请求的列4是所选 ACTV 建议的位置,索引为零。

4

2 回答 2

0

试试这样

MatrixCursor matrix = .............

Log.d("hello", "matrix values is = " + matrix);

/***** Check here Cursor is NOT NULL *****/
if(matrix != null) {
    if(matrix.getCount() > 0) {
        matrix.moveToFirst();
        /***
        Your Stuff will be here....
        **/
    }    
}
于 2014-11-27T06:35:14.530 回答
0

使用不同的方法使其工作。我得到视图并将其转换为 TextView。从那里,我得到字符串输入。然后我使用这个字符串并在原始列表中查找它的位置。请注意,我的列表是一个 Array,而不是 ArrayList,这就是我必须遍历所有项目的原因。

search.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        TextView tv = (TextView) view;
        String userSelection = tv.getText().toString();
        Log.d("hello", "selection is = " + userSelection);

        int pos = -1;

        for (int i = 0; i < pdflist.length; i++) {
            if(pdflist[i].equalsIgnoreCase(userSelection)){
                pos = i;
            }
        }

        Log.d("hello","int position = " + pos);

        String path = imagelist[pos].getAbsolutePath();
        openPdfIntent(path);
    }
});
于 2014-11-27T10:03:17.037 回答