我有一个主要的活动应该调用Asynctask从远程URL获取Json数据后我得到这些数据我解析它们并将它们放到listview中如下:
Top20Activity.java
package com.example.example;
import com.example.example.neededClasses.FetchTop20FromDB;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
public class Top20Activity extends FragmentActivity {
//private TextView role;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_top20);
//role = (TextView)findViewById(R.id.textView7);
new FetchTop20FromDB(this).execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.top20, menu);
return true;
}
}
FetchTop20FromDB.java
package com.example.example.neededClasses;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import com.example.example.R;
import android.content.Context;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class FetchTop20FromDB extends AsyncTask<String, Void, ArrayList<String>> {
//private TextView roleField;
private Context context;
private ListView top20FinalMenu ;
ArrayAdapter<String> listAdapter ;
ArrayList<String> top20listItems = new ArrayList<String>();
public FetchTop20FromDB(Context context) {
this.context = context;
//this.roleField = roleField;
}
@Override
protected ArrayList<String> doInBackground(String... urls) {
try{
String link = "http://example.com/android_data.php";
//URL url = new URL(link);
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(link));
HttpResponse response = client.execute(request);
BufferedReader in = new BufferedReader
(new InputStreamReader(response.getEntity().getContent()));
// StringBuffer sb = new StringBuffer("");
String line="";
while ((line = in.readLine()) != null) {
JSONArray ja = new JSONArray(line);
for (int i = 0; i < ja.length(); i++) {
JSONObject jo = (JSONObject) ja.get(i);
top20listItems.add(jo.getString("localteam_name"));
}
//sb.append(line);
break;
}
in.close();
}catch(Exception e){
// return new String("Exception: " + e.getMessage());
}
return top20listItems;
}
@Override
protected void onPostExecute(ArrayList<String> result) {
// Create ArrayAdapter using the menu list.
listAdapter = new ArrayAdapter<String>(context, R.layout.top20_menu_item, result);
LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = li.inflate(R.layout.activity_top20, null,true);
// Find the ListView resource.
top20FinalMenu = (ListView) v.findViewById( R.id.top20Menu );
// Set the ArrayAdapter as the ListView's adapter.
top20FinalMenu.setAdapter( listAdapter );
}
}
Activity_top20.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/top20Menu">
</ListView>
</RelativeLayout>
Top20_menu_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/top20_teams"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp" />
</LinearLayout>
列表永远不会显示我确定数据已被购买,但为什么没有显示我没有任何线索我希望对此有所帮助?
答案 0 :(得分:1)
我认为问题在于你onPostExecute()
方法:
LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = li.inflate(R.layout.activity_top20, null,true);
您正在创建包含您正在填充的ListView的View
的新实例。但是,这不是Top20Activity
显示的视图。
您必须访问并填充ListView
显示的原始Activity
才能查看数据。
答案 1 :(得分:0)
由于Asyntask
在单独的线程上运行,因此您无法从那里访问UI和UI元素。但是您可以使用onPreExecute, onPostExecute, onProgressUpdate
的{{1}}方法,因为它们在UI线程上运行。如果您想在asyntask中找到listview,请使用Asyntask
或onPreExecute
。
将onPostExecute
更改为
onPostExecute
答案 2 :(得分:0)
您可以从asyncTask访问UI元素。但你必须在正确的功能中调用它们。
可以在onPostExecute()
答案 3 :(得分:0)
我的考虑,可能会帮助你。
你使用ArrayAdapter类,它是android系统类,所以你需要
android.R.layout.simple_list_item_1
现在使用
的布局R.layout.top20_menu_item
等
new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1,android.R.id.text1,yourarraylist)
或强>
如果你想使用这个文件
R.layout.top20_menu_item
所以请创建自己的自定义ArrayAdapter。