qq_33055553
qq_33055553
2016-07-01 01:40
采纳率: 33.3%
浏览 1.1k
已采纳

android控件不展示,求大神解决

小弟刚刚学习android,新手一枚,这两天再搞android通过webservice取的数据展示到listview上
我经过调试,确定数据已经取到,sql也非常简单,select * from table,但是list控件并没有展示到模拟器上,搞了很久也没找到问题的根本,希望有大神能够帮助指点一下,感激不尽

以下是我的android代码:

package com.dcdz.carmensys;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.dcdz.db.DbHelper;
import com.example.carmensys.R;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.StrictMode;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ListActivity;
import android.view.Menu;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends ListActivity {
DbHelper db=new DbHelper();
private PreparedStatement pstmt;
private ResultSet resultSet;
ArrayList> list=new ArrayList>();
SimpleAdapter sp=null;

 @SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     Thread th=new Thread(ran);
     th.start();

}

 Handler handler=new Handler(){
     @Override  
        public void handleMessage(Message msg) {  
            super.handleMessage(msg);  

             showList();

        }  
 };

 public void showList(){
     SimpleAdapter sp=new SimpleAdapter(this, list, R.layout.listitemone, new String[]{"userid","username","usertime"}, new int[]{R.id.userid,R.id.username,R.id.usertime});
    setListAdapter(sp);
 }

 Runnable ran=new Runnable()
    {
        @Override
        public void run()
        {
            getlist();
        }
    };

 public void getlist(){
     HashMap<String, String> map =new HashMap<String, String>();
    String requestUrl="http://192.168.1.104:8080/SysCarMen/servlet/getListServlet";
         HttpClient client = new DefaultHttpClient();  
            // 根据URL创建HttpPost实例  mm
            HttpPost post = new HttpPost(requestUrl);  
            HttpGet getx=new HttpGet(requestUrl);
            List<NameValuePair> params = new ArrayList<NameValuePair>();  
            params.add(new BasicNameValuePair("", ""));  
            try {  
                // 设置URL编码  
                post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));  
                // 发送请求并获取反馈  
                HttpResponse response = client.execute(post);  


                // 判断请求是否成功处理  
                if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {  
                    // 解析返回的内容  
                    String result = EntityUtils.toString(response.getEntity());  
                    JSONArray jsonArray = new JSONArray(result);
                    JSONObject jsonObject; 
                    for (int i = 0; i < jsonArray.length(); i++) {
                        jsonObject = jsonArray.getJSONObject(i);
                        list.add(getMap(jsonObject.toString())); 
                    }
                    System.out.println(list.toArray());
                }  

            } catch (Exception e) {  
                e.printStackTrace();  
            }  


 }














    public static HashMap<String, String> getMap(String jsonString)
    {
    JSONObject jsonObject;
    try
    {
    jsonObject = new JSONObject(jsonString); @SuppressWarnings("unchecked")
    Iterator<String> keyIter = jsonObject.keys();
    String key;
    String value;
    HashMap<String, String> valueMap = new HashMap<String, String>();
    while (keyIter.hasNext())
    {
    key = (String) keyIter.next();
    value = jsonObject.get(key).toString();
    valueMap.put(key, value);
    }
    return valueMap;
    }
    catch (JSONException e)
    {
    e.printStackTrace();
    }
    return null;
    } 

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • hexingen
    新根 2016-07-01 01:50
    已采纳
        你忘记写handler发送信息了,在  System.out.println(list.toArray());前面多加一句handler.obtainMessage().sendToTarget();
    
    点赞 评论

相关推荐