I’m getting data as string from the Google App Engine so I have to do it in a thread. Next I want to display it in a ListView and I’m trying to do something like this code:
public void ShowContacts (){
new Thread(new Runnable() {
@Override
public void run() {
try
{
final RestClient client = new RestClient("http://xxx.appspot.com/client");
client.AddParam("userName", "user");
client.Execute(RequestMethod.GET);
_listView.post(new Runnable() {
@Override
public void run() {
String s = client.getResponse();
String[] items = s.split(";");
//adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, items);
//_listView.setAdapter(adapter);
}
});
}
catch (Exception e)
{
}
}
}).start();
}
This code is not working for me. Is there any way to get some value from the thread and set it to a global variable or construct an ArrayAdapter directly in the thread or something else?
use AsyncTask for getting data from server and add to ListView as:
start this AsyncTask from your Ui thread as: