Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从ArrayList填充ListView<;HashMap<;字符串,字符串>&燃气轮机;_Java_Android - Fatal编程技术网

Java 从ArrayList填充ListView<;HashMap<;字符串,字符串>&燃气轮机;

Java 从ArrayList填充ListView<;HashMap<;字符串,字符串>&燃气轮机;,java,android,Java,Android,我有一个ArrayList,我想用它填充一个ListView。这是我的尝试(不起作用) ArrayList lista=new ArrayList(); //字符串数组“titulos” 字符串titulos[]={“Dolar(Transferencia)”,“Euro(Transferencia)”, “多拉(埃菲蒂沃)”、“欧元(埃菲蒂沃)”、“多拉(库库塔)”, “欧元(库库塔)”; 试一试{ JSONObject json=result;//结果是一个JSONObject,源代码位于此

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

ArrayList lista=new ArrayList();
//字符串数组“titulos”
字符串titulos[]={“Dolar(Transferencia)”,“Euro(Transferencia)”,
“多拉(埃菲蒂沃)”、“欧元(埃菲蒂沃)”、“多拉(库库塔)”,
“欧元(库库塔)”;
试一试{
JSONObject json=result;//结果是一个JSONObject,源代码位于此处:https://dl.dropbox.com/u/8102604/dolar.json
JSONObject root=json.getJSONObject(“根”);
JSONArray items=root.getJSONArray(“item”);
int j=0;
对于(int i=0;i
最后一行在eclipse中抛出一个错误:

The constructor ArrayAdapter<String>(Context, int, ArrayList<HashMap<String,String>>) is undefined
构造函数ArrayAdapter(上下文、int、ArrayList)未定义
我什么都试过了,但还是不行,你能帮帮我吗

提前谢谢


PS:Full source:

如果您真的想传递整个
HashMap
s列表,您必须创建自己的适配器,因为
ArrayAdapter
希望您案例中的第三个参数是
list
类型


您应该按照@
Tomislav Novoselec
在评论中的建议,从HashMap值创建一个
列表。

您需要使用您自己的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;
    }

}
package com.kustomrtr.dolarparalelovenezuela;
导入android.app.Activity;
导入android.os.Bundle;
导入android.view.Menu;
导入com.loopj.android.http.*;
公共类MainActivity扩展了活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AsyncHttpClient=新的AsyncHttpClient();
client.get(“http://192.168.1.5/dolar.json,新的AsyncHttpResponseHandler(){
@凌驾
成功时公共无效(字符串响应){
System.out.println(响应);
试一试{
JSONObject json=new JSONObject(response);//结果是一个JSONObject,源位于此处:https://dl.dropbox.com/u/8102604/dolar.json
JSONObject root=json.getJSONObject(“根”);
JSONArray items=root.getJSONArray(“item”);
ListView lv=(ListView)myMainActivity.findViewById(R.id.listView1);//创建列表视图
lv.setAdapter(新的CustomArrayAdapter(contexto,android.R.layout.simple_list_item_1,items));
}捕获(JSONException e){
Log.e(“Log_标记”,“错误解析数据”+e.toString());
}
}
});
} 
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.activity\u主菜单);
返回true;
}
}

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

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


希望这有帮助。

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

然后
from
to
就是:

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

ArrayAdapter需要一个字符串列表,而您正在为其提供一个HashMaps列表..创建一个新列表,并使用该列表中的值将其传递给constructorlist of hashmap使用SimpleAdapteralso,错误不是来自eclipse,而是来自javac。您可以使用它,并且可以轻松地从服务器获得响应。它将为您处理异步任务和多线程。非常感谢您提供了该库,它节省了大量代码,并且更易于阅读和修改。我在我的程序中实现了它,非常感谢!没问题,总是很乐意帮忙:)嘿,非常感谢!这是最简单的,它正是我想要的。我不得不做一些研究,因为我真的不知道列表在android中是如何工作的,我也不知道我必须为一行做一个单独的布局。我遵循了本教程()并使用了您提供的信息,我知道我的应用程序已经按照我想要的方式工作了!以下是一个屏幕截图:
map.put("key", key);
map.put("value", mount);
String[] from = new String[] { "value" };
int[] to = new int[] { android.R.id.text1 };