Populate ListView from List<Object>

20,205

Solution 1

Use the below. You can use a for loop and populate your ingredientsList. The below is just a example

List<Ingredient> ingredientsList= new ArrayList<Ingredient>(); 
Ingredient i= new Ingredient("foo");   
ingredientsList.add(i);
Ingredient i1= new Ingredient("bar");   
ingredientsList.add(i1);

Then

 ListView lv = (ListView) findViewById(R.id.listview);
 // initialize listview
 lv.setAdpater(new CustomAdapterArrayAdapter(ActivityName.this,ingredientsList));
 // set the custom adapter to listview

You can use a CustomAdapterArrayAdapter inflate a custom layout

public class CustomAarrayAdapter extends ArrayAdapter
{

List<Ingredient> ingredientsList;
public CustomArrayAdapter(Context context, List<Ingredient> list)
{
   super(context,0,list);
   ingredientList = list;
}

@Override 
public View getView(int position, View convertView, ViewGroup parent) {  
ViewHolder holder; 

if (convertView == null) { 
convertView = mInflater.inflate(R.layout.row,parent,false);
// inflate custom layout called row 
holder = new ViewHolder();
holder.tv =(TextView) convertView.findViewById(R.is.textView1);  
// initialize textview
convertView.setTag(holder);
}
else
{
      holder = (ViewHolder)convertView.getTag();
}
      Ingredient in = (Ingredient)ingredientsList.get(position);
      holder.tv.setText(in.name); 
      // set the name to the text;

return convertView;

}

static class ViewHolder
{

   TextView tv;
} 
}

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

ViewHolder is for smooth scrolling and performance

row.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="28dp"
        android:text="TextView" />

 </RelativeLayout>

Edit:

Without using custom adapter

class Ingredient {
    public int id;
    public String name;

    public Ingredient(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return this.name.toString();
    }

}

Then

public class MainActivity extends Activity {

List<Ingredient> ingredientsList= new ArrayList<Ingredient>(); 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    for(int i=0;i<10;i++)
    {
        ingredientsList.add(new Ingredient("foo"+i));
    }
   ArrayAdapter<Ingredient> adapter = new ArrayAdapter<Ingredient>(this,android.R.layout.simple_list_item_1,ingredientsList);
   ListView lv= (ListView) findViewById(R.id.listView1);
   lv.setAdapter(adapter);
  }
  }

Then

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" >
    </ListView>
</RelativeLayout>

Snap

enter image description here

Solution 2

Extend the array adapter class:

public class MyAdapter extends ArrayAdapter<Ingredient> {

    Activity activity;

    public MyAdapter(Activity context, int resource,
            List<Ingredient> objects) {
        super(context, resource, objects);
        this.activity = context;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        Ingredient currentIngredient = getItem(position);

                //Do Something
              ....
        }
Share:
20,205
smotru
Author by

smotru

Updated on January 03, 2020

Comments

  • smotru
    smotru over 4 years

    Basically I have a list of objects of class Ingredient which looks like this:

    class Ingredient {
        public int id;
        public String name;
    
        public Ingredient(String name) {
            this.name = name;
        }
    }
    

    so each object from list have a name.

    Now to use ArrayAdapter I need to have a List of strings filled with names. Is there any way to use ArrayAdapter with my list of Ingredients? This is how it looks right now

    List<Ingredient> ingredientsList= new ArrayList<Ingredient>();
    ingredientsList.add(new Ingredient("foo"));
    ingredientsList.add(new Ingredient("bar"));