我是 Android 新手,我找不到有效的答案。
我有以下适配器:
public class DailyDataAdapter extends ArrayAdapter<DailyData> {
private LayoutInflater layoutInflater;
private ArrayList<DailyData> dailyDatas;
private boolean isUserSavedSectionDefined = false;
private boolean isAutoSavedSectionDefined = false;
private int resourceId;
public DailyDataAdapter(Context context, int textViewResourceId,
ArrayList<DailyData> dailyDatas) {
super(context, textViewResourceId, dailyDatas);
this.layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.dailyDatas = dailyDatas;
this.resourceId = textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View dailyDataRow = convertView;
DailyData dailyData = dailyDatas.get(position);
if (dailyDataRow == null) {
dailyDataRow = layoutInflater.inflate(resourceId, null);
if (!isUserSavedSectionDefined && dailyData.isUserSaved()) {
TextView section = (TextView) dailyDataRow
.findViewById(R.id.section);
section.setText(R.string.user_saved_path);
section.setVisibility(View.VISIBLE);
isUserSavedSectionDefined = true;
} else if (!isAutoSavedSectionDefined && !dailyData.isUserSaved()) {
TextView section = (TextView) dailyDataRow
.findViewById(R.id.section);
section.setText(R.string.auto_saved_path);
section.setVisibility(View.VISIBLE);
isAutoSavedSectionDefined = true;
}
TextView description = (TextView) dailyDataRow
.findViewById(R.id.description);
description.setText(dailyData.getDate());
}
return dailyDataRow;
}
@Override
public void add(DailyData newDailyData) {
dailyDatas.add(newDailyData);
}
@Override
public void addAll(Collection<? extends DailyData> newDailyDatas) {
dailyDatas.addAll(newDailyDatas);
}
@Override
public void insert(DailyData newDailyData, int index) {
dailyDatas.add(index, newDailyData);
}
@Override
public void remove(DailyData newDailyData) {
dailyDatas.remove(newDailyData);
}
@Override
public void clear() {
dailyDatas.clear();
}
}
这会膨胀以下布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/section"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:layout_marginLeft="50dp"
android:background="@drawable/listitem_section"
android:gravity="center"
android:textColor="@color/white"
android:visibility="gone"
android:textStyle="bold" />
<TextView
android:id="@+id/description"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_marginLeft="50dp"
android:background="@drawable/listitem_selector"
android:gravity="center"
android:singleLine="true"
android:textColor="@color/black"
android:textSize="@dimen/normal_text" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_marginLeft="50dp"
android:layout_height="1dp"
android:background="@color/orange" >
</LinearLayout>
现在,问题是,如果我在将它与我假装显示信息的 listView 关联之前设置与适配器关联的列表的内容,它工作得很好,如下所示:
//Step #1
ArrayList<Data> ald = new ArrayList<Data>();
Location l = new Location("test");
Data d = new Data(l);
ald.add(d);
DailyData dd = new DailyData(false);
DailyData dd2 = new DailyData(false);
dd.setDataList(ald);
dd2.setDataList(ald);
dailyDataList.add(dd);
dailyDataList.add(dd2);
//Step #2
arrayAdapter = new DailyDataAdapter(this, R.layout.dailydata_item, dailyDataList);
但是,如果我在设置列表内容(这是我正在开发的应用程序的假装行为)之前将适配器与 listView 相关联,则不会显示任何内容。(切换步骤 1 和 2)。我添加的所有数据都是在之后调用方法notifyDataSetChanged进行的。