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<HashMap<String, Object>> 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<HashMap<String, Object>>(); 
ArrayList<HashMap<String, Object>> mylist2 = mylist; 
MySimpleAdapter mSchedule = new MySimpleAdapter(this, //没什么解释  
            mylist2,//数据来源   
            R.layout.item,//ListItem的XML实现  
            //动态数组与ListItem对应的子项          
            new String[] {"title","imgsrc"},   
            //ListItem的XML文件里面的两个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(){
   //你要实现的代码      
//添加list内容
     HashMap<String, Object> map = new HashMap<String, Object>();
     map.put("title", "Hello World0");  
     map.put("imgsrc", "http://www.baidu.com/img/baidu_logo_jr_1003_315.gif");
     mylist.add(map); 

     /*HashMap<String, Object> map1 = new HashMap<String, Object>();
     map1.put("title", "Hello World1");  
     map1.put("imgsrc", "http://www.google.cn/intl/zh-CN/images/logo_cn.gif");
     mylist.add(map1); */
    } 
   }
    
    /**
     * 装载数据
     * @return
     */
    
    
    

}
我这样不算是开新线程吗?怎么还显示不了啊网络Android多线程

解决方案 »

  1.   

    线程不是说调用了start就会马上执行的,你这个情况基本上是到list.setAdapter(mSchedule);那里时你的MyThread 都还没执行。你在start后面加上Sleep(1000)试试,看会不会显示
      

  2.   

    子线程不要对UI操作 用handler 这段代码 报错的可能性极大
      

  3.   

        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() {     // 命名空间
       String nameSpace = "http://sp.qwsy.com/";   // 调用的方法名称
       String methodName = "GetBookInfo";   // EndPoint
       String endPoint = "http://sp.qwsy.com/book.asmx";   // SOAP Action
       String soapAction = "http://sp.qwsy.com/GetBookInfo";
       //验证豹纹头
       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); 
       // 指定WebService的命名空间和调用的方法名
       SoapObject rpc = new SoapObject(nameSpace, methodName);
       Log.i("top",rpc.toString());
       // 设置需调用WebService接口需要传入的两个参数mobileCode、userId,不可以随便写,必须和提供的参数名相同
       //rpc.addProperty("mobileCode", "15975505657");
       //rpc.addProperty("userId", "");
       rpc.addProperty("bookID","41");   // 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本
       SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
         SoapEnvelope.VER11);
      
       envelope.bodyOut = rpc;
     
       // 设置是否调用的是dotNet开发的WebService
       envelope.dotNet = true;
       envelope.headerOut = header; 
       // 等价于envelope.bodyOut = rpc;
       envelope.setOutputSoapObject(rpc);
       HttpTransportSE transport = new HttpTransportSE(endPoint);   try {
        // 调用WebService
        transport.call(soapAction, envelope);
       } catch (Exception e) {    e.printStackTrace();
       }   // 获取返回的数据
       SoapObject object = (SoapObject) envelope.bodyIn;
      
       // 获取返回的结果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<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();  
         for(int i=0;i<100;i++)  
         {  
             HashMap<String, Object> map = new HashMap<String, Object>();  
             map.put("ItemImage",pic);//图像资源的ID  
             map.put("ItemTitle",name);  
             map.put("ItemText",result);  
             listItem.add(map);  
         }  
         //生成适配器的Item和动态数组对应的元素  
         MySimpleAdapter  listItemAdapter = new MySimpleAdapter (MainActivity.this,listItem,//数据源   
             R.layout.list_item,//ListItem的XML实现  
             //动态数组与ImageItem对应的子项          
             new String[] {"ItemImage","ItemTitle", "ItemText"},   
             //ImageItem的XML文件里面的一个ImageView,两个TextView ID  
             new int[] {R.id.ItemImage,R.id.ItemTitle,R.id.ItemText}  
         );  
          
         //添加并且显示  
         message.obj=listItemAdapter;
         handler.sendMessage(message);
         //点击事件
         lView.setOnItemClickListener(new OnItemClickListener() {
         @Override  
             public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,  
                     long arg3) {  
                 setTitle("点击第"+arg2+"个项目");  
             }  
    });
      
      }
       }).start();

    }private Handler handler = new Handler(){  
          public void handleMessage(android.os.Message msg) {  
              // 将WebService得到的结果返回给TextView      
            lView.setAdapter((ListAdapter) msg.obj); 
      
          }; 
      };  }这样写也是线程问题啊,2.2可以4.0就不可以..我要疯了,刚接触安卓2周而已,求大神指点怎么搞这个4.0访问线程问题啊
      

  4.   

     HttpTransportSE transport = new HttpTransportSE(endPoint);要使用多线程,4.0后会阻塞主线程的进程都要开线程执行
    new Thread() {
    public void run() {
    }
    }.start();