当前位置:网站首页 > 更多 > 编程开发 > 正文

[安卓] 安卓在子线程传值给主线程,通过Handler传值

作者:CC下载站 日期:2020-09-26 00:00:00 浏览:62 分类:编程开发

昨晚上在进行http请求获取数据并修改listview的时候遇到了一个问题Only the original thread that created a view hierarchy can touch its views

大概意思就是:只有创建了视图层级的原始线程才可以修改这个视图

于是我百度了一下,然后大概解决方案是这样子的,子线程通过handler传值给主线程,主线程接收后,再进行修改listview。

Handlerhandler=newHandler(){
@Override
publicvoidhandleMessage(Messagemsg){
super.handleMessage(msg);
if(msg.what==1){
Bundledata=msg.getData();
Stringval=data.getString("value");
//设置UI
tvCode.setText(val);
Log.i(TAG,"请求结果:"+val);
}elseif(msg.what==0){
Toast.makeText(getApplicationContext(),"请求资源不成功",Toast.LENGTH_LONG).show();
}
}
};

/**
*处理网络请求的线程
*/
privateclassRequestThreadextendsThread{
@Override
publicvoidrun(){

//网络请求
Stringstring=请求结果
Messagemsg=newMessage();
Bundledata=newBundle();

//将获取到的String装载到msg中
data.putString("value",string);
msg.setData(data);
msg.what=1;
//发消息到主线程
handler.sendMessage(msg);
}
}

//点击事件启动新线程
publicvoidtest(Viewv){
newRequestThread().start();
}

解决方法原链接:https://blog.csdn.net/LJX_ahut/article/details/89432576

经过实践着实有用。

获取前:

获取后:

MainActivity.java代码

packageink.cik.logininfoapp;

importandroidx.appcompat.app.ActionBar;
importandroidx.appcompat.app.AppCompatActivity;

importandroid.annotation.SuppressLint;
importandroid.net.wifi.aware.DiscoverySession;
importandroid.os.Bundle;
importandroid.os.Handler;
importandroid.os.Message;
importandroid.util.Log;
importandroid.view.Menu;
importandroid.view.View;
importandroid.widget.Adapter;
importandroid.widget.ArrayAdapter;
importandroid.widget.Button;
importandroid.widget.ListView;
importandroid.widget.Toast;

importcom.google.gson.Gson;
importcom.google.gson.reflect.TypeToken;

importjava.util.ArrayList;
importjava.util.List;

importink.cik.logininfoapp.eneity.userInfo;
importink.cik.logininfoapp.help.httpHelper;
importokhttp3.OkHttpClient;
importokhttp3.Request;
importokhttp3.Response;

publicclassMainActivityextendsAppCompatActivity{
privatefinalGsongson=newGson();
@SuppressLint("HandlerLeak")
Handlerhandler=newHandler(){
@Override
publicvoidhandleMessage(Messagemsg){
super.handleMessage(msg);
if(msg.what==1){
Bundledata=msg.getData();
Stringval=data.getString("value");
List<userInfo>userInfoList=parseJson(val);
ListViewlistView=(ListView)findViewById(R.id.listInfo);
ArrayList<String>list=newArrayList<String>();//数据源集合创建
for(userInfouserInfo:userInfoList){
list.add(userInfo.getUserName());
}
ArrayAdapter<String>adapter=newArrayAdapter<String>(
MainActivity.this,android.R.layout.simple_list_item_1,list
);
listView.setAdapter(adapter);
}elseif(msg.what==0){
Toast.makeText(MainActivity.this,"数据获取失败,请检查网络!",Toast.LENGTH_SHORT);
}
}
};

@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Buttonbutton=(Button)findViewById(R.id.searchButton);
button.setOnClickListener(newView.OnClickListener(){
@Override
publicvoidonClick(Viewv){
getAll();
}
});


}

@Override
publicbooleanonCreateOptionsMenu(Menumenu){
getMenuInflater().inflate(R.menu.menu,menu);
returntrue;
}

privatevoidgetAll(){
finalhttpHelperhttpHelper=newhttpHelper();
newThread(newRunnable(){
@Override
publicvoidrun(){
try{
Stringres=httpHelper.httpGet("https://api.565.ink/login/getAll?passwd=lanol666");
Log.d("结果:",res);
Messagemsg=newMessage();
Bundledata=newBundle();
//将获取到的String装载到msg中
data.putString("value",res);
msg.setData(data);
msg.what=1;
handler.sendMessage(msg);
}catch(Exceptione){
e.printStackTrace();
}
}
}).start();
}

privateList<userInfo>parseJson(StringJsonData){
Gsongson=newGson();
List<userInfo>userInfoList=gson.fromJson(JsonData,newTypeToken<List<userInfo>>(){
}.getType());
returnuserInfoList;
}
}


您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯