不。 runOnUiThread() 是必要的,因为你想调用setListAdapter
.
再次。 -
移动这两行
ListAdapter adapter = new SimpleAdapter(Utenti.this, mylist , R.layout.utentii,new String[] { "name" },new int[] { 1});
setListAdapter(adapter);
就在之后for(){}
循环中onClick()
method.
如果您确定您的mylist
列表中填充有 json 调用中的数据。
基本上是这样的样本:
将 xml 文件内容替换为以下内容:
测试.xml
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
对于 ListView 的行
row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView" android:layout_gravity="center"/>
</LinearLayout>
然后是 onCreate():
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
ListView lv = getListView();
populateListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
populateListView();
}
});
}
private void populateListView(){
Thread t = new Thread(new Runnable (){
public void run() {
JSONObject json = getUserObject("http://myhost:8080/login/email");
Log.i("json",json.toString());
try {
//Get the element that holds the earthquakes ( JSONArray )
JSONArray friends = json.getJSONObject("user").getJSONArray("friends");
final List<Map<String, String>> mylist =
new ArrayList<Map<String, String>>();
//get list of friend ids in the user object
for(int i=0;i < friends.length();i++){
Map<String,String> map = new LinkedHashMap<String, String>();
map.put("friendid", friends.getString(i));
mylist.add(map);
}
runOnUiThread(new Runnable() {
public void run() {
ListAdapter adapter = new SimpleAdapter(MyTestListActivity.this, mylist , R.layout.row,new String[] { "friendid" },new int[] {R.id.textView});
setListAdapter(adapter);
}
});
} catch (JSONException e) {
System.out.println("4");
e.printStackTrace();
}
}
});
t.start();
}
public static JSONObject getUserObject(String page){
JSONObject jArray = null;
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(page);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("user", "test"));
nameValuePairs.add(new BasicNameValuePair("pwd", "test"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
String str = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
try{
jArray=new JSONObject (str);}
catch(JSONException e){Log.d("error",e);}
}
catch(Exception e)
{
Log.i("NetworkTest", "Network Error: " + e);
}
return jArray;
}
希望这个对你有帮助。