a179583637
2016-06-27 01:04
采纳率: 35.1%
浏览 1.1k
已采纳

获取网络图片直接显示到布局中,有的显示有的不显示,而且顺序还不对,求大神指点,还有怎么自定义写适配器

直接上我的代码,我用了两个线程,一个用于接收网络数据,一个用于接收图片,然后放到列表中,可是好像计算错误,要不就是不显示,就算显示也是错误的。下面是我的代码
public class FragmentMainchatnews extends Fragment {
private Button button;
private ImageView imageView;
private Bitmap bitmap;
private String urlString;
Handler handler1;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.main_chat_news, null);
Thread thread=new Thread(runnable);
thread.start();
return view;

}
//创建Hander对象,用于接收子线程接收到的数据
Handler handler = new Handler() {
List> listItemx = new ArrayList>();
public void handleMessage(android.os.Message msg) {
if (msg.arg1 == 0X456) {
listItemx= (List>) msg.obj;
// 创建一个SimpleAdapter实现界面的更新
SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity(), listItemx, R.layout.news_simple_item,
new String[]{"image", "title", "abs"},
new int[]{R.id.image, R.id.title, R.id.abs});
simpleAdapter.setViewBinder(new SimpleAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Object data,
String textRepresentation) {
// TODO Auto-generated method stub
if ((view instanceof ImageView) && (data instanceof Bitmap)) {
ImageView i = (ImageView) view;
i.setImageBitmap((Bitmap) data);
Log.i("我是", "二蛋");
return true;

                }
                Log.i("我是", "sadasd");
                return false;

            }
        });
        ListView list = (ListView) getView().findViewById(R.id.chat_news);
        // 为ListView设置Adapter
        list.setAdapter(simpleAdapter);
        //动态更新ListView
        simpleAdapter.notifyDataSetChanged();
    }
}

;

};
//开启子线程得到数据
Runnable runnable= new Runnable() {
public void run() {
final Message message = handler.obtainMessage();
Parameters para = new Parameters();
Log.i("尼玛","1");
para.put("keyword", "娱乐");
para.put("page","3");
ApiStoreSDK.execute("http://apis.baidu.com/songshuxiansheng/real_time/search_news",
ApiStoreSDK.GET,
para,
new ApiCallBack() {
@Override
public void onSuccess(int status, String responseString) {
Log.i("连接状态", "连接成功");

                    try {
                        JSONObject  dataJson=new JSONObject(responseString);
                        JSONObject  retData=dataJson.getJSONObject("retData");
                        JSONArray data=retData.getJSONArray("data");
                        int return_count=retData.getInt("return_count");
                        // 创建一个List集合,List集合的元素是Map
                        List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
                        for (int i = 0; i < return_count; i++) {
                            JSONObject info = data.getJSONObject(i);
                            Map<String, Object> listItem = new HashMap<String, Object>();
                            urlString=info.getString("img_url");
                            Thread thread1=new Thread(runnable1);
                            thread1.start();
                            //创建Hander对象,用于接收子线程的图片
                            handler1 = new Handler() {
                                public void handleMessage(android.os.Message msg) {
                                    if (msg.arg1 == 0X123) {
                                        Log.i("我爱","你");
                                        bitmap=(Bitmap) msg.obj;
                                    }
                                }
                            };
                            listItem.put("title", info.getString("title"));
                            listItem.put("image",bitmap);
                            listItem.put("abs", info.getString("abstract"));
                            listItems.add(listItem);

                        }
                        message.obj = listItems;
                        //定义标识子线程
                        message.arg1 = 0X456;
                        handler.sendMessage(message);
                    } catch (JSONException e) {
                        Log.i("获取到数据:", responseString);
                    }
                }

                @Override
                public void onComplete() {
                    Log.i("sdkdemo", "onComplete");
                }

                @Override
                public void onError(int status, String responseString, Exception e) {
                    Log.i("sdkdemo", "onError, status: " + status);
                    Log.i("sdkdemo", "errMsg: " + (e == null ? "" : e.getMessage()));
                }

            });

}

};
//开启另子线程得到图片
Runnable runnable1= new Runnable() {
public void run() {
final Message message = handler.obtainMessage();
//获取网络图片
URL url;

    try {
        url = new URL(urlString);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setConnectTimeout(6000);
        connection.setDoInput(true);
        connection.setUseCaches(true);

        InputStream is = connection.getInputStream();
        bitmap = BitmapFactory.decodeStream(is);
        is.close();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    message.obj = bitmap;
    //定义标识子线程
    message.arg1 =0X123;
    handler1.sendMessage(message);
}

};

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • bdmh 2016-06-27 01:07
    已采纳

    你就别自己写了,多线程估计你是控制不了,你用Imageloader吧

    评论
    解决 无用
    打赏 举报
  • 查看更多回答(2条)

相关推荐 更多相似问题