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它显示了正确的变量等