0
我一直在嘗試幾個小時才能找到這個在線的答案,但我越看越困惑。有人可以幫我弄這個嗎?listview在listview中返回原始位置onclick
這是我在那裏搜索一個項目,並可以點擊搜索到的項目的類。但是它總是返回未過濾列表視圖中的第一個位置,在下一個意圖中給出錯誤的變量。
db = new DBAdapter(this);
db.open();
final ArrayList<String> list = new ArrayList<String>();
final ArrayList<Integer> listId = new ArrayList<>();
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(ListChosenCategory.this);
final int categoryId = settings.getInt("CategoryId",0);
Log.d("hehehe","categoryid"+categoryId);
//Fills out the listview with items
Cursor cur = db.listAllItems(categoryId);
if(cur.moveToFirst())
{
do
{
list.add(cur.getString(0));
listId.add(cur.getInt(cur.getColumnIndex(db.ITEMSID)));
}
while(cur.moveToNext());
}
cur.close();
final ListView listItems = (ListView) findViewById(R.id.listAllCategoryItems);
final StableArrayAdapter adapter = new StableArrayAdapter(ListChosenCategory.this,android.R.layout.simple_list_item_1,list,listId);
listItems.setAdapter(adapter);
adapter.notifyDataSetChanged();
listItems.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(final AdapterView<?> parent, final View view, final int position, long id)
{
int itemId = adapter.getActualId(position);
Intent intent = new Intent(ListChosenCategory.this, ListItemDetails.class);
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putInt("ItemId",itemId).apply();
startActivityForResult(intent,1);
}
});
SearchView searchView = (SearchView)findViewById(R.id.SearchForItemText);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String text) {
return false;
}
@Override
public boolean onQueryTextChange(String text) {
adapter.notifyDataSetChanged();
adapter.getFilter().filter(text);
adapter.notifyDataSetChanged();
return false;
}
});
這是我arrayadapter:
私有類StableArrayAdapter擴展ArrayAdapter
HashMap<String, Integer> mIdMap = new HashMap<>();
List<Integer> trialId;
public StableArrayAdapter(Context context, int textViewResourceId, List<String> objects, List<Integer> objectId)
{
super(context, textViewResourceId, objects);
for (int i = 0; i < objects.size(); i++)
mIdMap.put(objects.get(i), i);
trialId = objectId;
}
public long getItemId(int position)
{
String item = getItem(position);
return mIdMap.get(item);
}
public int getActualId(int position)
{
return trialId.get(position);
}
任何幫助是非常讚賞
//編輯
當我點擊33在過濾的列表中顯示了這個,這是u中的第一行未過濾的列表。
當我在未過濾列表中單擊33它顯示了正確的變量等