ArrayList自定义类
ArrayList<ItemClass> nomenclatureList
有一个onItemClickListener
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
ItemClass word = nomenclatureList.get(position);
int singleImg = word.getImageResourse();
String singleName = word.getNomenclatureName();
String singleWeight = word.getNomenclatureWeight();
String singleBrand = word.getNomeclatureBrand();
String singleBar = word.getNomenclatureBarCode();
String singlePercent = word.getNomenclaturePercent();
String singleComposition = word.getNomenclatureComposition();
String singlePack = word.getNomenclaturePack();
Intent intent = new Intent(ItemListGKI.this, ItemSingle.class);
intent.putExtra("SINGLE_IMG_KEY", singleImg);
intent.putExtra("SINGLE_NAME_KEY", singleName);
intent.putExtra("SINGLE_WEIGHT_KEY", singleWeight);
intent.putExtra("SINGLE_BRAND_KEY", singleBrand);
intent.putExtra("SINGLE_BAR_KEY", singleBar);
intent.putExtra("SINGLE_PERCENT_KEY", singlePercent);
intent.putExtra("SINGLE_COMPOSITION_KEY", singleComposition);
intent.putExtra("SINGLE_PACK_KEY", singlePack);
startActivity(intent);
}
});
并且有用于从此ArrayList搜索的代码
@Override
public boolean onQueryTextChange(String newText) {
boolean success = false;
ArrayList<String> tempList = new ArrayList<>();
for(ItemClass temp : nomenclatureList){
if(temp.getNomenclatureName().toLowerCase().contains(newText.toLowerCase())){
tempList.add(temp.getNomenclatureName());
ArrayAdapter<String> adapter = new ArrayAdapter<>(ItemListGKI.this, android.R.layout.simple_list_item_1, tempList);
listView.setAdapter(adapter);
tempList.size();
success = true;
}
}
return success;
}
当我单击搜索结果项时,打开的活动信息错误。如果我不使用搜索就单击项目-一切都OK。
我不知道该怎么做。
答案 0 :(得分:0)
根据此处定义的API-> https://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener
onItemClick(AdapterView<?> adapterView, View view, int position, long id)
您的代码正在考虑position
来检索被单击的项目。我相信应该是身份证。尝试更换
ItemClass word = nomenclatureList.get(position);
使用
ItemClass word = nomenclatureList.get(id);
答案 1 :(得分:0)
替换此:
ItemClass word = nomenclatureList.get(position);
与此
ItemClass word = (ItemClass) adapterView.getItemAtPosition(position);
为什么?因为当您搜索或过滤数据时nomenclatureList的数据与设置的适配器不同,因此例如nomenclatureList.get(5)
与(ItemClass) adapterView.getItemAtPosition(position);
现在应在onQueryTextChange中输入:
@Override
public boolean onQueryTextChange(String newText) {
boolean success = false;
ArrayList<ItemClass> tempList = new ArrayList<>();
for(ItemClass temp : nomenclatureList){
if(temp.getNomenclatureName().toLowerCase().contains(newText.toLowerCase())){
tempList.add(temp);
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<>(ItemListGKI.this, android.R.layout.simple_list_item_1, tempList);
listView.setAdapter(adapter);
tempList.size();
if(tempList.size() > 0){
success = true;
}
return success;
}