Itβs normal that it does not update, you add an item to βlistaβ, but the adapter saves its own copy of this list, so either you set the list in the adapter again and then call notifyDataChanged or add a new adapter element.
Anyway, I see a couple of strange things, I could compile everything using an array adapter, you do not need to add add, etc. I wrote the code by simply adding yours:
public class WeatherAppActivity extends ListActivity { Button buton; ItemsAdapter lista; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); List<String> initialList = new ArrayList<String>(); initialList.add("Bucuresti"); initialList.add("Sibiu"); lista=new ItemsAdapter(this, initialList); buton=(Button)findViewById(R.id.button1); buton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { lista.add(""+System.currentTimeMillis());
This is the xml that I used:
main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="63dp" android:text="Button" /> <ListView android:id="@id/android:list" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" > </ListView> </RelativeLayout>
lista.xml
<?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/elementLista" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Medium Text" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout>
This is the adapter version using the base adapter:
class ItemsBaseAdapter extends BaseAdapter { private List<String> items; private Context mContext; public ItemsBaseAdapter(Context context, List<String> list) { items = list; mContext = context; } public void addItem(String str) { items.add(str); } @Override public View getView(final int position, View row, final ViewGroup parent) { final String item = (String) getItem(position); ItemWrapper wrapper = null; if (row == null) { row = getLayoutInflater().inflate(R.layout.lista, parent, false); wrapper = new ItemWrapper(row); row.setTag(wrapper); } else { wrapper = (ItemWrapper) row.getTag(); } wrapper.refreshData(item); return row; } class ItemWrapper { TextView text; public ItemWrapper(View row) { text = (TextView) row.findViewById(R.id.elementLista); } public void refreshData(String item) { text.setText(item); } } @Override public int getCount() { return items.size(); } @Override public Object getItem(int position) { return items.get(position); } @Override public long getItemId(int position) {
And this is the version of the list item, which also includes the image on the left:
<?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="horizontal" > <ImageView android:layout_height="wrap_content" android:src="@android:drawable/btn_star_big_on" android:scaleType="fitCenter" android:layout_width="wrap_content" /> <TextView android:id="@+id/elementLista" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Medium Text" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout>