从log里面可以看出message是确确实实发送了
但是 上面的handler也没办法执行handlemessage的方法
package czl.baoleme.Fragment;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import czl.baoleme.HttpClient.MyHttpClient;
import czl.baoleme.Other.JSONAnalys;
import czl.baoleme.R;
/**
- Created by LeoChen on 2017/5/15. */
public class HomePageFragment extends Fragment {
private static final int JSONSTR_MESSAGE=666;
private ListView list;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_homepage,container,false);
Thread restaurantThread = new Thread(new RestaurantThread());
restaurantThread.start();
//问题是这里 这个handlemessage方法里面并没有执行
//但是message是确确实实发送了
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case JSONSTR_MESSAGE:
List<Map<String,Object>> data = (List<Map<String,Object>>)msg.obj;
getListView(view,data);
break;
default:
break;
}
}
};
return view;
}
public void getListView(View view,List<Map<String,Object>> data){
list = (ListView) view.findViewById(R.id.resraurantListView) ;
SimpleAdapter simpleAdapter = new SimpleAdapter(this.getActivity(), data, R.layout.item_hplistview,
new String[]{"restaurant_list_item_logo",
"restaurant_list_item_name",
"restaurant_list_item_score",
"restaurant_list_item_distribution",
"restaurant_list_item_discount"},
new int[]{R.id.restaurant_list_item_logo,
R.id.restaurant_list_item_name,
R.id.restaurant_list_item_score,
R.id.restaurant_list_item_distribution,
R.id.restaurant_list_item_discount}
);
list.setAdapter(simpleAdapter);
setListViewHeightBasedOnChildren(list);
}
//这个方法是从解析json得到的列表里取出图片url然后请求图片并放入新的列表
public List> getData(String jsonStr){
List> list = JSONAnalys.getJSON(jsonStr);
List> dataList = new ArrayList<>();
for(Map item:list){
Map map = new HashMap<>();
String url=item.get("reslogo");
Log.d("url",url);
Bitmap pic = getImage(url);
map.put("restaurant_list_item_logo",pic);
map.put("restaurant_list_item_name",item.get("name"));
map.put("restaurant_list_item_score","33333");
map.put("restaurant_list_item_distribution",item.get("jianjie"));
map.put("restaurant_list_item_discount","55555");
dataList.add(map);
}
return dataList;
}
//请求图片方法
public Bitmap getImage(String url){
HttpGet get = new HttpGet(url);
HttpClient client= new DefaultHttpClient();
Bitmap pic = null;
try{
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
pic = BitmapFactory.decodeStream(is);
}catch (Exception e){
e.printStackTrace();
}
return pic;
}
//执行网络请求 获取json 最终生成List 并发送List对象
class RestaurantThread implements Runnable{
Handler handler = new Handler();
@Override
public void run() {
MyHttpClient httpClient = new MyHttpClient();
String jsonStr = httpClient.getRestaurant();
List> list = getData(jsonStr);
try{
Message message = new Message();
message.what=JSONSTR_MESSAGE;
message.obj = list;
if(handler.sendMessage(message)){
Log.d("发送列表","成功");
}
}catch(Exception e){
e.printStackTrace();
}
}
}
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
/**
* listItem.measure(0,0) will throw a NPE if listItem is a ViewGroup
* instance
*/
if (listItem instanceof ViewGroup) {
listItem.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
LayoutParams params = listView.getLayoutParams();
// listView.getDividerHeight()获取子项间分隔符占用的高度
// params.height最后得到整个ListView完整显示需要的高度
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
}