我有一个Listview,它从异步任务中获取其内容,但只有第一个项目实际上被放入Listview。
活动的我的xml是:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:orientation="vertical"
android:background="@drawable/bg_card">
<!-- Card Contents go here -->
<TextView
android:id="@+id/portfolioTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="20sp"
android:textStyle = "bold"
android:text="Your Portfolio"
android:padding="5dip"
>
</TextView>
</LinearLayout >
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:orientation="vertical"
android:background="@drawable/bg_card">
<!-- Card Contents go here -->
<TextView
android:id="@+id/sortTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="15sp"
android:textStyle = "bold"
android:text="Sorting Options:"
android:padding="5dip"
>
</TextView>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal"
>
<Spinner
android:id="@+id/portfolioSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/portfolio_array"
/>
</LinearLayout>
</LinearLayout >
</FrameLayout>
<ListView
android:id="@+id/allYourBeersList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:dividerHeight="0px"
android:divider="@null"
>
</ListView>
</LinearLayout>
</ScrollView>
异步任务是:
public class PortfolioGetAllBeers extends AsyncTask
<String, Void, String> {
Context c;
private ProgressDialog Dialog;
public PortfolioGetAllBeers (Context context)
{
c = context;
Dialog = new ProgressDialog(c);
}
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
return readJSONFeed(arg0[0]);
}
protected void onPreExecute() {
Dialog.setMessage("Getting beers");
Dialog.setTitle("Loading");
Dialog.setCancelable(false);
Dialog.show();
}
protected void onPostExecute(String result){
//decode json here
try{
JSONArray jsonArray = new JSONArray(result);
//acces listview
ListView lv = (ListView) ((Activity) c).findViewById(R.id.allYourBeersList);
//make array list for beer
final List<ShortBeerInfo> tasteList = new ArrayList<ShortBeerInfo>();
for(int i = 0; i < jsonArray.length(); i++) {
String beer = jsonArray.getJSONObject(i).getString("beer");
String rate = jsonArray.getJSONObject(i).getString("rate");
String beerID = jsonArray.getJSONObject(i).getString("id");
String breweryID = jsonArray.getJSONObject(i).getString("breweryID");
int count = i + 1;
beer = count + ". " + beer;
//create object
ShortBeerInfo tempTaste = new ShortBeerInfo(beer, rate, beerID , breweryID);
//add to arraylist
tasteList.add(tempTaste);
//add items to listview
ShortBeerInfoAdapter adapter1 = new ShortBeerInfoAdapter(c ,R.layout.brewer_stats_listview, tasteList);
lv.setAdapter(adapter1);
//set up clicks
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
ShortBeerInfo o=(ShortBeerInfo)arg0.getItemAtPosition(arg2);
String tempID = o.id;
String tempBrewID = o.brewery;
Toast toast = Toast.makeText(c, tempID, Toast.LENGTH_SHORT);
toast.show();
//get beer details from id
Intent myIntent = new Intent(c, BeerPage2.class);
myIntent.putExtra("id", tempID);
myIntent.putExtra("breweryID", tempBrewID);
c.startActivity(myIntent);
}
});
}
}
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回来时有一个以上的结果:
[{"beer":"#9","rate":"3","id":"hB0QeO","breweryID":"qIqpZc"},{"beer":"120 Minute IPA","rate":"5","id":"EWlB8A","breweryID":"g0jHqt"},{"beer":"2011 Pere Jacques","rate":"4","id":"7EhKvb","breweryID":"APW1BC"},{"beer":"2xIPA","rate":"3","id":"kZsjqY","breweryID":"x8kqVp"},{"beer":"312 Urban Wheat Ale","rate":"3","id":"EaoR0H","breweryID":"APW1BC"},{"beer":"471 Double IPA Small Batch","rate":"4","id":"j9cp5e","breweryID":"IImUD9"},{"beer":"5 Golden Rings","rate":"2","id":"tRFIN3","breweryID":"4MVtcc"},{"beer":"60 Minute IPA","rate":"3","id":"TACnR2","breweryID":"g0jHqt"},{"beer":"75 Minute IPA","rate":"5","id":"ovCoQh","breweryID":"g0jHqt"},{"beer":"90 Minute IPA","rate":"5","id":"qqTzHb","breweryID":"g0jHqt"},{"beer":"\u00a7ucaba (Abacus)","rate":"5","id":"GWY6vH","breweryID":"qghrkC"},{"beer":"Admiral IPA","rate":"3","id":"nHYjdl","breweryID":"OYQA8m"},{"beer":"Agave Wheat","rate":"3","id":"6AcqY6","breweryID":"IImUD9"},{"beer":"Alimony Ale","rate":"0","id":"G7kmvt","breweryID":"P1I1gt"},{"beer":"Allagash Black","rate":"4","id":"z77hjQ","breweryID":"pdLPeS"},{"beer":"Allagash Dubbel","rate":"3","id":"KzHweV","breweryID":"pdLPeS"},{"beer":"Allagash Fluxus 2013","rate":"3","id":"SlMu4Q","breweryID":"pdLPeS"},{"beer":"Allagash Four","rate":"4","id":"0mttNd","breweryID":"pdLPeS"},{"beer":"Allagash Victoria Ale","rate":"4","id":"JLBMnT","breweryID":"pdLPeS"},{"beer":"Allagash White","rate":"4","id":"Q8hjek","breweryID":"pdLPeS"},{"beer":"American Amber Ale","rate":"3","id":"3SvZ4H","breweryID":"X0l98q"},{"beer":"American Pilsner","rate":"3","id":"67Hcxk","breweryID":"xcKkLh"},{"beer":"Anchor Steam","rate":"3","id":"Uiol9p","breweryID":"6PBXvz"},{"beer":"Angels Share","rate":"5","id":"ikoDPe","breweryID":"9x7wNn"},{"beer":"Aprihop","rate":"4","id":"qV0bBx","breweryID":"g0jHqt"},{"beer":"Arrogant Bastard Ale","rate":"3","id":"qlwwem","breweryID":"709vEK"},{"beer":"Aventinus Weizen-Eisbock","rate":"4","id":"avMkil","breweryID":"FQLVgV"},{"beer":"B.O.R.I.S. The Crusher Oatmeal-Imperial Stout","rate":"5","id":"Tzy7eJ","breweryID":"w5OOQ5"},{"beer":"Back in Black","rate":"3","id":"UD5Sm4","breweryID":"EdRcIs"},{"beer":"Balt Altbier","rate":"3","id":"dR7EQg","breweryID":"t6Gyij"},{"beer":"Baltic Thunder","rate":"3","id":"DCwAKJ","breweryID":"VoKbnS"},{"beer":"Bass Pale Ale","rate":"3","id":"r5GwCX","breweryID":"X2Qkw1"},{"beer":"Belgian Abbey Dubbel","rate":"3","id":"YkoqCD","breweryID":"M5zcKb"},{"beer":"Big DIPA","rate":"4","id":"aP9faI","breweryID":"wTFQaf"},{"beer":"Black & Blue","rate":"3","id":"tL3vdf","breweryID":"g0jHqt"},{"beer":"Black Cannon","rate":"3","id":"f9WbNU","breweryID":"wTFQaf"},{"beer":"Blackwing Lager","rate":"3","id":"ISXK3w","breweryID":"t6Gyij"},{"beer":"Blue Moon Belgian White","rate":"3","id":"dDXOEp","breweryID":"avMkil"},{"beer":"Br\u00e4u Weisse","rate":"4","id":"L6f8QM","breweryID":"QKdFk2"},{"beer":"Brawler","rate":"2","id":"czwGMM","breweryID":"jwWiTH"},{"beer":"Breakfast Stout","rate":"4","id":"9na4NR","breweryID":"Idm5Y5"},{"beer":"Bronx Pale Ale","rate":"4","id":"yDHpxQ","breweryID":"V0wvf7"},{"beer":"Bud Light","rate":"2","id":"dimtrs","breweryID":"BznahA"},{"beer":"Burton Baton","rate":"4","id":"em9Lxc","breweryID":"g0jHqt"},{"beer":"Caldera IPA","rate":"3","id":"kMlOin","breweryID":"iL3Juq"},{"beer":"Centennial IPA","rate":"3","id":"rAHla4","breweryID":"Idm5Y5"},{"beer":"Cerveza Sol","rate":"2","id":"4ap3mq","breweryID":"UWBCmm"},{"beer":"Chateau Jiahu","rate":"4","id":"bFOmYH","breweryID":"g0jHqt"},{"beer":"Chocolate Stout","rate":"4","id":"93XbpS","breweryID":"X0l98q"},{"beer":"Circus Boy","rate":"3","id":"JQCCB0","breweryID":"qIqpZc"},{"beer":"Corona Extra","rate":"3","id":"ujPz4L","breweryID":"wadu38"},{"beer":"Corona Light","rate":"3","id":"u7U2Ga","breweryID":"wadu38"},{"beer":"Cr\u00e8me Br\u00fbl\u00e9e Imperial Milk Stout","rate":"4","id":"Kax7jD","breweryID":"x8kqVp"},{"beer":"Cricket Hill Hopnotic","rate":"1","id":"FCGb3K","breweryID":"U0vh9j"},{"beer":"Curieux","rate":"4","id":"T6rZqg","breweryID":"pdLPeS"},{"beer":"Cutlass","rate":"3","id":"1KVlP0","breweryID":"wTFQaf"},{"beer":"Dales Pale Ale","rate":"4","id":"iT9pf4","breweryID":"q6vJUK"},{"beer":"Delirium Tremens","rate":"3","id":"siPwY9","breweryID":"8eyXN7"},{"beer":"Dogtoberfest","rate":"3","id":"IgdWNj","breweryID":"jmGoBA"},{"beer":"Don De Dieu","rate":"3","id":"i6yPHf","breweryID":"llbEuB"},{"beer":"Dos Equis Amber Lager","rate":"2","id":"s5lxSd","breweryID":"UWBCmm"},{"beer":"Double Dog Double Pale Ale","rate":"3","id":"r9nypM","breweryID":"jmGoBA"},{"beer":"Double Jack Imperial IPA","rate":"5","id":"sUL1uy","breweryID":"qghrkC"},{"beer":"Double Stout","rate":"3","id":"uVJmkY","breweryID":"c67gGy"},{"beer":"Double Wide I.P.A.","rate":"4","id":"LKzaHC","breweryID":"VDMEV7"},{"beer":"DreamWeaver Wheat Ale","rate":"3","id":"0aUaXd","breweryID":"n5QFi2"},{"beer":"Drifter Pale Ale","rate":"3","id":"HfAvcD","breweryID":"8wcv7h"},{"beer":"Duck-Rabbit Milk Stout","rate":"4","id":"C6EUeD","breweryID":"rBxNNF"},{"beer":"Duckpin Pale Ale","rate":"3","id":"17OQHF","breweryID":"t6Gyij"},{"beer":"Etrusca","rate":"4","id":"ffEc4d","breweryID":"g0jHqt"},{"beer":"Euforia Toffee Nut","rate":"4","id":"o4ENUL","breweryID":"TVgBWg"},{"beer":"Fat Tire","rate":"2","id":"tuqTtX","breweryID":"Jt43j7"},{"beer":"Festina Peche","rate":"3","id":"Gf9h6p","breweryID":"g0jHqt"},{"beer":"Finestkind IPA","rate":"3","id":"3RFxH6","breweryID":"v0MKXA"},{"beer":"Franziskaner Hefe-Weissbier Hell \/ Franziskaner C","rate":"3","id":"ZhGbaC","breweryID":"o5vGy8"},{"beer":"Furious","rate":"4","id":"qRuupG","breweryID":"cPRfoj"},{"beer":"FV13","rate":"4","id":"xv9fS8","breweryID":"pdLPeS"},{"beer":"Gemini","rate":"3","id":"L8oWyF","breweryID":"7VPQrN"},{"beer":"GKnight","rate":"3","id":"69R1Vd","breweryID":"q6vJUK"},{"beer":"Gold Ale","rate":"3","id":"bIk08d","breweryID":"wTFQaf"},{"beer":"Golden Monkey","rate":"4","id":"UfxKKB","breweryID":"VoKbnS"},{"beer":"Gonzo Imperial Porter","rate":"3","id":"kkcQtN","breweryID":"jmGoBA"},{"beer":"Guinness Draught","rate":"3","id":"StkEiv","breweryID":"HaPdSL"},{"beer":"Hardcore IPA","rate":"3","id":"7RaQWJ","breweryID":"wfAwfx"},{"beer":"Harvest","rate":"3","id":"9Pj2Cr","breweryID":"x8kqVp"},{"beer":"Harvest Moon Pumpkin Ale","rate":"3","id":"Oa5lvx","breweryID":"avMkil"},{"beer":"Heart of Darkness","rate":"3","id":"KvSwJR","breweryID":"qIqpZc"},{"beer":"Hefeweizen","rate":"3","id":"7Xn4wJ","breweryID":"P2xdU4"},{"beer":"Heifer-in-Wheat","rate":"3","id":"4MyOgi","breweryID":"REDaIN"},{"beer":"Heineken","rate":"3","id":"eGtqKZ","breweryID":"robMSl"},{"beer":"Heinnieweisse Weissebeir","rate":"3","id":"RhmCRd","breweryID":"9BPs2d"},{"beer":"Hell or High Watermelon","rate":"4","id":"VNqOKH","breweryID":"EdRcIs"},{"beer":"Helles Lager","rate":"3","id":"MSdXm6","breweryID":"D3A2mu"},{"beer":"Hellhound On My Ale","rate":"4","id":"ALDE5Q","breweryID":"g0jHqt"},{"beer":"Hellrazer","rate":"3","id":"yJ1zJK","breweryID":"TVgBWg"},{"beer":"Hennepin Saison Ale","rate":"4","id":"RsXBB4","breweryID":"tCxPtR"},{"beer":"Hercules Double IPA","rate":"4","id":"xHovlT","breweryID":"I8WZv2"},{"beer":"Honey Weiss","rate":"3","id":"YgHnZO","breweryID":"ZDghkK"},{"beer":"Honker's Ale","rate":"3","id":"TEG3J9","breweryID":"APW1BC"},{"beer":"Hop Heathen Imperial Black Ale","rate":"3","id":"bMXin5","breweryID":"w5OOQ5"},{"beer":"Hop Stoopid","rate":"3","id":"Pni8Jo","breweryID":"nLsoQ9"},{"beer":"Hop Wallop","rate":"3","id":"b58sGA","breweryID":"VoKbnS"},{"beer":"HopDevil","rate":"3","id":"IzTjAm","breweryID":"VoKbnS"},{"beer":"Hopfen-Weisse","rate":"5","id":"wXHOmA","breweryID":"FQLVgV"},{"beer":"Hops Infusion","rate":"3","id":"6nkcOK","breweryID":"a57dkm"},{"beer":"Hopsecutioner","rate":"4","id":"Rfe5aM","breweryID":"DPLTAJ"},{"beer":"Hoptober","rate":"3","id":"n1bFMy","breweryID":"Jt43j7"},{"beer":"Horseshoe Bend Pale Ale","rate":"3","id":"Uvuoay","breweryID":"xcKkLh"},{"beer":"Illinois","rate":"3","id":"PZbw01","breweryID":"APW1BC"},{"beer":"Imperial IPA","rate":"3","id":"WT6hkF","breweryID":"TR98tr"},{"beer":"Imperial Pumpkin Ale","rate":"4","id":"LRCEhC","breweryID":"a57dkm"},{"beer":"Imperial Russian Stout","rate":"0","id":"pD5RiK","breweryID":"709vEK"},{"beer":"Imperial Stout","rate":"4","id":"IqPdv8","breweryID":"Idm5Y5"},{"beer":"India Pale Ale","rate":"3","id":"fu2qgB","breweryID":"APW1BC"},{"beer":"Indian Brown Ale","rate":"3","id":"AZI8ib","breweryID":"g0jHqt"},{"beer":"International Arms Race","rate":"2","id":"aX0e6r","breweryID":"jmGoBA"},{"beer":"IPA","rate":"3","id":"iLlMCb","breweryID":"nLsoQ9"},{"beer":"Italian Strong Ale","rate":"5","id":"9S5ObB","breweryID":"wAdTKf"},{"beer":"Judgement Day","rate":"3","id":"Gwo4rP","breweryID":"9x7wNn"},{"beer":"Just the Tip","rate":"3","id":"ekhzhU","breweryID":"rKXfsB"},{"beer":"Kellerweis","rate":"3","id":"JVv3qI","breweryID":"nHLlnK"},{"beer":"Killian's Irish Red","rate":"3","id":"FXGCOG","breweryID":"avMkil"},{"beer":"La Torpille","rate":"3","id":"gghCq9","breweryID":"6DG1qh"},{"beer":"Lancaster Milk Stout","rate":"3","id":"aZtMd7","breweryID":"VGDfKl"},{"beer":"Little Sumpin' Sumpin'","rate":"4","id":"svXHfu","breweryID":"nLsoQ9"},{"beer":"Local 2","rate":"3","id":"vpdrzj","breweryID":"4OBVPn"},{"beer":"Loose Cannon","rate":"3","id":"Bt79WS","breweryID":"wTFQaf"},{"beer":"Lucky 13","rate":"3","id":"CWwxeR","breweryID":"nLsoQ9"},{"beer":"Lucky 7 Porter","rate":"3","id":"a6XiKI","breweryID":"Ysh6PO"},{"beer":"M\u00e4rzen","rate":"4","id":"zomWCT","breweryID":"P2xdU4"},{"beer":"M\u00fcnchner Sommer","rate":"5","id":"TbP3By","breweryID":"4rlPFf"},{"beer":"Mad Elf Ale","rate":"4","id":"ZRzruY","breweryID":"n5QFi2"},{"beer":"Mad King\u2019s Weiss","rate":"3","id":"5JQA4h","breweryID":"VoKbnS"},{"beer":"Mana Wheat","rate":"3","id":"fem70X","breweryID":"fwCFE4"},{"beer":"Matilda","rate":"5","id":"uKquc0","breweryID":"APW1BC"},{"beer":"Maui Coconut Porter","rate":"3","id":"sNQLDD","breweryID":"fwCFE4"},{"beer":"Merry Monks","rate":"3","id":"gz4uZ6","breweryID":"a57dkm"},{"beer":"Midas Touch","rate":"5","id":"YFZKZA","breweryID":"g0jHqt"},{"beer":"Midshipman Mild","rate":"3","id":"eEjA64","breweryID":"OYQA8m"},{"beer":"Mild Winter","rate":"3","id":"hrhfc1","breweryID":"APW1BC"},{"beer":"Milk Stout Nitro","rate":"3","id":"gtaIQr","breweryID":"Ro08YF"},{"beer":"Miller Lite","rate":"1","id":"KJIjyd","breweryID":"MWi5Kp"},{"beer":"Mischief","rate":"3","id":"5A7pI0","breweryID":"4MVtcc"},{"beer":"Misery Bay IPA","rate":"2","id":"uSxQME","breweryID":"LHQ79n"},{"beer":"Mongo Double IPA","rate":"4","id":"9tIw2j","breweryID":"ayEBYP"},{"beer":"Monumental IPA","rate":"3","id":"kckAgC","breweryID":"9FwufS"},{"beer":"Moo Thunder Stout","rate":"3","id":"D4Ka8l","breweryID":"9BPs2d"},{"beer":"Moonglow Weizenbock","rate":"4","id":"DOrgPb","breweryID":"VoKbnS"},{"beer":"My Antonia","rate":"3","id":"OyeqOI","breweryID":"g0jHqt"},{"beer":"Namaste","rate":"3","id":"1INeXj","breweryID":"g0jHqt"},{"beer":"Narragansett Lager","rate":"2","id":"lBKttb","breweryID":"mGqkXE"},{"beer":"National Bohemian","rate":"2","id":"ssQJcb","breweryID":"AKyyYN"},{"beer":"Newcastle Werewolf","rate":"2","id":"xzED0r","breweryID":"Qutakc"},{"beer":"Noble Rot","rate":"4","id":"tEpzX3","breweryID":"g0jHqt"},{"beer":"Nommo Dubbel","rate":"3","id":"WzJmsi","breweryID":"VDMEV7"},{"beer":"Nugget Nectar","rate":"3","id":"x8H3l7","breweryID":"n5QFi2"},{"beer":"Oak Barrel Stout","rate":"3","id":"JdJXIl","breweryID":"xG9JyI"},{"beer":"Oat Imperial Oatmeal Stout","rate":"4","id":"1UmxNj","breweryID":"x8kqVp"},{"beer":"Old Chub","rate":"3","id":"KmsdoV","breweryID":"q6vJUK"},{"beer":"Old Guardian Oak-Smoked","rate":"4","id":"Pwkvsq","breweryID":"709vEK"},{"beer":"Old Man Winter","rate":"3","id":"X60E3Q","breweryID":"x8kqVp"},{"beer":"Old Pro","rate":"4","id":"SS2uWf","breweryID":"t6Gyij"},{"beer":"Old Rasputin","rate":"4","id":"CfJ0cK","breweryID":"yLBNrD"},{"beer":"Old Ruffian","rate":"3","id":"AqEUBQ","breweryID":"I8WZv2"},{"beer":"Old Scratch Amber Lager","rate":"4","id":"N4LF2o","breweryID":"jmGoBA"},{"beer":"Olde School Barleywine","rate":"5","id":"YDBgUE","breweryID":"g0jHqt"},{"beer":"Ommegang Abbey Ale","rate":"4","id":"jYBtXz","breweryID":"tCxPtR"},{"beer":"Ommegang BPA","rate":"3","id":"N101SS","breweryID":"tCxPtR"},{"beer":"Otis","rate":"2","id":"uTPvQK","breweryID":"7VPQrN"},{"beer":"Ozzy","rate":"4","id":"3te24V","breweryID":"YytkpO"},{"beer":"Pabst Blue Ribbon","rate":"2","id":"pDKyvz","breweryID":"AKyyYN"},{"beer":"Pale Ale","rate":"3","id":"cdkpyx","breweryID":"nHLlnK"},{"beer":"Palo Santo Marron","rate":"5","id":"7LrYr1","breweryID":"g0jHqt"},{"beer":"Pangaea","rate":"4","id":"Oq4XtM","breweryID":"g0jHqt"},{"beer":"Pearl Jam Twenty Faithful Ale","rate":"3","id":"6IjMIK","breweryID":"g0jHqt"},{"beer":"Peeper","rate":"3","id":"KdKlo9","breweryID":"xgrmyW"},{"beer":"Phin ","rate":"3","id":"cWMMWU","breweryID":"x8kqVp"},{"beer":"Porter","rate":"3","id":"bZub8V","breweryID":"9FwufS"},{"beer":"Porter Pounder","rate":"3","id":"h5zbGr","breweryID":"pqSkmD"},{"beer":"Positive Contact","rate":"3","id":"eAvRRc","breweryID":"g0jHqt"},{"beer":"Powder Monkey","rate":"2","id":"PbHXnC","breweryID":"OYQA8m"},{"beer":"Pumkin Ale","rate":"4","id":"zpyLEw","breweryID":"tNDKBY"},{"beer":"Pumking","rate":"4","id":"iHEJZm","breweryID":"x8kqVp"},{"beer":"Pumpkin Ale","rate":"3","id":"yu0Mbf","breweryID":"v0MKXA"},{"beer":"Pumpkinhead Ale","rate":"3","id":"TlBAjN","breweryID":"5N0usi"},{"beer":"Punkin Ale","rate":"4","id":"hGjIJg","breweryID":"g0jHqt"},{"beer":"Racer 5 IPA","rate":"3","id":"o1OELJ","breweryID":"5tw2Iw"},{"beer":"Raging Bitch Belgian IPA","rate":"4","id":"P203ye","breweryID":"jmGoBA"},{"beer":"Raison DEtre","rate":"3","id":"Q4Ah1v","breweryID":"g0jHqt"},{"beer":"Ranger","rate":"3","id":"iaYJ7X","breweryID":"Jt43j7"},{"beer":"Rare Vos","rate":"3","id":"OMyQoC","breweryID":"tCxPtR"},{"beer":"Rayon Vert","rate":"4","id":"vMm0Rc","breweryID":"Nj8cgD"},{"beer":"Red Sky at Night","rate":"3","id":"QWYlpK","breweryID":"wTFQaf"},{"beer":"Reds Rye PA","rate":"3","id":"FOC78c","breweryID":"Idm5Y5"},{"beer":"Robust Porter","rate":"4","id":"fkylQX","breweryID":"v0MKXA"},{"beer":"Ruination IPA","rate":"4","id":"7cnuJq","breweryID":"709vEK"},{"beer":"Sah'tea","rate":"4","id":"dc6ShA","breweryID":"g0jHqt"},{"beer":"Saison du BUFF","rate":"3","id":"CTbk45","breweryID":"g0jHqt"},{"beer":"Saison Rue","rate":"4","id":"V0T8uP","breweryID":"4MVtcc"},{"beer":"Samuel Adams Boston Lager","rate":"3","id":"z4k3eU","breweryID":"1wSztN"},{"beer":"Samuel Adams Octoberfest","rate":"3","id":"Hyyhug","breweryID":"1wSztN"},{"beer":"Samuel Adams Summer Ale","rate":"3","id":"DiBfxM","breweryID":"1wSztN"},{"beer":"Samuel Adams Winter Lager","rate":"2","id":"4fM6Pf","breweryID":"1wSztN"},{"beer":"Sapporo Premium Beer","rate":"2","id":"TOHi4D","breweryID":"D61TcY"},{"beer":"Schwartz Bier","rate":"3","id":"y4THvR","breweryID":"iw1hDB"},{"beer":"Shock Top","rate":"2","id":"v7H1Ev","breweryID":"BznahA"},{"beer":"Sixty-One","rate":"4","id":"L1eJ2p","breweryID":"g0jHqt"},{"beer":"Small Craft Warning","rate":"3","id":"2qHZyl","breweryID":"wTFQaf"},{"beer":"Smoking Wood","rate":"5","id":"8NX7Sy","breweryID":"4MVtcc"},{"beer":"Snake Dog","rate":"3","id":"Blig3z","breweryID":"jmGoBA"},{"beer":"Snow Pants","rate":"4","id":"g8oVuV","breweryID":"t6Gyij"},{"beer":"Sofie","rate":"4","id":"KBQTlS","breweryID":"APW1BC"},{"beer":"Son of a Peach","rate":"4","id":"94Fe7D","breweryID":"bWL816"},{"beer":"Squall IPA","rate":"4","id":"LZmXC8","breweryID":"g0jHqt"},{"beer":"Stella Artois","rate":"3","id":"Jc7iGI","breweryID":"mIWMKP"},{"beer":"Stone IPA","rate":"3","id":"PAM6wX","breweryID":"709vEK"},{"beer":"Stone Levitation Ale","rate":"4","id":"OaZYgf","breweryID":"709vEK"},{"beer":"Storm King Imperial Stout","rate":"4","id":"4M26ru","breweryID":"VoKbnS"},{"beer":"Sublimely Self-Righteous Ale","rate":"4","id":"SAtbDP","breweryID":"709vEK"},{"beer":"Summer Love Ale","rate":"3","id":"SzhLvG","breweryID":"VoKbnS"},{"beer":"Summer Shandy","rate":"2","id":"nJjPsG","breweryID":"ZDghkK"},{"beer":"Summertime","rate":"3","id":"tXTETf","breweryID":"APW1BC"},{"beer":"Sunset Amber Ale","rate":"4","id":"scC0RX","breweryID":"xcKkLh"},{"beer":"Sweet Baby Jesus!","rate":"4","id":"opSf4n","breweryID":"TVgBWg"},{"beer":"Sweet Child of Vine","rate":"3","id":"xrLwng","breweryID":"5GoGSi"},{"beer":"ta henket","rate":"4","id":"icSARl","breweryID":"g0jHqt"},{"beer":"Tank 7 Farmhouse Ale","rate":"3","id":"lDiXyX","breweryID":"VDMEV7"},{"beer":"Ten Fidy","rate":"5","id":"x6bRxw","breweryID":"q6vJUK"},{"beer":"The Cask","rate":"3","id":"NgxwRi","breweryID":"dwroV3"},{"beer":"The Fear","rate":"2","id":"0ZX12D","breweryID":"jmGoBA"},{"beer":"The Libertine","rate":"4","id":"irF7Mh","breweryID":"5GoGSi"},{"beer":"The Sixth Glass","rate":"4","id":"zrAAHP","breweryID":"VDMEV7"},{"beer":"The Truth","rate":"3","id":"o9TSOv","breweryID":"jmGoBA"},{"beer":"The Vermonster","rate":"2","id":"UJI7C4","breweryID":"LpX3cU"},{"beer":"Theobroma","rate":"0","id":"H1L7UE","breweryID":"g0jHqt"},{"beer":"Third Shift","rate":"3","id":"g284jn","breweryID":"avMkil"},{"beer":"Thomas Jefferson's Tavern Ale","rate":"3","id":"xDzvQP","breweryID":"jwWiTH"},{"beer":"Three Philosophers","rate":"4","id":"TOzkLy","breweryID":"tCxPtR"},{"beer":"Torpedo Extra IPA","rate":"3","id":"JaS6T7","breweryID":"nHLlnK"},{"beer":"Trade Winds Tripel","rate":"4","id":"jdulDH","breweryID":"4MVtcc"},{"beer":"Tribute Tripel","rate":"5","id":"FdRZ7e","breweryID":"UbQHhM"},{"beer":"Troegenator Double Bock","rate":"3","id":"OGlmOC","breweryID":"n5QFi2"},{"beer":"Tweasonale","rate":"3","id":"Qx1hbt","breweryID":"g0jHqt"},{"beer":"UFO White","rate":"3","id":"cG83LQ","breweryID":"RzvedX"},{"beer":"Unfiltered Wheat Beer","rate":"3","id":"lgaPWe","breweryID":"VDMEV7"},{"beer":"Unibroue 17","rate":"4","id":"VfyYpZ","breweryID":"llbEuB"},{"beer":"Urkontinent","rate":"3","id":"XQyXhk","breweryID":"g0jHqt"},{"beer":"Vanilla Porter","rate":"3","id":"Bk34Go","breweryID":"IImUD9"},{"beer":"Vanilla Stout","rate":"4","id":"NRhCm4","breweryID":"oBe8dQ"},{"beer":"Walkers Reserve","rate":"3","id":"JgdTCV","breweryID":"qghrkC"},{"beer":"Wells Banana Bread Beer","rate":"4","id":"hQzCbn","breweryID":"6oEP92"},{"beer":"West Coast IPA","rate":"4","id":"RC4BkU","breweryID":"Nj8cgD"},{"beer":"Western Rider","rate":"2","id":"ymabSa","breweryID":"bRKIdZ"},{"beer":"Winter Storm - Category 5 Ale","rate":"3","id":"fA0O8C","breweryID":"wTFQaf"},{"beer":"Wookey Jack","rate":"3","id":"f5DoHi","breweryID":"qghrkC"},{"beer":"World Wide Stout","rate":"4","id":"H5sA73","breweryID":"g0jHqt"},{"beer":"Yakima Glory Ale","rate":"3","id":"q7dLm5","breweryID":"VoKbnS"},{"beer":"Yeti Imperial Stout","rate":"5","id":"oz1oll","breweryID":"I8WZv2"},{"beer":"Yuengling Traditional Lager","rate":"3","id":"wd1Y84","breweryID":"pX8lES"}]
适配器代码:
public class ShortBeerInfoAdapter extends ArrayAdapter<ShortBeerInfo>{
Context context;
int layoutResourceId;
List<ShortBeerInfo> data = null;
public ShortBeerInfoAdapter(Context context, int layoutResourceId, List<ShortBeerInfo> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
beerHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new beerHolder();
holder.txtBeer = (TextView)row.findViewById(R.id.breweryName);
holder.txtRate = (TextView)row.findViewById(R.id.breweryRate);
holder.txtBar = (RatingBar) row.findViewById(R.id.starbar);
row.setTag(holder);
}
else
{
holder = (beerHolder)row.getTag();
}
ShortBeerInfo beer = data.get(position);
holder.txtBeer.setText(beer.beer);
holder.txtRate.setText(beer.rate + " out of 5.00 Stars");
holder.numHolder= Float.parseFloat(beer.rate);
holder.txtBar.setNumStars(5);
holder.txtBar.setRating(holder.numHolder);
return row;
}
static class beerHolder
{
TextView txtBeer;
TextView txtRate;
RatingBar txtBar;
Float numHolder;
}
}
答案 0 :(得分:3)
我认为您无法正确查看列表,因为您的ListVew位于ScrollView中。 ListView本身是可滚动的,因此它无法在ScrollView中正确显示,您最多可以看到一个高度非常小的ListView,它可以容纳一个或两个元素。尝试在ScrollView外部使用ListView来测试您的代码是否正确。希望它有所帮助。
答案 1 :(得分:1)
简短的回答是你做错了。
您应该将数据异步加载到sqlite db表中并使用带有游标适配器的Loader。
此外,即使使用您拥有的代码(以及此处难以从手机中读取),在将列表适配器添加到列表视图之前,您似乎只能向列表中添加一个元素,这就是您的原因只看到一个项目。
可能导致您遇到麻烦的另一件事是,您异步将数据加载到已创建的列表视图中,但在任务完成时您似乎并未发出适配器具有新数据的信号。
答案 2 :(得分:0)
移动适配器的代码,使其位于for循环的外部和下方。您正在为每次迭代创建一个新的适配器,因此它可能运行得非常慢。此外,您还要为每次迭代设置onitemclicklistener。它只需要做一次。
这样做,然后看看会发生什么。在for循环之后还放置一个断点,看看你的味道列表中有多少项。如果有多个,那么你的适配器很可能是问题。
如果您可以发布适配器的代码。
我想我的回答是你可能没有返回多个项目。由于适配器将数据发布到列表,它应该没问题。一旦你可以调试应用程序并查看返回的项目数量,我们就可以确定。