关于启用线程获取网络消息然后添加到布局中,不出现是怎么回事啊 3C

不说什么,直接上代码
package textview.exam;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
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.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.baidu.apistore.sdk.ApiCallBack;
import com.baidu.apistore.sdk.ApiStoreSDK;
import com.baidu.apistore.sdk.network.Parameters;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FragmentMainchatnews extends Fragment {
private Button button;
private Bitmap imageView;
@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);
    return view;
}
//创建Hander对象,用于接收子线程的
Handler handler = new Handler() {
    public void handleMessage(android.os.Message msg) {
        if (msg.arg1 == 1) {
            List listitem=(List) msg.obj;
            // 创建一个SimpleAdapter实现界面的更新
            SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity(), listitem, R.layout.news_simple_item,
                    new String[]{"image", "title", "abs"},
                    new int[]{R.id.image, R.id.title, R.id.abs});
            ListView list = (ListView) getView().findViewById(R.id.chat_news);
            // 为ListView设置Adapter
            list.setAdapter(simpleAdapter);
            //imageView.setImageBitmap(bitmap);
        }
    }

    ;
};
//开启子线程得到数据
Runnable runnable;

{
    runnable = new Runnable() {
        public void run() {
            final Message message = handler.obtainMessage();
            Parameters para = new Parameters();

            //para.put("keyword", "娱乐");
            ApiStoreSDK.execute("http://apis.baidu.com/songshuxiansheng/news/news",
                    ApiStoreSDK.GET,
                    para,
                    new ApiCallBack() {

                        @Override
                        public void onSuccess(int status, String responseString) {
                            Log.i("连接状态:", "连接成功");


                            try {
                                JSONObject dataJson = new JSONObject(responseString);
                                JSONArray data = dataJson.getJSONArray("retData");
                                // 创建一个List集合,List集合的元素是Map
                                List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
                                for (int i = 0; i < 5; i++) {
                                    JSONObject info = data.getJSONObject(i);
                                    Map<String, Object> listItem = new HashMap<String, Object>();
                                    //imageView = getHttpBitmap(info.getString("image_url"));
                                    listItem.put("title", info.getString("title"));
                                    //listItem.put("image",imageView);
                                    listItem.put("abs", info.getString("abstract"));
                                    listItems.add(listItem);
                                    message.obj = listItems;
                                    //定义标识子线程
                                    message.arg1 = 1;
                                    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()));
                        }

                    });
        }
    };
}
//获取网络图片
private Bitmap getHttpBitmap(String urlString) {
    URL url;
    Bitmap bitmap = null;

    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) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return bitmap;
}

}

3个回答

for (int i = 0; i < 5; i++) {
        JSONObject info = data.getJSONObject(i);
        Map<String, Object> listItem = new HashMap<String, Object>();
        //imageView = getHttpBitmap(info.getString("image_url"));
        listItem.put("title", info.getString("title"));
        //listItem.put("image",imageView);
        listItem.put("abs", info.getString("abstract"));
        listItems.add(listItem);
        message.obj = listItems;
        //定义标识子线程
        message.arg1 = 1;
        handler.sendMessage(message);
}

你先让for循环遍历完之后,再发送handler。

循环发送handler

报了什么错误没,log贴出来看下吧

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!