Java 在ListFragment中显示自定义ArrayAdapter
我想在带有SlidengTableOut的ListFragment中显示我的自定义ArrayAdapter。之前我在一个正常的活动中测试了适配器,工作正常,SlidingTableOut也正常工作,但是ListFragment没有显示任何内容。我从meteor服务器获取数据,logcat显示数据已下载 有人有什么建议吗 这是我的片段Java代码:Java 在ListFragment中显示自定义ArrayAdapter,java,android,meteor,Java,Android,Meteor,我想在带有SlidengTableOut的ListFragment中显示我的自定义ArrayAdapter。之前我在一个正常的活动中测试了适配器,工作正常,SlidingTableOut也正常工作,但是ListFragment没有显示任何内容。我从meteor服务器获取数据,logcat显示数据已下载 有人有什么建议吗 这是我的片段Java代码: public class MorningFragment extends ListFragment { @Override public View
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>
调用
handleMedicationTimes(最终列表药物时间)
使用一些数据,您的代码似乎没有膨胀任何数据。调用handleMedicationTimes(最终列表药物时间)
使用一些数据,您的代码似乎没有膨胀任何数据。看一看,您可以发布MedicalationTimeAdapter.java吗?我通过edit@Blackbelt适配器正确地工作在正常活动中,但不在片段中。我认为片段类是问题所在,而不是适配器中的getView。片段不是问题。您没有将数据提交给adpater/看一看,您可以发布MedicalationTimeAdapter.java吗?我通过edit@Blackbelt适配器正确地工作在正常活动中,但不在片段中。我认为片段类是问题所在,而不是适配器中的getView。片段不是问题。您没有将数据提交给adpater/i从meteor服务器获取数据。该服务器工作正常,数据存储在容器类(DataManager)中。在正常的layout_活动中,适配器工作正常,并向我显示数据我从meteor服务器获取的数据工作正常,数据存储在containerclass(DataManager)中。在正常的布局活动中,适配器工作正常,并向我显示数据
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());
}
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;
}