-1

Hello I have a problem with my listView I have an error with my OnItemClick methd

FATAL EXCEPTION: main java.lang.ClassCastException: com.example.dell.app3.Product cannot be cast to 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 method

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);
    }

I have an error also with my 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;
    }
}
clara
  • 11
  • 1
  • 6

4 Answers4

1

Instead of

HashMap<String, String> map = (HashMap) parent.getItemAtPosition(position);
    String tid = map.get("ref").toString();

change it to

Product prod =  (Product)parent.getItemAtPosition(position);
    String tid = prod.getRef();
SripadRaj
  • 1,687
  • 2
  • 22
  • 33
0

Try this

declare global

  ArrayList<Product> productList = new ArrayList<>();

Replace your onItemClick with,

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);
    }

For your Filter functionality:

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) {
            }
        });

To filter functionality try,

customArrayAdapter.getFilter().filter(cs);

Instead of

((CustomArrayAdapter) ProduitsFragment.this.adapter).getFilter().filter(cs);

For more please refer: Custom Listview Adapter with filter Android

Community
  • 1
  • 1
Pramod Waghmare
  • 1,273
  • 13
  • 21
0

You are geting value from Hashmap.it is excption in your code.Get value from Product Mode.Which is refer an object. try code

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);
        } 
Muhammad Waleed
  • 2,517
  • 4
  • 27
  • 75
-1

Instead of parent in onItemClick() use view

HashMap<String, String> map = (HashMap) view.getItemAtPosition(position);
rahul sharma
  • 149
  • 1
  • 2
  • 11