2014-07-25 93 views
-1

我想從多個值的json中檢索數據並將其轉換爲列表視圖,但是我得到的錯誤java.util.hashmap不能轉換到java.util.list。Android 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) 
+0

你在哪裏使用散列表?我看不到它從這段代碼.. – nafas

+0

@nafas我編輯的代碼,我沒有使用Hashmap,但我得到的錯誤,我不知道如何 – AND4011002849

+0

技術上json是一個hashmap。所以我認爲你的jsonobject字段中的一個/一些是錯誤的(例如,你認爲它是一個字符串,但是它是其他的東西)。嘗試在每個***之後打印一個日誌mClientesModel.setX *** – nafas

回答

2

因爲HashMap中#值()返回一個java.util.Collection中並且你不能將Collection轉換爲ArrayList,因此你會得到ClassCastException。

在ArrayList(HashMap.values())的情況下ArrayList構造函數將Collection作爲參數。因此,當您將HashMap.values()作爲參數傳遞給ArrayList時,您不會得到ClassCastException。 ():檢查源代碼中的返回類型,就像問問自己,是否可以將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  } 
相關問題