Search code examples
androidandroid-listviewjavabeanslistitem

Write item in a ListView Android


I'm developing an app. In my app I'm stored some information in a class named Promo in which I defined 4 strings and I made the 4 getter and setters. Then I made another class named Promotions in which I defined an array of Promo. To store data in the Promo model I'm doing so:

 Promo promo = new Promo();
            promo.setName("Promozione");
            promo.setDescription("La promozione può essere spesa");
            promo.setDate("29 Lug 2013");
            promo.setImgUrl("http://www.pensando.it/wp/wp-content/uploads/2012/11/coupon.jpg");

Now in another intent I will to show the field name in a ListView and when I click on the item of this ListView I will load another intent in which I will show the details of this promo. To write the name in the ListView I made a method in which I want to read the data from the array Promo and I will to display the name of promotions in my list item. To do that I made a ListAdapter and I pass it the class and the array of promotions, but it says me that I should initialized the array promotions. I will post here the code I'm trying to use to display this informations:

Method loadDataFromModel

private void loadDataFromModel() {

    lvTitle = (ListView) findViewById(R.id.lvTitle);

    final Promotions[] promotions;

    ListAdapter laTitle = new ListAdapter(ArchiveActivity2.class, promotions);
    lvTitle.setAdapter(laTitle);

    lvTitle.setOnClickListener(new AdapterView.OnItemClickListener() {

    });


}

ListAdapter.java

import android.content.Context;
import android.widget.ArrayAdapter;

import it.bitmama.InteracTv.R;
import it.bitmama.InteracTv.model.Promotions;

public class ListAdapter extends ArrayAdapter<Promotions> {
    public  ListAdapter(Context context, Promotions[] promotions) {
        super(context, R.layout.list_item, R.id.tvTitle, promotions);
    }
}

I will post too the two class that I'm using like models:

Promo.java

public class Promo {
    String name;
    String description;
    String imgUrl;
    String date;

    @Override
    public String toString() {
        return name.toString();
    }

    public String getName() {
        return name;
    }

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

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getImgUrl() {
        return imgUrl;
    }

    public void setImgUrl(String imgUrl) {
        this.imgUrl = imgUrl;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }
}

Promotions.java

public class Promotions {
    public Promo[] promotions;
}

Now I hope you know what's I'm trying to do, I need your help to display correctly this informations. Thank you for the suggestion


Solution

    1. create adapter:

      private ArrayAdapter mAdapter;

      private void loadDataFromModel() {

      lvTitle = (ListView) findViewById(R.id.lvTitle);
      ArrayList<Promo> list = new ArrayList<Promo>();
      
      
      ArrayAdapter mAdapter = new ArrayAdapter(getContext() , R.id.lvTitle, list);
      lvTitle.setAdapter(mAdapter);
      
      lvTitle.setOnItemClickListener(new AdapterView.OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
      
          }
      });
      

      }

    2. modify members of adapter:

      void onNewMember(Promo promo) {

      mAdapter.add(promo);
      

      }

    You may set members of array list before injection in loadDataFromModel()

    Promo promo = new Promo();
    ... set properties as wanted
    list.add(promo)