2016-04-07 19 views
1

カスタムArrayAdapterをListFragmentにSlidingTabLayoutで表示します。以前は、通常のアクティビティでアダプタをテストして正常に動作し、SlidingTabLayoutも正しく動作しましたが、ListFragmentでは何も表示されません。私は流星のサーバーからデータを取得し、logcatはデータがダウンロードされたことを示します...ListFragmentにカスタムArrayAdapterを表示

誰かがヒントを持っていますか?

これは私の断片Javaコードです:

public class MorningFragment extends ListFragment { 

@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.fragment_morning,container,false); 


    return v; 
} 

@Override 
public void onActivityCreated(Bundle savedInstanceState){ 
    super.onActivityCreated(savedInstanceState); 
    MedicationTimeAdapter timeAdapter; 
    timeAdapter = new MedicationTimeAdapter(getActivity(), R.layout.item_time); 
    setListAdapter(timeAdapter); 
} 
} 

そして、私の断片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"> 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

    </ListView> 

</LinearLayout> 

マイアダプタ

public class MedicationTimeAdapter extends ArrayAdapter<MedicationTime> implements DataManager.MedicationCallback { 
    private static final String LOG_TAG = MedicationTimeAdapter.class.getName(); 
    private final int resourceId; 
    private final Activity activity; 
// private List<MedicationEntry> medications = new ArrayList<MedicationEntry>(); 

    public MedicationTimeAdapter(Activity context, int resource) { 
     super(context, resource); 
     resourceId = resource; 
     activity = context; 
    } 

    @Override 
    public void handleMedicationPlan(List<MedicationEntry> medication) { 
     // ignore 
    } 

    @Override 
    public void handleMedicationTimes(final List<MedicationTime> medicationTimes) { 
     // TODO Activity möglicherweise nicht mehr aktiv 
     activity.runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       setItems(medicationTimes); 
      } 
     }); 
    } 


    public void setItems(List<MedicationTime> itemList) { 
     clear(); 
     for (MedicationTime item : itemList) { 
      add(item); 
     } 

     notifyDataSetChanged(); 
    } 

    @Override 
    public int getCount() { 
     return super.getCount(); 
    } 

    @Override 
    public View getView(int index, View convertView, ViewGroup parent) { 
     //data from your adapter 
     MedicationTime itemData = getItem(index); 

     if (convertView == null) { 
//   final LayoutInflater inflater = MHApplication.getLayoutInflater(); 
      final LayoutInflater inflater = LayoutInflater.from(parent.getContext()); 
      // TODO mit Referenz Parent-Group erzeugen?! (war für ColorFinder notwendig, um die LayoutParams beim Laden zu erhalten) 
//   convertView = inflater.inflate(resourceId, parent, false); 
      convertView = inflater.inflate(resourceId, null); 
//   prepareInflatedView(convertView); 
     } 

     setViewContent(index, convertView, itemData); 

     return convertView; 
    } 

    protected void setViewContent(int index, View itemView, MedicationTime itemData) { 
     final TextView nameView = (TextView) itemView.findViewById(R.id.time_name); 
     final TextView medicationView = (TextView) itemView.findViewById(R.id.medication); 

     int nameId = activity.getResources().getIdentifier("time_name." + itemData.getTimeName(), "string", activity.getPackageName()); 
     nameView.setText(activity.getResources().getString(nameId)); 
     nameView.setText(nameId); 

     StringBuilder medText = new StringBuilder(); 
     List<MedicationTime.Entry> medications = itemData.getMedications(); 
     for (MedicationTime.Entry medication : medications) { 
      medText.append(medication.getCount()); 
      medText.append(" * "); 
      medText.append(medication.getMedicationEntry().getSubstance()); 
      medText.append(" ("); 
      medText.append(medication.getMedicationEntry().getTradingName()); 
      medText.append(")\n"); 
     } 
     medicationView.setText(medText.toString()); 
    } 

編集:

私は問題を解決した、私は私のmainActivityにデータでアダプタを埋めるためのメソッドがあります。問題は私の断片に新しいアダプタを作成することでした。だから私は私のmainActivityから私のフラグメントクラスと提出者のアダプタにkonstruktorを追加します。いくつかのデータと

public MorningFragment(MedicationTimeAdapter timeAdapter) { 
    medicationTimeAdapter = timeAdapter; 
} 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.fragment_morning,container,false); 

    setListAdapter(medicationTimeAdapter); 

    return v; 
} 
+0

[こちら](http://stackoverflow.com/questions/16338281/custom-adapter-getview-method-is-not-called/16338380#16338380) – Blackbelt

+0

MedicationTimeAdapter.javaを投稿できますか? –

+0

私はそれを編集 – developKinberg

答えて

0

コールhandleMedicationTimes(final List<MedicationTime> medicationTimes)、あなたのコードは、任意のデータを膨張されていないようです。

+0

にデータを送信していません。これは流星群からデータが得られます。これは正しく動作し、データはコンテナクラス(DataManager)に格納されます。通常のlayout_activityでアダプタが正常に動作し、データが表示されます – developKinberg

+0

おそらくactivity_main.xmlコードが役立つ可能性があります –

関連する問題