这是我的沙发数据库中的JSON对象: https://emamafinal.iriscouch.com/stadtrundgang/_design/nodes/_view/nodes
我发现这个教程完全符合我的需要: http://www.learn2crack.com/2013/11/listview-from-json-example.html
只有两个问题: - 我需要在我的片段中使用ListView,我无法调用" MainActivity.this"在onPreExeute()方法中。 - 我遇到的第二个问题是我收到此错误: org.json.JSONException:没有行的值
以下是我的片段中的代码:
ListView list;
TextView name;
TextView comment;
TextView lat;
Button Btngetdata;
ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();
//URL to get JSON Array
private static String url = "https://emamafinal.iriscouch.com/stadtrundgang/_design/nodes/_view/nodes";
//JSON Node Names
private static final String TAG_ROWS = "rows";
private static final String TAG_NAME = "name";
private static final String TAG_COMMENT = "comment";
private static final String TAG_LAT = "latitude";
JSONArray android = null;
View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.list_layout, container,false);
oslist = new ArrayList<HashMap<String, String>>();
Btngetdata = (Button)rootView.findViewById(R.id.getdata);
Btngetdata.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new JSONParse().execute();
}
});
return rootView;
}
private class JSONParse extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
name = (TextView)rootView.findViewById(R.id.name);
comment = (TextView)rootView.findViewById(R.id.comment);
lat = (TextView)rootView.findViewById(R.id.lat);
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Getting Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... args) {
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl(url);
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
pDialog.dismiss();
try {
// Getting JSON Array from URL
android = json.getJSONArray(TAG_ROWS);
for(int i = 0; i < android.length(); i++){
JSONObject c = android.getJSONObject(i);
// Storing JSON item in a Variable
String name = c.getString(TAG_NAME);
String comment = c.getString(TAG_COMMENT);
String lat = c.getString(TAG_LAT);
// Adding value HashMap key => value
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_NAME, name);
map.put(TAG_COMMENT, comment);
map.put(TAG_LAT, lat);
oslist.add(map);
list=(ListView)rootView.findViewById(R.id.list);
ListAdapter adapter = new SimpleAdapter(MainActivity.this, oslist,
R.layout.list_item,
new String[] { TAG_NAME,TAG_COMMENT, TAG_LAT }, new int[] {
R.id.name,R.id.comment, R.id.lat});
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, "You Clicked at "+oslist.get(+position).get("name"), Toast.LENGTH_SHORT).show();
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
提前感谢任何提示。
答案 0 :(得分:0)
您应该在此行中使用getActivity()
而不是MainActivity.this
:
ListAdapter adapter = new SimpleAdapter(MainActivity.this, oslist,
R.layout.list_item, ...
ListAdapter adapter = new SimpleAdapter(getActivity(), oslist,
R.layout.list_item, ....
更正:将所有MainActivity.this
更改为getActivity()