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