您好我的listView有问题我的OnItemClick方法有错误
FATAL EXCEPTION:main java.lang.ClassCastException:com.example.dell.app3.Product无法强制转换为java.util.HashMap
private void showProduct(String json){
JSONObject jsonObject = null;
ArrayList<Product> productList = new ArrayList<>();
try {
JSONArray result = new JSONArray(json);
for(int i = 0; i<result.length(); i++){
JSONObject jo = result.getJSONObject(i);
String name = jo.getString("Nom");
String ref = jo.getString("Reference");
String image1 = "http://aaaa.com/Scripts/images/"+jo.getString("image")+".jpg";
Product product = new Product();
product.setName(name);
product.setRef(ref);
product.setImageUrl(image1);
productList.add(product);
}
} catch (JSONException e) {
e.printStackTrace();
}
adapter = new CustomArrayAdapter(getContext(), productList);
listView.setAdapter(adapter);
}
OnItemClick方法
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getActivity().getApplicationContext(), ViewProduct.class);
HashMap<String, String> map = (HashMap) parent.getItemAtPosition(position);
String tid = map.get("ref").toString();
intent.putExtra("ref", tid);
startActivity(intent);
}
我的inputSearch
也有错误inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
((CustomArrayAdapter) ProduitsFragment.this.adapter).getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
}
});
product.java
public class Product {
String name;
String ref;
String imageUrl;
public String getName() {
return name;
}
public String getRef() {
return ref;
}
public String getImageUrl() {
return imageUrl;
}
public void setName(String name) {
this.name = name;
}
public void setRef(String ref) {
this.ref = ref;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
}
答案 0 :(得分:1)
而不是
HashMap<String, String> map = (HashMap) parent.getItemAtPosition(position);
String tid = map.get("ref").toString();
将其更改为
Product prod = (Product)parent.getItemAtPosition(position);
String tid = prod.getRef();
答案 1 :(得分:0)
试试这个
宣布全球
ArrayList<Product> productList = new ArrayList<>();
将onItemClick
替换为
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getActivity().getApplicationContext(), ViewProduct.class);
// get item from list @position
Product product = (Product)productList.get(position);
String tid = product.getRef();
intent.putExtra("ref", tid);
startActivity(intent);
}
适用于您的Filter
功能:
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// global object of custom adapter
customArrayAdapter.getFilter().filter(cs.toString());
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
}
});
要过滤功能,请尝试
customArrayAdapter.getFilter().filter(cs);
而不是
((CustomArrayAdapter) ProduitsFragment.this.adapter).getFilter().filter(cs);
答案 2 :(得分:0)
你正在从Hashmap中获取值。在你的代码中是excption.Get来自Product Mode.Which是一个对象。试试代码
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getActivity().getApplicationContext(), ViewProduct.class);
Product product = (Product)parent.getItemAtPosition(position);
String tid = product.getRef();
intent.putExtra("ref", tid);
startActivity(intent);
}
答案 3 :(得分:-1)
而不是onItemClick()中的父级使用视图
HashMap<String, String> map = (HashMap) view.getItemAtPosition(position);