3

我有一个ArrayList<HashMap<Contact, Name>>,我想ListView用它填充一个。这是我的尝试(不起作用)

    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

4

4 回答 4

3

只需使用 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);

然后fromandto很简单:

String[] from = new String[] { "value" };
int[] to = new int[] { android.R.id.text1 };
于 2013-01-04T10:42:56.280 回答
2

如果您真的想传递HashMaps 的整个列表,则必须创建自己的适配器,因为ArrayAdapter<String>在您的情况下期望第三个参数是 type List<String>

您应该在评论中遵循 @的建议,并从 HashMap 值Tomislav Novoselec创建一个。List<String>

于 2013-01-04T10:26:52.307 回答
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");
       }
    }
}

我对您的主要活动的建议

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

}
于 2013-01-04T10:32:59.480 回答
0

您必须通过在 Android 中扩展 BaseAdapter 来创建自己的自定义适配器。然后,您可以使用列表视图的 setAdapter 方法将自定义适配器设置为 ListView。

供您参考,请参阅下面的 BaseAdapter 小示例。您需要将 ArrayList< HashMaP > 传递给适配器。

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

希望这可以帮助。

于 2013-01-04T10:35:16.160 回答