miao~

mimi

Android获取Json数据并显示在listview里面

public class MainActivity extends Activity {

    private ListView lv;
    private List<GCXX> listItems = new ArrayList<GCXX>();
    private MyPagerAdater myPagerAdater;
    private String json;
    private int pageNo = 1;
    private String name = "zhouhui";
    private Handler handler = new Handler() {

        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            if (msg.what == 1) {
                setData();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lv = (ListView) findViewById(R.id.lv);
        jsonTest();


    }


    private void jsonTest() {
        new Thread() {
            @Override
            public void run() {
                json = WebServiceUtil.everycanforStr("username", "search", "", "", "", "pageNo",
                        name, "", "", "", "", pageNo, "GetGCXX");
                JSONArray jsonObjs;
                JSONObject jsonObj;
                JSONTokener jsonTokener = new JSONTokener(json);
                try {
                    JSONObject person = (JSONObject) jsonTokener.nextValue();
                    jsonObjs = person.getJSONArray("gcxx");
                    for (int i = 0; i < jsonObjs.length(); i++) {
                        GCXX gcxx = new GCXX();
                        jsonObj = (JSONObject) jsonObjs.opt(i);
                        gcxx.setID(jsonObj.getInt("XMInfo_id"));
                        gcxx.setXMInfo_zls(jsonObj.getInt("XMInfo_zls"));

                        if (jsonObj.getString("XMInfo_mc") != null) {
                            gcxx.setXMInfo_mc(jsonObj.getString("XMInfo_mc"));
                        } else {
                            gcxx.setXMInfo_mc("");

                        }

                        listItems.add(gcxx);
                        Log.d("121212", gcxx.getXMInfo_mc());

                    }

                    Message msg = new Message();
                    msg.what = 1;
                    handler.sendMessage(msg);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }.start();

    }


    private void setData() {
        myPagerAdater = new MyPagerAdater(this, listItems);
        lv.setAdapter(myPagerAdater);
    }

}

public class WebServiceUtil {

    static final String SERVICE_NS = "http://tempuri.org/";
    static String SERVICE_URL = "http://miao";

    public static String getURL() {
        return SERVICE_URL;
    }

    // 多参数调用的方法(name1,name2,name3是添加的属性参数,设置为""时不添加到属性值中)
    public static String everycanforStr(
            String name1,  String name2,  String name3,  String name4, String name5,String name6,
            String value1, String value2, String value3, String value4,String value5,int value6,String method) {

        String methodName =method;
        HttpTransportSE ht = new HttpTransportSE(SERVICE_URL);
        ht.debug = true;
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        SoapObject soapObject = new SoapObject(SERVICE_NS, methodName);

        if (!name1.equals("")) {
            soapObject.addProperty(name1,value1);
        }
        if (!name2.equals("")) {
            soapObject.addProperty(name2,value2);
        }
        if (!name3.equals("")) {
            soapObject.addProperty(name3,value3);
        }
        if (!name4.equals("")) {
            soapObject.addProperty(name4,value4);
        }
        if (!name5.equals("")) {
            soapObject.addProperty(name5,value5);
        }
        if (!name6.equals("")) {
            soapObject.addProperty(name6,value6);
        }


        envelope.bodyOut = soapObject;
        envelope.dotNet = true;
        envelope.encodingStyle="UTF-8";
        try
        {
            ht.call(SERVICE_NS + methodName, envelope);
            if (envelope.getResponse() != null)
            {
                SoapObject result = (SoapObject) envelope.bodyIn;

                return result.getProperty(0)+"";
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
            return null;
        }
        catch (XmlPullParserException e)
        {
            e.printStackTrace();
            return null;
        }
        return null;
    }

}



public class MyPagerAdater extends BaseAdapter {
    private Context context;
    private List<GCXX> listItems;
    private LayoutInflater listContainer;


    public MyPagerAdater(Context context, List<GCXX> listItems) {
        this.context = context;
        listContainer = LayoutInflater.from(context); // 创建视图容器并设置上下文
        this.listItems = listItems;

    }


    @Override
    public int getCount() {
        if (listItems == null) {
            return 0;
        } else {
            return listItems.size();
        }
    }

    @Override
    public Object getItem(int position) {

        return null;
    }

    @Override
    public long getItemId(int position) {

        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = listContainer.inflate(R.layout.list_item, null);
            viewHolder.image = (ImageView) convertView
                    .findViewById(R.id.avator);
            viewHolder.textView1 = (TextView) convertView
                    .findViewById(R.id.name);
            viewHolder.textView2= (TextView) convertView
                    .findViewById(R.id.sj);
            viewHolder.textView3 = (TextView) convertView
                    .findViewById(R.id.zt);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.textView1.setText(listItems.get(position).getXMInfo_mc());
        viewHolder.textView2.setText(listItems.get(position).getXMInfo_zmj());
        viewHolder.textView3.setText(listItems.get(position).getXMInfo_xmbh()+" "+listItems.get(position).getXMInfo_zls());

       /* viewHolder.textView1.setText(listItems.get(position).getXMInfo_bjsj().toString());
        viewHolder.textView2.setText(listItems.get(position).getXMInfo_cs().toString());*/
        return convertView;
    }

    final static class ViewHolder {
        ImageView image;
        TextView textView1;
        TextView textView2;
        TextView textView3;
    }
}


public class GCXX implements Serializable {
    private int ID;
    
public int getID() {
    return ID;
}

阅读更多
上一篇Junit
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭