从url获取json对象中的数据但无法将其绑定到android应用程序中的listview

时间:2015-06-01 09:36:13

标签: android json listview

我正在创建android应用程序,其中根据当前经度和纬度从数据库获取数据。我在json对象中获取了数据,但无法在listview中绑定它。请指导我。

  

块引用

**@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.shopdetail);
    aq=new AQuery(this);
        GPSTracker  gps = new GPSTracker(Shopdetail.this);
                   if(gps.canGetLocation()){
                     double latitude = gps.getLatitude();
                        double longitude = gps.getLongitude();
                        getdatalatlog(latitude,longitude);  
                     }else{
                        gps.showSettingsAlert();
                          }
                    }**

getdatalatlog(纬度,经度)方法如下。

  

块引用

private void getdatalatlog(double latitude, double longitude) {

    String link = "http://192.168.0.104/PHP/webservice/comments.php?latitude='"+latitude+"'&longitude='"+longitude+"'";
    aq.progress(R.id.progressBar1).ajax(link, JSONObject.class, this,"jsonCallback");
}
public void jsonCallback(String link, JSONObject json, AjaxStatus status) {             
    mCommentList = new ArrayList<HashMap<String, String>>();
    JSONParser jParser = new JSONParser();
    json = jParser.getJSONFromUrl(link);
    try {
        (success==1)
        mComments = json.getJSONArray(TAG_POSTS);
        for (int i = 0; i < mComments.length(); i++) {
            JSONObject c = mComments.getJSONObject(i);
            String title = c.getString(TAG_TITLE);
            String content = c.getString(TAG_MESSAGE);
            String username = c.getString(TAG_USERNAME);
            HashMap<String, String> map = new HashMap<String, String>();
            map.put(TAG_TITLE, title);
            map.put(TAG_MESSAGE, content);
            map.put(TAG_USERNAME, username);
            mCommentList.add(map);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
ListAdapter adapter = new SimpleAdapter(this, mCommentList,
            R.layout.single_post, new String[] { TAG_TITLE,TAG_USERNAME ,TAG_MESSAGE
                    }, new int[] { R.id.shop_name,R.id.address,R.id.distance
                     });
    setListAdapter(adapter);
    ListView lv = getListView();    
    lv.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
        }
    });
}*

1 个答案:

答案 0 :(得分:0)

请这样做可能会对你有所帮助:

ListAdapter adapter = new SimpleAdapter(this, mCommentList,
                R.layout.single_post, new String[] { TAG_TITLE,TAG_USERNAME ,TAG_MESSAGE
                        }, new String[] { getResources().getString(R.id.shop_name),getResources().getString(R.id.address),getResources().getString(R.id.distance)
                         });

这里我将整数类型数组更改为String,因为正如您所做的那样,它显示了您唯一的整数值或空值。但是我们想要使用来自android的资源,那么我们必须使用get Resource()方法来提及它。