扫码关注官方订阅号
认证0级讲师
adapter.notifyDataSetChanged();
这个要在数据发生改变之后才调用
错误原因终于找到:当用AsyncHttpClient时,程序会自动新开一个线程,数据获取在一个线程,而adapter中的getCount又在另一个线程,两者的进度是无法把握的。结果很明显了现将纠正后的代码奉献出package com.example.fiz.myapplication;
/**
Created by Fiz on 16/3/21.
和服务器进行通信*/
import android.annotation.SuppressLint;import android.os.Bundle;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.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ListView;import android.os.Handler;import android.widget.SimpleAdapter;import com.example.fiz.myapplogin.DjangoRestClient;import com.loopj.android.http.JsonHttpResponseHandler;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import java.util.ArrayList;import java.util.HashMap;
import cz.msebera.android.httpclient.Header;
@SuppressLint("ValidFragment")public class Home2 extends Fragment {
private String textString; private Handler mHandler; private static final String Tag = "MainActivity"; private ArrayList<HashMap<String, Object>> arrayList = new ArrayList<HashMap<String, Object>>();
public Home2(String textString) { this.textString = textString; } public static Home2 newInstance(String textString) { Home2 mFragment = new Home2(textString); return mFragment; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.home_page, container, false); final ListView listView = (ListView) view.findViewById(R.id.listView); DjangoRestClient.get("/demo/test/?format=json", null, new JsonHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, JSONObject response) { // If the response is JSONObject instead of expected JSONArray System.out.println(response); try { JSONArray array = response.getJSONArray("data"); System.out.print("状态码" + statusCode); } catch (Exception e) { e.printStackTrace(); } } @Override public void onSuccess(int statusCode, Header[] headers, JSONArray timeline) { try { JSONArray secondEvent = (JSONArray) timeline; for (int j = 0; j < secondEvent.length(); j++) { JSONObject firstEvent = (JSONObject) timeline.get(j); HashMap<String, Object> tempHashMap = new HashMap<String, Object>(); //新闻标题 String title = firstEvent.getString("title"); //新闻时间 String description = firstEvent.getString("description"); //新闻来源 String link = firstEvent.getString("link"); //新闻序号 String linkmd5id = firstEvent.getString("linkmd5id"); tempHashMap.put("news_title", title); tempHashMap.put("news_from", link); tempHashMap.put("news_judge", linkmd5id); tempHashMap.put("news_time", description); arrayList.add(tempHashMap); } final SimpleAdapter adapter = new SimpleAdapter(getActivity(), getData(), R.layout.home_page_text, new String[]{"news_title", "news_from", "news_judge","news_time"}, new int[]{R.id.news_title, R.id.news_from, R.id.news_judge,R.id.news_time}); listView.setAdapter(adapter); System.out.println(arrayList); } catch (JSONException e) { e.printStackTrace(); } } }); /** *事件监听 */ listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (!arrayList.get(position).equals(null)) { Log.d(Tag, "THIS IS A JOKKE AA UN FUNC M "); } else { Log.d(Tag, "THIS IS A error are you undenstard AA UN FUNC M "); } } });
return view; }
getData*/
ArrayList<HashMap<String, Object>> getData() {
return arrayList;
}
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
这个要在数据发生改变之后才调用
错误原因终于找到:当用AsyncHttpClient时,程序会自动新开一个线程,数据获取在一个线程,而adapter中的getCount又在另一个线程,两者的进度是无法把握的。结果很明显了
现将纠正后的代码奉献出
package com.example.fiz.myapplication;
/**
Created by Fiz on 16/3/21.
和服务器进行通信
*/
import android.annotation.SuppressLint;
import android.os.Bundle;
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.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.os.Handler;
import android.widget.SimpleAdapter;
import com.example.fiz.myapplogin.DjangoRestClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import cz.msebera.android.httpclient.Header;
@SuppressLint("ValidFragment")
public class Home2 extends Fragment {
getData
*/
ArrayList<HashMap<String, Object>> getData() {
}
}