我是想要把我从json解析得到的字符串数据 添加到listview里每一个item的textview里。现在遇到了点问题,求助大神。这个问题应该很基础的。。 主要问题是我得到的数据shuzhangyalist 在getdata方法里  谢谢各位了
上代码public class ShuzhangyaGraph extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.graph_of_shuzhangya);
                ListView listview = (ListView) findViewById(R.id.listView1);
ListAdapter adapter = new MyAdapter(this);
listview.setAdapter(adapter); getdata();
} public void getdata() { try {
HttpClient httpClient = new DefaultHttpClient();
String urlStr = "http://211.83.105.83/test2/MobileHealth/OrdersListAction.aspx?UserID=123";
HttpPost post = new HttpPost(new URI(urlStr));
JSONObject json = new JSONObject();
json.put("UserID", 123);
json.put("UserName", "name1"); post.setEntity(new StringEntity(json.toString()));
HttpResponse httpResponse = httpClient.execute(post); if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
// 获取服务器响应的json字符串
String jsonobj = EntityUtils.toString(entity);
System.out.println("jsonobj:" + jsonobj);
// 解析从服务器返回的json
JSONObject obj = new JSONObject(jsonobj);
JSONArray forumobj = new JSONArray();
forumobj = obj.optJSONArray("datalist");
JSONArray arr = new JSONArray(forumobj.toString());
ArrayList shuzhangyalist = new ArrayList();
for (int i = 0; i < arr.length(); i++) {
JSONObject temp = (JSONObject) arr.get(i);
String shuzhangyadata = temp
.getString("DiastolicPressure");
shuzhangyalist.add(shuzhangyadata);
} }
} } catch (Exception e) {
e.printStackTrace();
}
ListView listview = (ListView) findViewById(R.id.listView1);
ListAdapter adapter = new MyAdapter(this);
listview.setAdapter(adapter);
} private class MyAdapter extends BaseAdapter { private Context context; private LayoutInflater inflater; public ArrayList<String> arr; public MyAdapter(Context context) { super(); this.context = context; inflater = LayoutInflater.from(context); arr = new ArrayList<String>(); for (int i = 0; i < 7; i++) { // listview初始化3个子项 arr.add(""); } } @Override
public int getCount() { // TODO Auto-generated method stub return arr.size(); } @Override
public Object getItem(int arg0) { // TODO Auto-generated method stub return arg0; } @Override
public long getItemId(int arg0) { // TODO Auto-generated method stub return arg0; } public View getView(final int position, View view, ViewGroup arg2) { // TODO Auto-generated method stub
ViewHolder holder; if (view == null) { view = inflater.inflate(
R.layout.graph_of_shuzhangya_list_item1, null);
holder = new ViewHolder();
holder.shuzhangyaview = (TextView) findViewById(R.id.ShuzhangyaView1);

holder.shuzhangyaview = (TextView) findViewById(R.id.ShuzhangyaView1);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
return view; } private class ViewHolder {
private TextView shuzhangyaview; } } public void data_back(View v) {
finish();
} }

解决方案 »

  1.   

    MyAdapter 里写个构造方法,把shuzhangyalist传进去不就行了么
      

  2.   


    shuzhangyalist 你这变量 ,类命名也太.... private class MyAdapter extends BaseAdapter {
     
    ArrayList list;
    Context context;
    //你可以如楼上所说的写一个构造方法,将data传进去
    public MyAdapter(Context context,ArrayList list){
           this.list = list;
           this.context = context;
    }
    // 或者set进去
    //public void setData(ArrayList list){
    //         this.list = list;
    //}}
      

  3.   

    shuzhangyalist 传入 arr 中 通过初始化