Android在JSON的片段中填充ListView

Android在JSON的片段中填充ListView,json,listview,android-fragments,android-listview,Json,Listview,Android Fragments,Android Listview,这是我沙发数据库中的JSON对象: 我发现本教程非常适合我的需要: 只有两个问题: -我需要a片段中的ListView,无法在onPreExecute()方法中调用“MainActivity.this”。 -我遇到的第二个问题是,我遇到了以下错误: org.json.JSONException:行没有值 以下是我的片段中的代码: ListView list; TextView name; TextView comment; TextView lat; Button Btngetdata;

这是我沙发数据库中的JSON对象:

我发现本教程非常适合我的需要:

只有两个问题: -我需要a片段中的ListView,无法在onPreExecute()方法中调用“MainActivity.this”。 -我遇到的第二个问题是,我遇到了以下错误: 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();
        }
    }
}
ListView列表;
文本视图名称;
文本视图评论;
文本视图lat;
按钮Btngetdata;
ArrayList oslist=新的ArrayList();
//获取JSON数组的URL
专用静态字符串url=”https://emamafinal.iriscouch.com/stadtrundgang/_design/nodes/_view/nodes";
//JSON节点名称
私有静态最终字符串标记_ROWS=“ROWS”;
私有静态最终字符串标记_NAME=“NAME”;
私有静态最终字符串标记_COMMENT=“COMMENT”;
私有静态最终字符串标记_LAT=“lation”;
JSONArray-android=null;
视图根视图;
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
rootView=充气机。充气(R.layout.list\u布局,容器,假);
oslist=newarraylist();
Btngetdata=(按钮)rootView.findviewbyd(R.id.getdata);
Btngetdata.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
新建JSONParse().execute();
}
});
返回rootView;
}
私有类JSONParse扩展了异步任务{
私人对话;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
name=(TextView)rootView.findViewById(R.id.name);
comment=(TextView)rootView.findViewById(R.id.comment);
lat=(TextView)rootView.findviewbyd(R.id.lat);
pDialog=新建进度对话框(MainActivity.this);
setMessage(“获取数据…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
}
@凌驾
受保护的JSONObject doInBackground(字符串…args){
JSONParser jParser=新的JSONParser();
//从URL获取JSON
JSONObject json=jParser.getJSONFromUrl(url);
返回json;
}
@凌驾
受保护的void onPostExecute(JSONObject json){
pDialog.disclose();
试一试{
//从URL获取JSON数组
android=json.getJSONArray(标记行);
for(int i=0;ivalue
HashMap=newHashMap();
地图放置(标签名称、名称);
地图放置(标签注释,注释);
地图放置(标记为LAT,LAT);
添加(地图);
list=(ListView)rootView.findViewById(R.id.list);
ListAdapter=new SimpleAdapter(MainActivity.this,oslist,
R.layout.list_项目,
新字符串[]{TAG_NAME,TAG_COMMENT,TAG_LAT},new int[]{
R.id.name,R.id.comment,R.id.lat});
list.setAdapter(适配器);
list.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
Toast.makeText(MainActivity.this),您在“+oslist.get(+position.get”(“name”)、Toast.LENGTH_SHORT.show()处单击过它;
}
});
}
}捕获(JSONException e){
e、 printStackTrace();
}
}
}
}


提前感谢您对此的任何提示。

您应该在此行中使用
getActivity()
,而不是
MainActivity。此

ListAdapter adapter = new SimpleAdapter(MainActivity.this, oslist,
                        R.layout.list_item, ...

ListAdapter adapter = new SimpleAdapter(getActivity(), oslist,
                        R.layout.list_item, ....
更正:将所有
MainActivity.更改为
getActivity()