package com.example.listwebimg;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ListView;
public class MainActivity extends Activity {
/ ** Called when the activity is first created. * /
private ArrayList> mylist;
@ Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.main);
new MyThread () start ();.
ListView list = (ListView) findViewById (R.id.listviewimg01);
mylist = new ArrayList> ();
ArrayList> mylist2 = mylist;
MySimpleAdapter mSchedule = new MySimpleAdapter (this, / / no explanation
mylist2, / / data source
R.layout.item, / / ListItem XML implementation
/ / Dynamic array with the corresponding sub-item
ListItemnew String [] {"title", "imgsrc"},
/ / ListItem XML file inside the two TextView ID
new int [] {R.id.listview_item_textview01, R.id.listview_item_imageview01});
list.setAdapter (mSchedule);
}
private class MyThread extends Thread {
public void run () {
/ / You want to achieve code
/ / Add list content
HashMapmap = new HashMap ();
map.put ("title", "Hello World0");
map.put ("imgsrc", "http://www.baidu.com/img/baidu_logo_jr_1003_315.gif");
mylist.add (map);
/ * HashMapmap1 = new HashMap ();
map1.put ("title", "Hello World1");
map1.put ("imgsrc", "http://www.google.cn/intl/zh-CN/images/logo_cn.gif");
mylist.add (map1); * /
}
}
/ **
* Load data
* @ Return
* /
}
I do not do is open a new thread? How also can not display ah<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Start calling thread is not to say it will be executed immediately, you this is basically to list.setAdapter (mSchedule); MyThread when you are not there to perform. You add in the back start Sleep (1000) try to see will not be displayed
Reply:
Child threads do not operate with the possibility of UI great error handler code
Reply:
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
lView = (ListView) findViewById (R.id.listview);
resultView = (TextView) findViewById (R.id.t);
getRemoteInfo ("1");
}
public void getRemoteInfo (String phoneSec) {
new Thread (new Runnable () {
@ Override
public void run () {
/ / Namespace
String nameSpace = "http://sp.qwsy.com/";
/ / Method name
callString methodName = "GetBookInfo";
/ / EndPoint
String endPoint = "http://sp.qwsy.com/book.asmx";
/ / SOAP Action
String soapAction = "http://sp.qwsy.com/GetBookInfo";
/ / Verify Leopard head
Element [] header = new Element [1];
header [0] = new Element () createElement (nameSpace, "MyCustomSoapHeader");.
Element Spid = new Element () createElement (nameSpace, "Spid");.
Spid.addChild (Node.TEXT, "10001");
header [0] addChild (Node.ELEMENT, Spid);.
Element Qtime = new Element () createElement (nameSpace, "Qtime");.
Qtime.addChild (Node.TEXT, "20130510144000");
header [0] addChild (Node.ELEMENT, Qtime);.
Element Sign = new Element () createElement (nameSpace, "Sign");.
Sign.addChild (Node.TEXT, "30fc3bfd81120422561fd849efc44853");
header [0] addChild (Node.ELEMENT, Sign);.
/ / Specify WebService namespace and method name to call
SoapObject rpc = new SoapObject (nameSpace, methodName);
Log.i ("top", rpc.toString ());
/ / Set the required incoming call WebService interface requires two parameters mobileCode, userId, we can not write, must be the same name and parameters provided
/ / Rpc.addProperty ("mobileCode", "15975505657");
/ / Rpc.addProperty ("userId", "");
rpc.addProperty ("bookID", "41");
/ / Generate method call WebService SOAP request message, and specify the version of SOAP
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope (
SoapEnvelope.VER11);
envelope.bodyOut = rpc;
/ / Set whether the call is dotNet development WebService
envelope.dotNet = true;
envelope.headerOut = header;
/ / Equivalent to envelope.bodyOut = rpc;
envelope.setOutputSoapObject (rpc);
HttpTransportSE transport = new HttpTransportSE (endPoint);
try {
/ / Call WebService
transport.call (soapAction, envelope);
} Catch (Exception e) {
e.printStackTrace ();
}
/ / Get the returned data
SoapObject object = (SoapObject) envelope.bodyIn;
/ / Get the results returned bookpic
String result = ((SoapObject) ((SoapObject) object.getProperty (0)) getProperty (0).) GetProperty ("zzjs") toString ();..
String name = (. (SoapObject) ((SoapObject) object.getProperty (0)) getProperty (0)) getProperty ("bookname") toString ();..
. String pic = (. (SoapObject) ((SoapObject) object.getProperty (0)) getProperty (0)) getProperty ("bookpic") toString ();.
Message message = handler.obtainMessage ();
Log.i ("end", result);
ArrayList > listItem = new ArrayList > ();
for (int i = 0; i <100; i + +)
{
HashMap map = new HashMap ();
map.put ("ItemImage", pic) ;/ / image resource ID
map.put ("ItemTitle", name);
map.put ("ItemText", result);
listItem.add (map);
}
/ / Generate adapter Item and dynamic arrays corresponding element
MySimpleAdapter listItemAdapter = new MySimpleAdapter (MainActivity.this, listItem, / / data source
R.layout.list_item, / / ListItem XML implementation
/ / Dynamic array with the corresponding sub-item
ImageItemnew String [] {"ItemImage", "ItemTitle", "ItemText"},
A ImageView / / ImageItem XML file inside, two TextView ID
new int [] {R.id.ItemImage, R.id.ItemTitle, R.id.ItemText}
);
/ / Add and display
message.obj = listItemAdapter;
handler.sendMessage (message);
/ / Click event
lView.setOnItemClickListener (new OnItemClickListener () {
@ Override
public void onItemClick (AdapterView arg0, View arg1, int arg2,
long arg3) {
setTitle ("Click on the first" + arg2 + "items");
}
});
}
.}) Start ();
}
private Handler handler = new Handler () {
public void handleMessage (android.os.Message msg) {
/ / Get the results back to the WebService TextView
lView.setAdapter ((ListAdapter) msg.obj);
};
};
} wrote also the thread problem ah, 4.0 2.2 can not be .. I'm going crazy, nothing new to Andrews two weeks, find out how big God pointing this problem ah 4.0 Access threadReply:
HttpTransportSE transport = new HttpTransportSE (endPoint); want to use multiple threads, the 4.0 will block the main thread of the process must be open-thread execution
new Thread () {
public void run () {
}
.} Start ();
No comments:
Post a Comment