下面是 代码 先说一下 小弟搞了好久实在没搞出来 在子线程里面获取到了服务器返回的数据,然后赋值给每个控件.但就是不更新.并且我是重新加载运行了oncreate方法的 还是不更新 必须退出整个应用在进入才看到最新的信息.实在不明白是为什么了 下面是代码 请各位发表下看法 谢谢
public class TestActivity extends Activity { TextView level=null;
TextView qq=null;
TextView mobile=null;
TextView email=null;
TextView autograph=null;
ImageView imageView=null;
TextView profession=null; String account;
Integer userId;
Bitmap bitmap;
String json = null;
String imageUrl;
int leveljson;
Context context = TestActivity.this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_information);
System.out.println("运行了oncreate");
//initCreate();
account = AdminActivity.account;
new Thread(new viewPage()).start();
}
/**
* 初始化控件
* */
public void initCreate(){
level = (TextView) this.findViewById(R.id.level);
qq = (TextView) this.findViewById(R.id.qq);
mobile = (TextView) this.findViewById(R.id.mobile);
email = (TextView) this.findViewById(R.id.email);
autograph = (TextView) this.findViewById(R.id.autograph);
imageView = (ImageView) this.findViewById(R.id.imageView);
profession = (TextView) this.findViewById(R.id.profession);
}
public class viewPage implements Runnable{
public void run(){
Message msg = null;
User user = null;
JSONArray array = null;
JSONObject jsonObject = null;
try{
Map<String,String> map = new HashMap<String,String>();
map.put("username",account);
StringBuilder data = new StringBuilder();
if(null!=map&&null!=map.entrySet()){
for(Map.Entry<String,String> m:map.entrySet()){
data.append(m.getKey()).append("=");
data.append(URLEncoder.encode(m.getValue(), "UTF-8"));
data.append("&");
}
}
data.deleteCharAt( data.length() - 1);
LoginService login = new LoginService();
json = login.checkLogic(data.toString(),UrlParam.getUserInfoUrl);
if(!json.equals("-1")){
array = new JSONArray(json);
user = new User();
for(int i = 0 ; i < array.length() ; i++){
jsonObject = array.getJSONObject(i);
user.setUserId(jsonObject.getInt("userId"));
user.setLevel(jsonObject.getInt("level"));
user.setMobile(jsonObject.getString("mobile"));
user.setQq(jsonObject.getString("qq"));
user.setImageurl(jsonObject.getString("headerimages"));
user.setEmail(jsonObject.getString("email"));
user.setAutograph(jsonObject.getString("autograph"));
user.setProfession(jsonObject.getString("profession"));
}
}
msg = new Message();
if(json.equals("-1")){
msg.what = -1;
}else{
msg.what = 3;
msg.obj = user;
}
handler.sendMessage(msg);
}catch(Exception e){
e.printStackTrace();
}
}
}
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
User data = null;
HttpBitmap bit = null;
data = (User) msg.obj;
userId = data.getUserId();
qq.setText(data.getQq());
mobile.setText(data.getMobile());
email.setText(data.getEmail());
autograph.setText(data.getAutograph());
profession.setText(data.getProfession());
Toast.makeText(context, qq.getText().toString(),3).show();//这一句是输出 修改过的QQ号码 每次运行这里都是最新的数据 但是上面被赋值的控件显示的数据就是之前没有修改的数据.完全搞不明白.开始以为是控件引用的不是同一个造成的 结果我把所有控件在这段代码的上面初始化 结果还是一样.
bit = new HttpBitmap();
if(null!=data.getImageurl()&&!"null".equals(data.getImageurl())){
imageUrl = UrlParam.getUserInfoImage+data.getImageurl();
bitmap = bit.getHttpBitmap(UrlParam.getUserInfoImage+data.getImageurl());
imageView.setImageBitmap(bitmap);
}
}
}
};
如有哪里表达不到位请回帖我会及时回复.
public class TestActivity extends Activity { TextView level=null;
TextView qq=null;
TextView mobile=null;
TextView email=null;
TextView autograph=null;
ImageView imageView=null;
TextView profession=null; String account;
Integer userId;
Bitmap bitmap;
String json = null;
String imageUrl;
int leveljson;
Context context = TestActivity.this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_information);
System.out.println("运行了oncreate");
//initCreate();
account = AdminActivity.account;
new Thread(new viewPage()).start();
}
/**
* 初始化控件
* */
public void initCreate(){
level = (TextView) this.findViewById(R.id.level);
qq = (TextView) this.findViewById(R.id.qq);
mobile = (TextView) this.findViewById(R.id.mobile);
email = (TextView) this.findViewById(R.id.email);
autograph = (TextView) this.findViewById(R.id.autograph);
imageView = (ImageView) this.findViewById(R.id.imageView);
profession = (TextView) this.findViewById(R.id.profession);
}
public class viewPage implements Runnable{
public void run(){
Message msg = null;
User user = null;
JSONArray array = null;
JSONObject jsonObject = null;
try{
Map<String,String> map = new HashMap<String,String>();
map.put("username",account);
StringBuilder data = new StringBuilder();
if(null!=map&&null!=map.entrySet()){
for(Map.Entry<String,String> m:map.entrySet()){
data.append(m.getKey()).append("=");
data.append(URLEncoder.encode(m.getValue(), "UTF-8"));
data.append("&");
}
}
data.deleteCharAt( data.length() - 1);
LoginService login = new LoginService();
json = login.checkLogic(data.toString(),UrlParam.getUserInfoUrl);
if(!json.equals("-1")){
array = new JSONArray(json);
user = new User();
for(int i = 0 ; i < array.length() ; i++){
jsonObject = array.getJSONObject(i);
user.setUserId(jsonObject.getInt("userId"));
user.setLevel(jsonObject.getInt("level"));
user.setMobile(jsonObject.getString("mobile"));
user.setQq(jsonObject.getString("qq"));
user.setImageurl(jsonObject.getString("headerimages"));
user.setEmail(jsonObject.getString("email"));
user.setAutograph(jsonObject.getString("autograph"));
user.setProfession(jsonObject.getString("profession"));
}
}
msg = new Message();
if(json.equals("-1")){
msg.what = -1;
}else{
msg.what = 3;
msg.obj = user;
}
handler.sendMessage(msg);
}catch(Exception e){
e.printStackTrace();
}
}
}
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
User data = null;
HttpBitmap bit = null;
data = (User) msg.obj;
userId = data.getUserId();
qq.setText(data.getQq());
mobile.setText(data.getMobile());
email.setText(data.getEmail());
autograph.setText(data.getAutograph());
profession.setText(data.getProfession());
Toast.makeText(context, qq.getText().toString(),3).show();//这一句是输出 修改过的QQ号码 每次运行这里都是最新的数据 但是上面被赋值的控件显示的数据就是之前没有修改的数据.完全搞不明白.开始以为是控件引用的不是同一个造成的 结果我把所有控件在这段代码的上面初始化 结果还是一样.
bit = new HttpBitmap();
if(null!=data.getImageurl()&&!"null".equals(data.getImageurl())){
imageUrl = UrlParam.getUserInfoImage+data.getImageurl();
bitmap = bit.getHttpBitmap(UrlParam.getUserInfoImage+data.getImageurl());
imageView.setImageBitmap(bitmap);
}
}
}
};
如有哪里表达不到位请回帖我会及时回复.
可以使用AsyncTask类 异步加载来刷新当前界面
请确保handler是在主线程中