2016-11-17 1 views
0

У меня есть следующий код, чтобы получить данные для просмотра списка, а также адаптер разместить его: -ListView оленья кожа отображаться на вкладке внутри фрагмента

public class ByState extends Fragment { 
    public ArrayList<Person> personData = new ArrayList<Person>(); 
    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.by_state, container, false); 
     Context cont=getActivity(); 
     new GetAllStates(getContext(),rootView).execute(); 
     return rootView; 
    } 
    class GetAllStates extends AsyncTask<String,String,JsonReader> { 
     private ByState asyncContext=null; 
     private View rootView=null; 
     private Context context=null; 
     public GetAllStates(Context state,View view) { 
      this.context=state; 
      this.rootView = view; 
     } 
     @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) { 
      ListView yourListView = (ListView) rootView.findViewById(R.id.state_listView); 
      Person object; 
      try { 
       reader.beginObject(); 
       while (reader.hasNext()) { 
        String name = reader.nextName(); 
        if (name.equals("results")){ 
         reader.beginArray(); 
         while (reader.hasNext()) { 
          reader.beginObject(); 
          object=new Person(); 
          while (reader.hasNext()) { 
           String id = reader.nextName(); 
           if(id.equals("bioguide_id")){ 
            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.state=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.image="https://theunitedstates.io/images/congress/original/"+object.bioguide+".jpg"; 
           //object.display(); 
          } 
          personData.add(object); 
          reader.endObject(); 
         }reader.endArray(); 
        } 
        else { 
         reader.skipValue(); 
        } 
       }reader.endObject(); 
      } 
      catch (IOException e) { 
       Log.w("Error",e.getMessage()); 
      } 
      ListAdapter customAdapter = new ListAdapter(context, R.layout.bystate_itemview,personData); 
      yourListView .setAdapter(customAdapter); 
     } 
    } 
    public class ListAdapter extends ArrayAdapter<Person> { 
     public ListAdapter(Context context, int textViewResourceId) { 
      super(context, textViewResourceId); 
     } 
     public ListAdapter(Context context, int resource, ArrayList<Person> items) { 
      super(context, resource, items); 
     } 
     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View v = convertView; 
      if (v == null) { 
       LayoutInflater vi; 
       vi = LayoutInflater.from(getContext()); 
       v = vi.inflate(R.layout.bystate_itemview, null); 
      } 
      Person p = getItem(position); 
      if (p != null) { 
       TextView tt1 = (TextView) v.findViewById(R.id.last_name); 
       TextView tt2 = (TextView) v.findViewById(R.id.first_name); 
       if (tt1 != null) { 
        tt1.setText(p.getLastname()); 
       } 
       if (tt2 != null) { 
        tt2.setText(p.getFirstname()); 
       } 
      } 
      return v; 
     } 
    } 
} 
class Person{ 
    String bioguide; 
    String image; 
    String lastname; 
    String firstname; 
    String district; 
    String state; 
    String party; 
    public String getBioguide() { 
     return bioguide; 
    } 
    public void setBioguide(String bioguide) { 
     this.bioguide = bioguide; 
    } 
    public String getDistrict() { 
     return district; 
    } 
    public void setDistrict(String district) { 
     this.district = district; 
    } 
    public String getFirstname() { 
     return firstname; 
    } 
    public void setFirstname(String firstname) { 
     this.firstname = firstname; 
    } 
    public String getImage() { 
     return image; 
    } 
    public void setImage(String image) { 
     this.image = image; 
    } 
    public String getLastname() { 
     return lastname; 
    } 
    public void setLastname(String lastname) { 
     this.lastname = lastname; 
    } 
    public String getParty() { 
     return party; 
    } 
    public void setParty(String party) { 
     this.party = party; 
    } 
    public String getState() { 
     return state; 
    } 
    public void setState(String state) { 
     this.state = state; 
    } 
} 

ArrayList содержит все объекты, но не отображается в виде списка вообще. Он просто показывает пустой экран.

Мой в XML: -

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 
    <ListView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/state_listView" 
     android:layout_alignParentTop="true" /> 
</RelativeLayout> 

и itemview 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" 
    android:weightSum="1"> 

    <TextView 
     android:layout_width="163dp" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Medium Text" 
     android:id="@+id/last_name" 
     android:layout_weight="0.06" /> 

    <TextView 
     android:layout_width="102dp" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:text="Small Text" 
     android:id="@+id/first_name" 
     android:layout_weight="0.06" /> 
</LinearLayout> 

Это мой первый заказ адаптер, и я не знаю, где я неправильно. Любая помощь высоко ценится. Фрагменты и вкладки отображаются правильно.

ответ

1

Может быть, попробуйте следующее:

android:layout_width="match_parent" 
android:layout_height="match_parent" 

относительного расположения и ListView, а также.

+0

Спасибо, его работа ... но почему обертывание содержимого не работает? – Anirban

Смежные вопросы