イベントonItemClick(AdapterView親、ビューv、int位置、long id)は、私が望むように動作しません。
これは、アダプターをフィルターに掛けるときの通常の状況です。アダプタは、フィルタリングされていない初期のデータへの参照をその観点から保持していますが、アダプタの初期のデータであるかフィルタ操作の結果であるかにかかわらず、単一のデータセットをベースにしています。しかし、これは問題を起こすべきではありません。デフォルトのsdkアダプタ(またはサブクラス)では、onItemClick()
には、の現在のリストのposition
が表示されます。その後、getItem()
を使用して、そのデータ項目を取得することができます。position
(初期値かフィルタリングかは関係ありません)
String data = getItem(position);
int realPosition = list.indexOf(data); // if you want to know the unfiltered position
これは(あなたがSimpleAdapter
を使用すると仮定して)リストやMaps
のために動作します。 Maps
の場合、最初のリストのフィルタリングされていない位置を設定するために追加のキーを追加することができます。
AutoCompleteTextView
と一緒に独自のアダプターを使用する場合、onItemClick()
はあなたに正しいid
(ただし位置は変更できません)を与えることができます。
public class SpecialAutoComplete extends AutoCompleteTextView {
public SpecialAutoComplete(Context context) {
super(context);
}
@Override
public void onFilterComplete(int count) {
// this will be called when the adapter finished the filter
// operation and it notifies the AutoCompleteTextView
long[] realIds = new long[count]; // this will hold the real ids from our maps
for (int i = 0; i < count; i++) {
final HashMap<String, String> item = (HashMap<String, String>) getAdapter()
.getItem(i);
realIds[i] = Long.valueOf(item.get("id")); // get the ids from the filtered items
}
// update the adapter with the real ids so it has the proper data
((SimpleAdapterExtension) getAdapter()).setRealIds(realIds);
super.onFilterComplete(count);
}
}
とアダプタ:
public class SimpleAdapterExtension extends SimpleAdapter {
private List<? extends Map<String, String>> mData;
private long[] mCurrentIds;
public SimpleAdapterExtension(Context context,
List<? extends Map<String, String>> data, int resource,
String[] from, int[] to) {
super(context, data, resource, from, to);
mData = data;
}
@Override
public long getItemId(int position) {
// this will be used to get the id provided to the onItemClick callback
return mCurrentIds[position];
}
@Override
public boolean hasStableIds() {
return true;
}
public void setRealIds(long[] realIds) {
mCurrentIds = realIds;
}
}
あなたはまた、アダプタのFilter
クラスを実装する場合、あなたはAutoCompleTextView
クラスをオーバーライドすることなく、そこからIDを得ることができます。
PLSのは、友人 – sarath
海の友人に私を助けて、私はこの問題を解決しようとしましたが...私はアンドロイドの虎から何の解決策をwonder.Whyないことができませんでした? –