Android java.util.hashmap无法强制转换为java.util.list

时间:2014-07-25 14:01:18

标签: java android android-arrayadapter android-volley

我正在尝试从具有多个值的json中检索数据并将其转换为listview但是我得到的错误java.util.hashmap无法转换为java.util.list。

我正在使用凌空。

FeedListActivity类:

 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类:

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


        }
    }

这是日志:

 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)

1 个答案:

答案 0 :(得分:2)

因为HashMap#values()返回一个java.util.Collection并且你无法将Collection转换为ArrayList,因此你得到ClassCastException。

在ArrayList(HashMap.values())的情况下,ArrayList构造函数将Collection作为参数。因此,当您将HashMap.values()作为参数传递给ArrayList时,您将不会获得ClassCastException。

HashMap#values():检查源代码中的返回类型,问问自己,可以将java.util.Collection转换为java.util.ArrayList吗?否

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

ArrayList(Collection):检查源中的参数类型。参数是超类型的方法可以接受子类型吗?是

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     }