首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >新线程和SimpleAdapter

新线程和SimpleAdapter
EN

Stack Overflow用户
提问于 2014-04-22 10:51:48
回答 1查看 200关注 0票数 0

当我启动系统发送我的:

代码语言:javascript
复制
public class Lista extends ListActivity 
{
        HttpPost httppost;
        StringBuffer buffer;
        HttpResponse response;
        HttpClient httpclient;
        List<NameValuePair> nameValuePairs; 
        private static final String PREFERENCES_NAME = "myPreferences";
        private static final String PREFERENCES_TEXT_FIELDK = "key";

TextView result;
String text;

// XML node keys
static final String KEY_ITEM = "quote"; // parent node
static final String KEY_ID = "id";
static final String KEY_NAME = "name";
static final String KEY_COST = "date";
static final String KEY_DESC = "place";
static final String KEY_POLEC = "action";
private SharedPreferences preferences;
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.lista);
    preferences = getSharedPreferences(PREFERENCES_NAME, Activity.MODE_PRIVATE);
    result = (TextView)findViewById(R.id.info);
    try {checksession();}catch(JSONException e){/* TODO Auto-generated catch block */ e.printStackTrace();}
}
public void checksession() throws JSONException
{ 
    // Create a new HttpClient and Post Header
    Thread thread = new Thread()
    {
        @Override
        public void run() 
        {
            try{
                httpclient=new DefaultHttpClient();
                httppost= new HttpPost(xxx); // make sure the url is correct.
                ......................
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                //Execute HTTP Post Request
                response=httpclient.execute(httppost);
                ResponseHandler<String> responseHandler = new BasicResponseHandler();
                final String response = httpclient.execute(httppost, responseHandler);
                System.out.println("Response : " + response);
                runOnUiThread(new Runnable() 
                {
                public void run() 
                {
                    // p.dismiss();
                if (response != null && response.length()==6)
                {

                    //  result.setText("In System");
                    takeXML(); // takeXML XML
                }else{ result.setText("No login:"+response);}
                }
                });
            }catch(Exception e){
                //runOnUiThread(new Runnable() {public void run() {p.dismiss();}});
                result.setText("Error system");
                System.out.println("Exception : " + e.getMessage());
            }
        }
    };
    thread.start(); 
}

在这个位置代码是可以的。现在是代码函数takeXML:

代码语言:javascript
复制
    private void takeXML()
{
    new Thread(new Runnable() {
    public void run() 
    {
        try{ 
            ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
            String adresxml="http://xxx.xml";
            XMLParser parser = new XMLParser(); 
            String xml = parser.getXmlFromUrl(adresxml); // getting XML
            Document doc = parser.getDomElement(xml); // getting DOM element
            NodeList nl = doc.getElementsByTagName(KEY_ITEM);

            for (int i = 0; i < nl.getLength(); i++) 
            {
                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();
                Element e = (Element) nl.item(i);
                // adding each child node to HashMap key => value
                map.put(KEY_ID, parser.getValue(e, KEY_ID));
                map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
                map.put(KEY_COST, parser.getValue(e, KEY_COST));
                // adding HashList to ArrayList
                // excel[i] = new ExcelFileClass() 

                menuItems.add(map);
            }
            Log.v("List","Array:"+menuItems);
            // Adding menuItems to ListView
            ListAdapter adapter = new SimpleAdapter(getBaseContext(), menuItems,R.layout.list_item,new String[] { KEY_NAME, KEY_COST, KEY_ID }, new int[] {R.id.name,R.id.cost, R.id.elements });

问题就在这里,在这个地方,系统发送给我:只有创建视图层次结构的原始线程才能接触到它的视图。“我试着,我在研究如何解决这个问题。但是,我不知道这个。

代码语言:javascript
复制
            setListAdapter(adapter);
            ListView lv = getListView();
            lv.setOnItemClickListener(new OnItemClickListener()
            {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,int position, long id) 
                {
                    ......  
                }
            });
        }catch(Exception e){
            //runOnUiThread(new Runnable() {public void run() {p.dismiss();}});
            result.setText("Error");
            System.out.println("Exception : " + e.getMessage());
        }
    }
}).start();
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-22 13:49:02

您需要调用代码的各个部分,这些部分在活动的主线程中修改UI。

要做到这一点,请使用runOnUiThread调用setListAdapter(adapter)

代码语言:javascript
复制
runOnUiThread(new Runnable() {
   @Override
   public void run() {
        setListAdapter(adapter)
   }
});

result.setText("Error");也做同样的事情。

编辑:

代码语言:javascript
复制
private void takeXML()
{
new Thread(new Runnable() {
public void run() 
{
    try{ 
        ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
        String adresxml="http://xxx.xml";
        XMLParser parser = new XMLParser(); 
        String xml = parser.getXmlFromUrl(adresxml); // getting XML
        Document doc = parser.getDomElement(xml); // getting DOM element
        NodeList nl = doc.getElementsByTagName(KEY_ITEM);

        for (int i = 0; i < nl.getLength(); i++) 
        {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);
            // adding each child node to HashMap key => value
            map.put(KEY_ID, parser.getValue(e, KEY_ID));
            map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
            map.put(KEY_COST, parser.getValue(e, KEY_COST));
            // adding HashList to ArrayList
            // excel[i] = new ExcelFileClass() 

            menuItems.add(map);
        }
        Log.v("List","Array:"+menuItems);
        // Adding menuItems to ListView
        ListAdapter adapter = new SimpleAdapter(getBaseContext(), menuItems,R.layout.list_item,new String[] { KEY_NAME, KEY_COST, KEY_ID }, new int[] {R.id.name,R.id.cost, R.id.elements });
        runOnUiThread(new Runnable() {
          @Override
          public void run() {
            setListAdapter(adapter)
            }
        });
        ListView lv = getListView();
        lv.setOnItemClickListener(new OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,int position, long id) 
            {
                ......  
            }
        });
    }catch(Exception e){
        //runOnUiThread(new Runnable() {public void run() {p.dismiss();}});
        runOnUiThread(new Runnable() {
          @Override
          public void run() {
            result.setText("Error");
            }
        });
        System.out.println("Exception : " + e.getMessage());
    }
  }
 }).start();
}

希望它有帮助:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23217412

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档