2016-08-09 14 views
2
Arbeits

ich eine benutzerdefinierte Listenansicht in Fragmente erstellt, aber wenn ich laufe, ist es nichts angezeigt unten vollständiger Code ist mir bitte meine PlanetFragment Klasse hiererstellt eine benutzerdefinierte Listenansicht in Fragmente nicht

hilft

public class PlanetFragment extends ListFragment { 
 
    public static final String ARG_PLANET_NUMBER = "planet_number"; 
 
    ListView listView; 
 
    ArrayList<Actors> actorList; 
 
    ActorsAdapter adapter; 
 
    private View view; 
 
    public PlanetFragment() { 
 
     // Empty constructor required for fragment subclasses 
 
    } 
 

 
    @Override 
 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
 
      Bundle savedInstanceState) { 
 
    \t if (view == null) { 
 
    \t \t view = inflater.inflate(R.layout.fragment_planet, container, false); 
 
     }else { 
 
     \t ViewGroup parent = (ViewGroup) view.getParent(); 
 
      parent.removeView(view); 
 
\t \t } 
 
    \t 
 
     listView = (ListView) view.findViewById(android.R.id.list); 
 
    
 
     actorList = new ArrayList<Actors>(); 
 
     adapter = new ActorsAdapter(getActivity(),R.layout.row, actorList); 
 
     listView.setAdapter(adapter); 
 
     new JSONAsyncTask().execute("/UpcomingEvents/GetEvents/1"); 
 
     return view; 
 
    } 
 
    public class JSONAsyncTask extends AsyncTask<String, Void, Boolean>{ 
 

 
\t \t @Override 
 
\t \t protected Boolean doInBackground(String... params) { 
 
\t \t \t HttpGet httpGs[0]); 
 
\t \t \t HttpClient httpClient = new DefaultHttpClient(); 
 
\t \t \t try { 
 
\t \t \t \t HttpResponse response = httpClient.execute(httpGet); 
 
\t \t \t \t int status = response.getStatusLine().getStatusCode(); 
 
\t \t \t \t if(status == 200){ 
 
\t \t \t \t \t HttpEntity entity = response.getEntity(); 
 
\t \t \t \t \t String data = EntityUtils.toString(entity); 
 
// \t \t \t \t \t JSONObject jsonObject = new JSONObject(data); 
 
\t \t \t \t \t JSONArray jsonArray = new JSONArray(data); 
 
\t \t \t \t \t for(int i=0;i<jsonArray.length();i++){ 
 
\t \t \t \t \t \t JSONObject object = jsonArray.getJSONObject(i); 
 
\t \t \t \t \t \t Actors actor = new Actors(); 
 
\t \t \t \t \t \t actor.setName(object.getString("eventTitle")); 
 
\t \t \t \t \t 
 
\t \t \t \t \t \t actorList.add(actor); 
 
\t \t \t \t \t \t 
 
\t \t \t \t \t } 
 
\t \t \t \t \t return true; 
 
\t \t \t \t } 
 
\t \t \t \t 
 
\t \t \t } catch (ClientProtocolException e) { 
 
\t \t \t \t // TODO Auto-generated catch block 
 
\t \t \t \t e.printStackTrace(); 
 
\t \t \t } catch (IOException e) { 
 
\t \t \t \t // TODO Auto-generated catch block 
 
\t \t \t \t e.printStackTrace(); 
 
\t \t \t } catch (JSONException e) { 
 
\t \t \t \t // TODO Auto-generated catch block 
 
\t \t \t \t e.printStackTrace(); 
 
\t \t \t } 
 
\t \t \t 
 
\t \t \t return false; 
 
\t \t } 
 
    \t 
 
    } 
 
}

hier ist meine ActorsAdapter Klasse

public class ActorsAdapter extends ArrayAdapter<Actors> { 
 

 
\t ArrayList<Actors> arraylistObject; 
 
\t int resource; 
 
\t Context context; 
 
\t ViewHolder holder; 
 
\t LayoutInflater li; 
 
\t private View view; 
 

 
\t public ActorsAdapter(Context context, int resource, 
 
\t \t \t ArrayList<Actors> objects) { 
 
\t \t super(context, resource, objects); 
 
\t \t // TODO Auto-generated constructor stub 
 

 
\t \t this.resource = resource; 
 
\t \t arraylistObject = objects; 
 
\t \t this.context = context; 
 

 
\t } 
 

 
\t @Override 
 
\t public View getView(int position, View convertView, ViewGroup parent) { 
 
\t \t // TODO Auto-generated method stub 
 
\t \t li = (LayoutInflater) context 
 
\t \t \t \t .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
 
\t \t view = li.inflate(resource, parent, false); 
 
\t \t if (view == null) { 
 
\t \t \t holder = new ViewHolder(); 
 
\t \t \t view = li.inflate(resource, parent, false); 
 

 
\t \t \t holder.tvName = (TextView) view.findViewById(R.id.tvName); 
 

 
\t \t \t view.setTag(holder); 
 
\t \t } else { 
 
\t \t \t holder = (ViewHolder) view.getTag(); 
 
\t \t } 
 

 
\t \t holder.tvName.setText(arraylistObject.get(position).getName()); 
 

 
\t \t return view; 
 

 
\t } 
 

 
\t static class ViewHolder { 
 

 
\t \t public TextView tvName; 
 

 
\t } 
 

 
}

und hier ist fragment_planet.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    xmlns:tools="http://schemas.android.com/tools" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent" 
 
    tools:context="com.example.demoasynctask.MainActivity" > 
 
    <ListView 
 
     android:id="@android:id/list" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="wrap_content" 
 
     tools:listitem="@layout/row"></ListView> 
 
</RelativeLayout>

hier row.xml ist

<?xml version="1.0" encoding="utf-8"?> 
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    android:id="@+id/LinearLayout1" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="wrap_content" 
 
    android:padding="4dp" 
 
    android:orientation="vertical" > 
 
    
 
    <LinearLayout 
 
     android:layout_width="match_parent" 
 
     android:layout_height="wrap_content" > 
 
    
 
     <ImageView 
 
      android:id="@+id/ivImage" 
 
      android:layout_width="wrap_content" 
 
      android:layout_height="wrap_content" 
 
      android:layout_margin="8dp" 
 
      android:src="@drawable/ic_launcher" /> 
 
    
 
     <LinearLayout 
 
      android:layout_width="wrap_content" 
 
      android:layout_height="wrap_content" 
 
      android:orientation="vertical" > 
 
    
 
      <TextView 
 
       android:id="@+id/tvName" 
 
       android:layout_width="wrap_content" 
 
       android:layout_height="wrap_content" 
 
       android:text="Tom Cruise" 
 
       android:textColor="#166CED" 
 
       android:textAppearance="?android:attr/textAppearanceLarge" /> 
 
    
 
      <TextView 
 
       android:id="@+id/tvDateOfBirth" 
 
       android:layout_width="wrap_content" 
 
       android:layout_height="wrap_content" 
 
       android:textColor="#D64530" 
 
       android:text="Date of Birth: July 3, 1962" /> 
 
    
 
      <TextView 
 
       android:id="@+id/tvHeight" 
 
       android:layout_width="wrap_content" 
 
       android:layout_height="wrap_content" 
 
       android:text="Height: 1.80 m" 
 
       android:textColor="#D64530" 
 
       android:textAppearance="?android:attr/textAppearanceSmall" /> 
 
    
 
      <TextView 
 
       android:id="@+id/tvCountry" 
 
       android:layout_width="wrap_content" 
 
       android:layout_height="wrap_content" 
 
       android:textColor="#D64530" 
 
       android:text="United States" /> 
 
    
 
     </LinearLayout> 
 
    
 
    </LinearLayout> 
 
    
 
    <TextView 
 
     android:id="@+id/tvDescriptionn" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:textColor="#009A57" 
 
     android:text="Description" /> 
 
    
 
    <TextView 
 
     android:id="@+id/tvSpouse" 
 
     android:layout_width="wrap_content" android:textColor="#166CED" 
 
     android:layout_height="wrap_content" 
 
     android:text="Spouse: Katie Holmes" /> 
 
    
 
    <TextView 
 
     android:id="@+id/tvChildren" 
 
     android:layout_width="wrap_content" android:textColor="#166CED" 
 
     android:layout_height="wrap_content" 
 
     android:text="Children: Suri Cruise, Isabella Jane Cruise, Connor Cruise" /> 
 
    
 
</LinearLayout>

+0

Sie haben außer Kraft setzen nicht 'getItemCount' Methode –

Antwort

0

Sie müssen notifyDataSetChanged() auf Ihrem Adapter für die ListView zu aktualisieren.

Aus der Dokumentation von notifyDataSetChanged():

Benachrichtigt die beigefügten Beobachter, dass die zugrunde liegenden Daten geändert worden ist und jede View den Datensatz reflektieren sollte selbst aktualisieren.

So etwas sollte funktionieren:

public class JSONAsyncTask extends AsyncTask<String, Void, Boolean>{ 

    @Override 
    protected Boolean doInBackground(String... params) { 
     HttpGet httpGs[0]); 
     HttpClient httpClient = new DefaultHttpClient(); 
     try { 
      HttpResponse response = httpClient.execute(httpGet); 
      int status = response.getStatusLine().getStatusCode(); 
      if(status == 200){ 
       HttpEntity entity = response.getEntity(); 
       String data = EntityUtils.toString(entity); 
       JSONArray jsonArray = new JSONArray(data); 
       for(int i=0;i<jsonArray.length();i++){ 
        JSONObject object = jsonArray.getJSONObject(i); 
        Actors actor = new Actors(); 
        actor.setName(object.getString("eventTitle")); 

        actorList.add(actor); 
       } 
       return true; 
      } 

     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     return false; 
    } 

    @Override 
    protected void onPostExecute(Boolean aBoolean) { 

     // tell the adapter that its data changed 
     adapter.notifyDataSetChanged(); 
    } 
}