请求网络数据后更新视图
Android不能直接在子线程中更新视图,需要借助 Handler 类来实现视图更新
[代码] [Java]代码
01 |
public void onCreate(Bundle savedInstanceState) { |
02 |
super .onCreate(savedInstanceState); |
03 |
setContentView(R.layout.soft_recommend); |
05 |
listview = (ListView) findViewById(R.id.recommSoftList); |
07 |
if (SoftInfo.recommendSofts == null ){ |
09 |
recommProBar = (ProgressBar)findViewById(R.id.recommProBar4); |
10 |
recommProBar.setIndeterminate( false ); |
11 |
recommProBar.setVisibility(View.VISIBLE); |
13 |
new Thread( new Runnable() { |
16 |
iconTexts = SoftInfo.getRecommImageTexts(Constants.RECOMM_SOFT_URL); |
18 |
SoftRecommend. this .listHandler |
19 |
.sendMessage(SoftRecommend. this .listHandler |
24 |
iconTexts = SoftInfo.getImageTexts(SoftInfo.recommendSofts); |
25 |
ImageTextAdapter adapter = new ImageTextAdapter( this ); |
26 |
adapter.setIconTexts(iconTexts); |
27 |
adapter.setViewId(R.layout.img_txt_view); |
28 |
listview.setAdapter(adapter); |
34 |
Handler listHandler = new Handler(){ |
41 |
* @see android.os.Handler#handleMessage(android.os.Message) |
43 |
public void handleMessage(Message msg){ |
44 |
recommProBar.setVisibility(View.GONE); |
45 |
ImageTextAdapter adapter = new ImageTextAdapter(SoftRecommend. this ); |
46 |
adapter.setIconTexts(iconTexts); |
47 |
adapter.setViewId(R.layout.img_txt_view); |
48 |
listview.setAdapter(adapter); |
49 |
super .handleMessage(msg); |