2013-01-04 4 views
3

ArrayList<HashMap<Contact, Name>>이 있는데 ListView을 채 웁니다. 당신은 내가 모든 노력을했지만 난 여전히 작동 만들 수 없습니다ArrayList에서 ListView 채우기 <HashMap <String, String >>

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

수 : 여기 마지막 줄에 이클립스에서 오류를 던지고있다 그건 (작동하지 않는) 내 시도

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

있어 도와주세요, 제발?

미리 감사드립니다.

PS : 전체 소스 : https://gist.github.com/4451519

+3

ArrayAdapter와, 당신이있어 :

나는

map.put(key, mount); 

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

다음 fromto에 의해 단순히 대체 할 것이다 그것을 HashMaps의리스트로 제공한다. 그것과 생성자에게 전달 –

+0

hashmap의 목록도 SimpleAdapter – njzk2

+0

함께 간다, 오류는 이클립스에서 해당 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); 

목록의 각 항목에 매번 다른 키가 아닌 비슷한 형식의 개체가 포함되어 있으면 간단하고 논리적 일 것입니다. 은 문자열의 목록을 기대하고있다

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

안녕하세요 대단히 감사합니다! 이것은 가장 단순했고 그것은 내가 원했던 것입니다. 리스트가 안드로이드에서 어떻게 작동하는지 실제로 알지 못했기 때문에 몇 가지 연구를해야했습니다. 행에 대해 별도의 레이아웃을 만들어야한다는 것을 모릅니다. 이 튜토리얼 (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를 사용해야하며 코드에서이 클래스를 사용해야합니다. 당신의 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.co.kr/android-async-http /를 사용하면 쉽게 서버에서 응답을받을 수 있습니다. AsyncTask 및 MultiThreading을 처리합니다. –

+0

해당 라이브러리를 이용해 주셔서 감사 드리며, 많은 코드를 저장하고 읽고 수정하는 것이 더 쉽습니다. 내 프로그램에서 구현했습니다. 정말 고마워요! – kustomrtr

+0

문제 없습니다, 항상 도와 드리겠습니다 :) –

0

에 대한

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

나의 제안 당신은 안드로이드 BaseAdapter를 확장하여 사용자 정의 어댑터를 소유 만들어야합니다. 그런 다음 목록보기의 setAdapter 메서드를 사용하여 사용자 지정 어댑터를 ListView로 설정할 수 있습니다.

참조 자료는 아래의 작은 BaseAdapter 예제를 참조하십시오. ArrayList < HashMaP>를 어댑터에 전달해야합니다. 이 도움이

http://jimmanz.blogspot.in/2012/06/example-for-listview-using-baseadapter.html

희망.

관련 문제