I am new to Android and I can’t find an answer to this that works.
I have the following adapter:
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();
}
}
Which inflates the following layout:
<?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>
Now, the problem is that, if I set the content of the list associated with the adapter prior to associating it with the listView where I pretend to show the information, it works well, as so:
//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);
But, if I associate the adapter to the listView prior to setting the content of the list (which is the pretended behaviour for the application I’m developing), nothing is shown. (Switching the Steps 1 and 2).
All the data I add is made calling the method notifyDataSetChanged afterwards.
Your custom ArrayAdapter will not automatically know that data has been added to
dailyDataList. You need to inform it that the list has changed. You should do this in your custom adapter too, which is whatArrayAdapterdoes:Now you can call
arrayAdapter.add(dd);and it will automatically update the ListView for you.Lastly since you have overridden the majority of ArrayAdapter and don’t call the super methods, you should simply extend BaseAdapter and override the last few methods as well.