项目里面的布局是这样的:一个Viewpager,Viewpager里面有三个Fragment,在第二个Fragment里面有一个ListView,使用了BaseAdapter来显示item。当时因为图方便,把获取数据源mData的方法直接写在了adapter的构造方法里面,数据源通过syncHttpClient向后台请求参数获取。
单击提示有错误:
CODE
package com.example.fiz.myapplication;
/**
Created by Fiz on 16/3/21.
和服务器进行通信
*/
import android.annotation.SuppressLint;
import android.app.LauncherActivity;
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.widget.ArrayAdapter;
import android.app.Activity;
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 Home extends Fragment {
private String textString;
private static final String Tag = "MainActivity";
private ArrayList<HashMap<String, Object>> arrayList = new ArrayList<HashMap<String, Object>>();
public Home(String textString) {
this.textString = textString;
}
public static Home newInstance(String textString) {
Home mFragment = new Home(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);
try {
getPublicTimeline();
} catch (JSONException e) {
e.printStackTrace();
}
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);
// adapter.notifyDataSetChanged();///
/**
*事件监听
*/
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
adapter.notifyDataSetChanged();
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;
}
/**
获取服务端数据
*/
-
void getPublicTimeline() throws JSONException {
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) {
// Pull out the first event on the public timeline
//这是我的后台适应编码
// System.out.println(timeline);
try {
// JSONObject firstEvent = (JSONObject) timeline.get(0);
JSONArray secondEvent = (JSONArray) timeline;
// String tweetText = firstEvent.getString("title");
// Do something with the response
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);
}
System.out.println(arrayList);
// System.out.println(tweetText);
} catch (JSONException e) {
//do something
}
}
});
}
/**
getData
*/
-
ArrayList<HashMap<String, Object>> getData() {
return arrayList;
}
/**捕获单击事件
*/
}
LOG
at android.widget.ListView.layoutChildren(ListView.java:1562)
at android.widget.AbsListView$CheckForTap.run(AbsListView.java:3261)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
04-12 00:42:38.120 29022-29022/com.example.fiz.myapplication I/Process: Sending signal. PID: 29022 SIG: 9
经过一天的思考源码之后终于修改好了代码:当用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 {
// System.out.println(tweetText);
getData
*/
ArrayList<HashMap<String, Object>> getData() {
}
}