简体   繁体   English

在ListFragment中显示自定义ArrayAdapter

[英]Show Custom ArrayAdapter in a ListFragment

I want to show my custom ArrayAdapter in a ListFragment with a SlidingTabLayout. 我想在带有SlidingTabLayout的ListFragment中显示我的自定义ArrayAdapter。 Previously I tested the adapter in a normal activity and worked fine, also the SlidingTabLayout worked correctly, but yet the ListFragment show nothing. 以前我在正常活动中测试了适配器并且工作正常,SlidingTabLayout也正常工作,但ListFragment却没有显示任何内容。 I get the data from a meteor server and logcat show me the data has been downloaded... 我从流星服务器获取数据,logcat显示数据已下载...

Does anybody have some tips? 有人有提示吗?

This is my Fragment Java Code: 这是我的片段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);
}
}

And my Fragment XML Code: 和我的片段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>

My adapter 我的适配器

    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());
    }

Edit: 编辑:

I solved the Problem , i have a Method in my mainActivity to fill the adapter with Data. 我解决了问题,我在mainActivity中有一个方法用Data填充适配器。 The problem was i create a new Adapter in my Fragment. 问题是我在片段中创建了一个新的适配器。 So i add a konstruktor in my Fragment class and submitter the Adapter from my mainActivity. 所以我在Fragment类中添加了一个konstruktor,并从我的mainActivity中提交了适配器。

   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;
}

使用一些数据调用handleMedicationTimes(final List<MedicationTime> medicationTimes) ,您的代码似乎不会膨胀任何数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM