0

タイトルが長文ですみません、150字超えたので切り捨てます。

私はAutoCompleteTextView(ACTV)を持ってSimpleCursorAdapterおり、通常の ACTV は各部分文字列 (部分文字列は空白で区切られている) の先頭でのみユーザー入力を検索し、それらの部分文字列内では検索しないため、 を使用しています。たとえば、Adiposeandを含むリストをBad Wolf検索すると、のみadが表示され、 は表示されません。以下に示すように、すでにアダプターを作成しました。AdiposeBad 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 行をクリックすると、コードが表示されますString selection = matrix.getString(position);

matrix.getString(position)getString は要求された列の値を文字列として返し、列が 2 つしかないため、ACTV でその位置 (ユーザーに表示される位置ではなく、ユーザーに表示される位置) で提案を選択するため、実行するとエラーが発生すると思いますリスト内の上記の項目) が 2 より大きいと、コードが台無しになります。

私の質問は、私が使用していることを考えると、選択したアイテムの文字列値を取得するより良い方法はありSimpleCursorAdapterますか? Android dev サイトで 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は、インデックス 0 が付けられた、選択された ACTV 提案の位置です。

4

2 に答える 2

0

別のアプローチを使用して機能させました。View を取得し、TextView としてキャストします。そこから、文字列入力を取得します。次に、この文字列を使用して、元のリスト内での位置を探します。私のリストはArrayListではなくArrayであることに注意してください。そのため、すべてのアイテムをループする必要がありました。

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 に答える
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 に答える