T.dog T.dog - 5 months ago 11
Java Question

ListView does not refresh

I'd like to create a ListView and add items dynamically. Here is my code:

CustomAdapter.java:

public class CustomAdapter extends ArrayAdapter<Item>{

Context context;
int layoutResourceId;
LinearLayout linearMain;
ArrayList<Item> data = new ArrayList<Item>();

public CustomAdapter(Context context, int layoutResourceId,
ArrayList<Item> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;

if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);

linearMain = (LinearLayout) row.findViewById(R.id.lineraMain);

Item myImage = data.get(position);

TextView label = new TextView(context);
label.setText(myImage.name);
linearMain.addView(label);

ImageView image = new ImageView(context);
int outImage = myImage.image;
image.setImageResource(outImage);
linearMain.addView(image);
}
return row;
}
}


Item.java:

public class Item {

int image;
String name;

public Item(int image, String name) {
super();
this.image = image;
this.name = name;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}


GallerF.java

ArrayList<Item> imageArry = new ArrayList<Item>();
CustomAdapter adapter;
ListView dataList;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_gallery, parent, false);
dataList = (ListView) v.findViewById(R.id.lista);

return v;
}
.....
public void CreateNewListRow(){
imageArry.add(new Item(R.drawable.ek_logo,name));
adapter = new CustomAdapter(getActivity(),R.layout.list,imageArry);
dataList.setAdapter(adapter);
adapter.notifyDataSetChanged();
}


list.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lineraMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp" >
</LinearLayout>


activity_gallery.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="hu.redbuttonebb.endi.fragmentek20.GalleryF"
android:background="@drawable/background"
android:id="@+id/linlayout">

<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<ListView
android:id="@+id/lista"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false" />

</ScrollView>
</LinearLayout>


Now when I run the app it's create one row with the correct picture and name. But when I'd like to create one more then nothing happen. Where do I do it wrong?

Drv Drv
Answer

Replace your file with this:

ArrayList<Item> imageArry = new ArrayList<Item>();
CustomAdapter adapter;
ListView dataList;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_gallery, parent, false);
dataList = (ListView) v.findViewById(R.id.lista);

adapter = new CustomAdapter(getActivity(),R.layout.list,imageArry);
dataList.setAdapter(adapter);

return v;
}
.....

public void CreateNewListRow(){
imageArry.add(new Item(R.drawable.ek_logo,name));
imageArry.add(new Item(R.drawable.ek_logo,name));
imageArry.add(new Item(R.drawable.ek_logo,name));
imageArry.add(new Item(R.drawable.ek_logo,name));
imageArry.add(new Item(R.drawable.ek_logo,name));
adapter.notifyDataSetChanged();
}

Edit your list.xml with below code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lineraMain"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dp" >
</LinearLayout>

Remove ScrollView, ListView do not require ScrollView:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      tools:context="hu.redbuttonebb.endi.fragmentek20.GalleryF"
      android:background="@drawable/background"
      android:id="@+id/linlayout">
<ListView
    android:id="@+id/lista"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:drawSelectorOnTop="false" />

</LinearLayout>
Comments