【发布时间】:2015-03-05 10:42:10
【问题描述】:
我是 android 的初学者。因为我想从本地存储在该应用程序中的 JSON 文件显示列表视图。当我尝试这个时,我遇到了一个错误
Error:(83, 52) error: no suitable constructor found for ArrayAdapter(PrimaryActivity,int,ArrayList<HashMap<String,String>>)
constructor ArrayAdapter.ArrayAdapter(Context,int,int) is not applicable
(argument mismatch; ArrayList<HashMap<String,String>> cannot be converted to int)
constructor ArrayAdapter.ArrayAdapter(Context,int,String[]) is not applicable
(argument mismatch; ArrayList<HashMap<String,String>> cannot be converted to String[])
constructor ArrayAdapter.ArrayAdapter(Context,int,List<String>) is not applicable
(argument mismatch; ArrayList<HashMap<String,String>> cannot be converted to List<String>)
我的代码在这里
public String loadJSONFromAsset() {
String json = null;
try {
InputStream is = getAssets().open("locate.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
我的活动代码放在这里。将数组列表值放入列表视图时出错。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_primary);
try {
JSONObject obj = new JSONObject(loadJSONFromAsset());
JSONArray jsonArray=obj.getJSONArray("Manali");
ArrayList<HashMap<String, String>> formList= new ArrayList<HashMap<String, String>>();
HashMap<String, String> m_li;
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jo_inside = jsonArray.getJSONObject(i);
Log.d("Details-->", jo_inside.getString("location"));
String location = jo_inside.getString("location");
String url = jo_inside.getString("url");
m_li=new HashMap<String, String>();
m_li.put("location", location );
m_li.put("url", url );
formList.add(m_li);
}
ListView listView=(ListView)findViewById(R.id.listView1);
//got error this line:I can't understnd this line
final ArrayAdapter<String> listAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,formList);
listView.setAdapter(listAdapter);
} catch (JSONException e) {
e.printStackTrace();
}
Button button= (Button) findViewById(R.id.btndis);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
我的 JSON 数据文件名为 locate.json
{
"Manali": [
{
"location": "Part of Manali Market",
"url": "ps1"
},
{
"location": "Kamaraj salai",
"url": "ps2"
}
]
}
【问题讨论】:
-
您的构造函数参数不正确。看看@developer.android.com/reference/android/widget/…。您可以使用自定义适配器
标签: android json android-listview android-arrayadapter