我有以下代码: -
public class ByState extends Fragment {
List<LegislatorByState> bystate= new ArrayList<LegislatorByState>();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.by_state, container, false);
new GetAllStates().execute("dummy");
ArrayAdapter adapter = new ArrayAdapter<LegislatorByState>(getActivity(), R.layout.bystate_itemview, bystate);
ListView list = (ListView) rootView.findViewById(R.id.state_listView);
list.setAdapter(adapter);
return rootView;
}
public void populateList(String bioguide, String lastname,String firstname,String party,String statename,String district ){
bystate.add(new LegislatorByState(bioguide, lastname, firstname, party, statename, district));
Log.d("bio",bioguide);
}
}
class LegislatorByState{
public String lastname;
public String firstname;
public String bioguide;
public String statename;
public String district;
public String party;
public LegislatorByState(String lastname, String firstname, String bioguide, String statename, String district, String party) {
this.lastname = lastname;
this.firstname = firstname;
this.bioguide = bioguide;
this.statename = statename;
this.district = district;
this.party = party;
}
public void display(){
ByState obj=new ByState();
obj.populateList(this.bioguide,this.lastname,this.firstname,this.party,this.statename,this.district);
}
}
class GetAllStates extends AsyncTask<String,String,JsonReader> {
@Override
protected JsonReader doInBackground(String... key) {
JsonReader reader=null;
try {
String url = "http://congress-search-148802.appspot.com/getData.php?value=legislator";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
int responseCode = con.getResponseCode();
reader = new JsonReader(new InputStreamReader(con.getInputStream()));
} catch (Exception e) {
Log.w("Error", e.getMessage());
}
return reader;
}
@Override
protected void onPostExecute(JsonReader reader) {
LegislatorByState object = null;
try {
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
if (name.equals("results")){
reader.beginArray();
while (reader.hasNext()) {
reader.beginObject();
while (reader.hasNext()) {
String id = reader.nextName();
if(id.equals("bioguide_id")){
if(object != null) {object.display();}
object = new LegislatorByState(null,null,null,null,null,null);
String bioguide_id=reader.nextString();
object.bioguide=bioguide_id;
}
else if(id.equals("last_name")){
String lastname=reader.nextString();
object.lastname=lastname;
}
else if(id.equals("first_name")){
String firstname=reader.nextString();
object.firstname=firstname;
}
else if(id.equals("party")){
String party=reader.nextString();
object.party=party;
}
else if(id.equals("state_name")){
String statename=reader.nextString();
object.statename=statename;
}
else if(id.equals("district")){
if (reader.peek() == JsonToken.NULL){
reader.skipValue();
object.district="NA";
}
else{
String district=reader.nextString();
object.district=district;
}
}
else
reader.skipValue();
//object.display();
}reader.endObject();
}reader.endArray();
}
else {
reader.skipValue();
}
}reader.endObject();
}
catch (IOException e){
Log.w("Error",e.getMessage());
}
//object.display(obj);
}
}
我能够获得正确的数据,但无法在列表视图中显示它。有人可以请你帮我一个新的。我无法掌握如何在片段中显示列表视图的概念。