我有一个asycn任务被调用:
GetListsJSON task = new GetListsJSON(getActivity());
task.setOnArticleSelectedListener(this);
task.execute(url);
然后运行此异步任务以检索JSON并填充listview:
public class GetListsJSON extends AsyncTask<String, Void, String> {
Context c;
private ProgressDialog Dialog;
public GetListsJSON (Context context)
{
c = context;
Dialog = new ProgressDialog(c);
}
//****************code for on click
OnArticleSelectedListener listenerBeer;
public interface OnArticleSelectedListener{
public void onArticleSelected(String myString, String brewery);
}
public void setOnArticleSelectedListener(OnArticleSelectedListener listener){
this.listenerBeer = listener;
}
//******************end code for onClick
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
return readJSONFeed(arg0[0]);
}
protected void onPreExecute() {
Dialog.setMessage("Getting Lists");
Dialog.setTitle("Loading");
Dialog.setCancelable(false);
Dialog.show();
}
protected void onPostExecute(String result){
//decode json here
try{
JSONArray jsonArray = new JSONArray(result);
final List<BeerData> beerList = new ArrayList<BeerData>();
//acces listview
final ListView lv = (ListView) ((Activity) c).findViewById(R.id.allYourBeersList);
Log.d("list", "inside async");
//make array list for beer
//tasteList.add("");
for(int i = 0; i < jsonArray.length(); i++) {
String bID = jsonArray.getJSONObject(i).getString("id");
String listName = jsonArray.getJSONObject(i).getString("name");
Log.d("list", listName);
//create beer object
BeerData thisBeer = new BeerData(listName, bID, "a", "a", "a", "a",
"a","a", "a", "a", "a", "breweryID", "breweryName",
"a", "a", "a","a", "a", "a");
//add beer to list
beerList.add(thisBeer);
}
//update listview
BeerSearchAdapter adapter1 = new BeerSearchAdapter(c ,R.layout.listview_item_row, beerList);
lv.setAdapter(adapter1);
//set up clicks
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
BeerData beerInfo = beerList.get(arg2);
String idToSend = beerInfo.beerId;
//************************Launch listener interface
listenerBeer.onArticleSelected(idToSend, beerInfo.beerBreweryId);
}
});
}
catch(Exception e){
}
Dialog.dismiss();
}
public String readJSONFeed(String URL) {
StringBuilder stringBuilder = new StringBuilder();
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(URL);
try {
HttpResponse response = httpClient.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
inputStream.close();
} else {
Log.d("JSON", "Failed to download file");
}
} catch (Exception e) {
Log.d("readJSONFeed", e.getLocalizedMessage());
}
return stringBuilder.toString();
}
}
我知道JSON解析正在运行,因为我的日志吐了出来:
03-28 16:04:27.175 30167-30167/com.beerportfolio.beerportfoliopro D/list﹕ inside async
03-28 16:04:27.175 30167-30167/com.beerportfolio.beerportfoliopro D/list﹕ My Fridge
03-28 16:04:27.175 30167-30167/com.beerportfolio.beerportfoliopro D/list﹕ Wish List
列表应加载的页面的xml是:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/linlaHeaderProgress"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone" >
<ProgressBar
android:id="@+id/pbHeaderProgress"
style="android@style/Spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ProgressBar>
</LinearLayout>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:dividerHeight="0px"
android:divider="@null"
>
</ListView>
</LinearLayout>
答案 0 :(得分:1)
你有这一行:
final ListView lv = (ListView) ((Activity) c).findViewById(R.id.allYourBeersList);
但是在您的xml文件中,您使用以下ID定义ListView: @android:id / list
您应该将其切换到xml文件中的以下内容:
@+id/allYourBeersList