Good evening. There is an activity in which an instance of Handler is created to process a message from another thread. Inside handle I can change the fields that are UI elements (TextView, EditText, etc.), but nothing happens with fields of other types. How can I get newhtml in the activity field?

public class AboutUsActivity extends Activity { Handler h; TextView largeText; List<String> stringLinks; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); stringLinks = new ArrayList<String>(); largeText = (TextView) findViewById(R.id.textView1); h = new Handler() { public void handleMessage(android.os.Message msg) { HtmlParser parser; StringBuilder result = new StringBuilder(); try { parser = new HtmlParser(String.valueOf(msg.getData())); List<TagNode> links = parser.getContentByClassName("ab"); for (Iterator<TagNode> iterator = stringLinks.iterator(); iterator .hasNext();) { TagNode divElement = (TagNode) iterator.next(); result.append(divElement.getText().toString()); } } catch (Exception e) { e.printStackTrace(); } largeText.setText(newhtml); // Работает, отображается текст stringLinks.add(newhtml); // Компилируется без ошибок, но в // список ничего не добавляется } }; MyHttpClientUsage connect = new MyHttpClientUsage(h); try { connect.getInfoAbout(); } catch (HttpException e) { e.printStackTrace(); } } 

}

  • if it's so hard, use AsyncTask better - Gorets

2 answers 2

Indeed, use AsyncTask. In it, you highlight the code that should work in the UI thread and in the parallel thread. In the asinkTaska method

 onPostExecute() 

you will announce UI and everything will be fine

    The fact is that the UI stream must "breathe", that is, you must return control to it so that it can display the changes you made to the UI elements. If you think that as soon as you change the contents of TextView it will immediately be displayed - then you have to upset you: your picture of the world is wrong.

    The message is simple: until you return control of the UI to the flow, changes in the UI will not occur.