简体   繁体   中英

Android java.util.hashmap cannot be cast to java.util.list

I'm trying to retrieve data from a json with multiple values and cast it into a listview but im gettting the error java.util.hashmap cannot be cast to java.util.list.

I'm using volley.

The FeedListActivity Class:

 public void updateList() {
        feedListView= (ListView) findViewById(R.id.custom_list);
        feedListView.setVisibility(View.VISIBLE);
        progressbar.setVisibility(View.GONE);

        feedListView.setAdapter(new CustomListAdapter(this, feedList));
        feedListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                Object o = feedListView.getItemAtPosition(position);
                ClientesContatosModel newsData = (ClientesContatosModel) o;

                Intent intent = new Intent(FeedListActivity.this, FeedDetailsActivity.class);
                intent.putExtra("nome", newsData);
                startActivity(intent);
            }
        });
    }
 JSONArray dados = json.getJSONArray("dados");
            // parsing json object
            for (int i = 0; i < dados.length(); i++) {

                JSONObject item = dados.getJSONObject(i);

                feedList = new ArrayList<ClientesModel>();
                ClientesModel mClientesModel = new ClientesModel();
                ClientesContatosModel mClientesContatoModel = new ClientesContatosModel();

                /* cadastra os dados necessários no objeto no modelo */
                mClientesModel.setId(item.optInt("id"));
                mClientesModel.setNome(item.optString("nome"));
                mClientesModel.setTipo_pessoa(item.optString("tipo_pessoa"));
                mClientesModel.setInformacoes_adicionais(item.optString("informacoes_adicionais"));
                mClientesModel.setCpf(item.optString("cpf"));
                mClientesModel.setCnpj(item.optString("cnpj"));
                JSONArray contatos = item.getJSONArray("contatos");
                for (int j = 0; j < contatos.length(); j++) {
                    JSONObject data = contatos.getJSONObject(j);

                    mClientesContatoModel.setNome(data.optString("nome"));
                    mClientesContatoModel.setCargo(data.optString("cargo"));

FeedDetailsActivity class:

public class FeedDetailsActivity extends Activity {

    private ClientesContatosModel feed;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feed_details);

        feed = (ClientesContatosModel) this.getIntent().getSerializableExtra("nome");

        if (null != feed) {


            TextView title = (TextView) findViewById(R.id.title);
            title.setText(feed.getNome());


        }
    }

Here is the Log:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.javatechig.feedreader/com.javatechig.feedreader.FeedDetailsActivity}: java.lang.ClassCastException: java.util.HashMap cannot be cast to com.javatechig.feedreader.model.ClientesContatosModel
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
                at android.app.ActivityThread.access$800(ActivityThread.java:139)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:136)
                at android.app.ActivityThread.main(ActivityThread.java:5086)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:515)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                at dalvik.system.NativeStart.main(Native Method)


Caused by: java.lang.ClassCastException: java.util.HashMap cannot be cast to com.javatechig.feedreader.model.ClientesContatosModel
                at com.javatechig.feedreader.FeedDetailsActivity.onCreate(FeedDetailsActivity.java:26)
                at android.app.Activity.performCreate(Activity.java:5248)
                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
                at android.app.ActivityThread.access$800(ActivityThread.java:139)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:136)
                at android.app.ActivityThread.main(ActivityThread.java:5086)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:515)

Because HashMap#values() return a java.util.Collection and you cant cast Collection into ArrayList, thus you get ClassCastException.

where as in case of ArrayList(HashMap.values()) ArrayList constructor takes Collection as an argument. Thus you wont get ClassCastException when you pass HashMap.values() as an argument to ArrayList.

HashMap#values(): check the return type in the source, as ask yourself, can a java.util.Collection be casted into java.util.ArrayList ?? No

 public Collection<V> values() {
921         Collection<V> vs = values;
922         return (vs != null ? vs : (values = new Values()));
923     }

ArrayList(Collection): check the argument type in the source. can a method whose argument is a super type accepts sub type ? Yes

public ArrayList(Collection<? extends E> c) {
151         elementData = c.toArray();
152         size = elementData.length; 
153         // c.toArray might (incorrectly) not return Object[] (see 6260652)
154         if (elementData.getClass() != Object[].class)
155             elementData = Arrays.copyOf(elementData, size, Object[].class);
156     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM