Question

This is the first time I am building a Listview with a custom layout, so in case I have missed something obvious please just point it out.

The problem I am having that I cannot get the listview to update itself with new information after the Oncreate(); has been used. So the list is very static.

I am trying to create a custom listview adapter that looks as such:

public class MainListCustomBaseAdapter extends BaseAdapter {

static ArrayList<ListItems> DataSomething;
static Context Cont;

public MainListCustomBaseAdapter (ArrayList<ListItems> data, Context c){
    DataSomething = data;
    Cont = c;
}

public int getCount() {
    // TODO Auto-generated method stub
    return DataSomething.size();
}

public Object getItem(int position) {
    // TODO Auto-generated method stub
    return DataSomething.get(position);
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
     View v = convertView;
     if (v == null)
     {
        LayoutInflater vi = (LayoutInflater)Cont.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.mainlistlayout, null);
     }

       ImageView image = (ImageView) v.findViewById(R.id.ListImage);
       TextView titleView = (TextView)v.findViewById(R.id.title);
       TextView DetailItemView = (TextView)v.findViewById(R.id.DetailItem);


       ListItems msg = DataSomething.get(position);
       image.setImageResource(msg.icon);
       titleView.setText(msg.title);
       DetailItemView.setText("ItemDetails: "+msg.ItemDetails);


    return v;
}

public void updateResults(ArrayList<MainListCustomBaseAdapter> results){
    notifyDataSetChanged();


}


}

My Oncreate looks like this:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


   RecipeList = (ListView) findViewById(R.id.mainListView);

    ShoppingItems = new ArrayList<ListItems>();
    RecipeList.setAdapter(new MainListCustomBaseAdapter(ShoppingItems, this));

    ListItems Detail;
    Detail = new ListItems();
    Detail.setIcon(R.drawable.food);
    Detail.setName("Food Stuff");
    Detail.setItemDetails("ItemDetailsComp");
    ShoppingItems.add(Detail);

}

and listitem looks like this:

public class ListItems {
        public int icon ;
        public String title;
        public String ItemDetails;


        public String getName() {
            return title;
        }

        public void setName(String from) {
            this.title = from;
        }

        public String getItemDetails() {
            return ItemDetails;
        }

        public void setItemDetails(String ItemDetailsComp) {
            this.ItemDetails = ItemDetailsComp;
        }

        public int getIcon() {
            return icon;
        }

        public void setIcon(int icon) {
            this.icon = icon;
        }

}

How do I get the listview to update dynamically? with maybe a SetInvalidatedViews() or notifyDatasetchanged()?

Any help is deeply appreciated.

Was it helpful?

Solution

Use the below

   MainListCustomBaseAdapter adapter = new MainListCustomBaseAdapter(ShoppingItems, this)
   RecipeList.setAdapter(adapter);

To refresh or update listview

   adapter.notifyDataSetChanged();

public void notifyDataSetChanged ()

Added in API level 1

Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.

OTHER TIPS

put this line after adding the element in the arraylist

RecipeList.setAdapter(new MainListCustomBaseAdapter(ShoppingItems, this));

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top