2013-01-04 93 views
3

我有一個ArrayList<HashMap<Contact, Name>>,我想用它填充一個ListView。這是我嘗試(沒有工作)從ArrayList填充ListView <HashMap <String,String >>

ArrayList<HashMap<String, String>> lista = new ArrayList<HashMap<String, String>>(); 

    // Array of strings "titulos" 
    String titulos[] = { "Dolar (Transferencia)", "Euro (Transferencia)", 
     "Dolar (Efectivo)", "Euro (Efectivo)", "Dolar (cúcuta)", 
     "Euro (cucuta)" }; 

    try { 
     JSONObject json = result; // result is a JSONObject and the source is located here: https://dl.dropbox.com/u/8102604/dolar.json 
     JSONObject root = json.getJSONObject("root"); 
     JSONArray items = root.getJSONArray("item"); 
     int j = 0; 

     for (int i = 0; i < items.length(); i++) { 
      JSONObject item = items.getJSONObject(i); 
      String key = item.getString("key"); 
      String mount = item.getString("mount"); 
      if (key.equals("TS") || key.equals("TE") || key.equals("EE") 
        || key.equals("CE") || key.equals("ES") 
        || key.equals("CS")) { // i did this since i only need the items where the key is equal to TS, TE, EE, CE, ES or CS. 
       HashMap<String, String> map = new HashMap<String, String>(); 
       map.put("id", String.valueOf(i)); 
       map.put(key, mount); 
       lista.add(map); 
       System.out.println(titulos[j] + "(" + key + "). BsF = " + mount); // just for debugging purposes 
       j++; // add 1 to j if key is equal to TS, TE, EE, CE, ES or CS. In this way i can associate the two arrays (item and titulos) 
      } 
     } 

     ListView lv = (ListView) myMainActivity.findViewById(R.id.listView1); // create a list view 
     lv.setAdapter(new ArrayAdapter<String>(contexto, android.R.layout.simple_list_item_1, lista)); // set adapter to the listview (not working) 

    } catch (JSONException e) { 
     Log.e("log_tag", "Error parsing data " + e.toString()); 
    } 
} 

最後一行是Eclipse拋出一個錯誤:

The constructor ArrayAdapter<String>(Context, int, ArrayList<HashMap<String,String>>) is undefined 

我嘗試了一切,但我仍然無法使它工作,你能請幫幫我?

在此先感謝。

PS:完整的源:https://gist.github.com/4451519

+3

ArrayAdapter 期待字符串列表,而你爲它提供一個HashMaps列表。創建一個新的列表並將其傳遞給構造函數 –

+0

hashmap的列表也隨着SimpleAdapter – njzk2

+0

也一樣,錯誤不是來自eclipse,它來自javac。 – njzk2

回答

2

只需使用SimpleAdapter。

String[] from = new String[] { /* all your keys */}; 
int[] to = new int[] { /* an equal number of android.R.id.text1 */}; 
ListAdapter adapter = new SimpleAdapter(contexto, lista, android.R.layout.simple_list_item_1, from, to); 

如果你的列表中的每個項目包含一個類似形成的對象,而不是一個不同的密鑰,每次這將是簡單的(更符合邏輯)。

我將取代

map.put(key, mount); 

通過

map.put("key", key); 
map.put("value", mount); 

,然後fromto只是:

String[] from = new String[] { "value" }; 
int[] to = new int[] { android.R.id.text1 }; 
+0

非常感謝你!這是最簡單的,它只是我想要的。我不得不做一些研究,因爲我不知道列表在Android中的工作方式,我不知道我必須爲一行進行單獨的佈局。我遵循本教程(http://www.youtube.com/watch?v=FP2gElnwTSs),並使用您提供給我的信息,並知道我的應用程序以我想要的方式工作!這裏有一個截圖:[ScreenShot](http://i.imgur.com/MfrCx.png) – kustomrtr

2

你必須創建自己的適配器,如果你真的想傳遞的HashMap就是全部名單,爲ArrayAdapter<String>期待你的情況下,第三參數是的鍵入List<String>

您應該在評論中關注@Tomislav Novoselec的建議,並從HashMap值創建List<String>

2

您需要像下面一樣使用您自己的CustomArrayAdapter並在代碼中使用此類。

public class CustomArrayAdapter extends BaseAdapter { 
    private JSONArray jsonArray = null; 
    public ImageAdapter(Context c, JSONArray jsonArray) { 
     context = c; 
     this.jsonArray = jsonArray; 
    } 
    public int getCount() { 
     return jsonArray.length(); 
    } 
    public View getView(int position, View convertView, ViewGroup parent) { 
     //DO YOUR CODE HERE 
     LayoutInflater inflater = (LayoutInflater)  context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     if (convertView == null) { 
      convertView = inflater.inflate(R.layout.list_item_view, null); 
     }else{ 
      //Set values for your listview on the list item. 
      convertView.findViewById(R.id.someID).setText("GetJSONTEXT"); 
     } 
    } 
} 

我對你MAINACTIVITY

package com.kustomrtr.dolarparalelovenezuela; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import com.loopj.android.http.*; 
public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     AsyncHttpClient client = new AsyncHttpClient(); 
     client.get("http://192.168.1.5/dolar.json", new AsyncHttpResponseHandler() { 
      @Override 
      public void onSuccess(String response) { 
       System.out.println(response); 
       try { 
        JSONObject json = new JSONObject(response); // result is a JSONObject and the source is located here: https://dl.dropbox.com/u/8102604/dolar.json 
        JSONObject root = json.getJSONObject("root"); 
        JSONArray items = root.getJSONArray("item"); 

        ListView lv = (ListView) myMainActivity.findViewById(R.id.listView1); // create a list view 
        lv.setAdapter(new CustomArrayAdapter<String>(contexto, android.R.layout.simple_list_item_1, items)); 

       } catch (JSONException e) { 
        Log.e("log_tag", "Error parsing data " + e.toString()); 
       } 

      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 

} 
+0

你可以使用這個 - http:// loopj。com/android-async-http /並輕鬆獲得服務器的響應。它會爲你處理AsyncTask和MultiThreading。 –

+0

非常感謝那個庫,它節省了大量的代碼,並且它更易於閱讀和修改。我在我的程序中實現了它,非常感謝! – kustomrtr

+0

沒問題,總是樂於幫忙:) –

相關問題