第一个选项:
使用runOnUiThread从非 Ui 线程更新 UI。将您的代码更改为:
new Thread(new Runnable(){
public void run()
{
ap=(ArrayList<Application>) getBoughtApps(android_id);
Current_Activity.this.runOnUiThread(new Runnable() {
public void run() {
adapter1 = new MyCustomAdapter(ap);
listView = ( ListView ) MainActivity.this.findViewById(R.id.listview);
changeAdapter();
//Your code here..
}
});
}
}).start();
第二种选择:
您可以使用AsyncTask而不是线程来进行网络操作,或者如果应用程序需要从后台更新 Ui。AsyncTask
您可以使用as更改当前代码:
private class CallwebTask extends AsyncTask<Void, Void, String>
{
protected ArrayList<Application> doInBackground(String... params)
{
ap=(ArrayList<Application>) getBoughtApps(android_id);
return ap; // Return ArrayList<Application>
}
protected onPostExecute(ArrayList<Application> result) {
Log.i("OnPostExecute :: ", String.valueOf(result.size()));
//Put UI related code here
adapter1 = new MyCustomAdapter(result);
listView = ( ListView ) MainActivity.this.findViewById(R.id.listview);
changeAdapter();
}
}
并开始AsyncTask
将这一行放在开始线程的位置:
new CallwebTask().execute();