Populating a ListView using an ArrayList?
You need to do it through an ArrayAdapter
which will adapt your ArrayList (or any other collection) to your items in your layout (ListView, Spinner etc.).
This is what the Android developer guide says:
A
ListAdapter
that manages aListView
backed by an array of arbitrary objects. By default this class expects that the provided resource id references a singleTextView
. If you want to use a more complex layout, use the constructors that also takes a field id. That field id should reference aTextView
in the larger layout resource.However the
TextView
is referenced, it will be filled with thetoString()
of each object in the array. You can add lists or arrays of custom objects. Override thetoString()
method of your objects to determine what text will be displayed for the item in the list.To use something other than
TextViews
for the array display, for instanceImageViews
, or to have some of data besidestoString()
results fill the views, overridegetView(int, View, ViewGroup)
to return the type of view you want.
So your code should look like:
public class YourActivity extends Activity {
private ListView lv;
public void onCreate(Bundle saveInstanceState) {
setContentView(R.layout.your_layout);
lv = (ListView) findViewById(R.id.your_list_view_id);
// Instanciating an array list (you don't need to do this,
// you already have yours).
List<String> your_array_list = new ArrayList<String>();
your_array_list.add("foo");
your_array_list.add("bar");
// This is the array adapter, it takes the context of the activity as a
// first parameter, the type of list view as a second parameter and your
// array as a third parameter.
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
your_array_list );
lv.setAdapter(arrayAdapter);
}
}
How to populate listview in android from ArrayList?
For suppose listitems is the arraylist then you need to append your arraylist to listview as
//Declaration part
ArrayAdapter<String> adapter;
ArrayList<String> listItems=new ArrayList<String>();
lv =(ListView)findViewById(R.id.listView1);
//arraylist Append
adapter=new ArrayAdapter<String>(From.this,
android.R.layout.simple_list_item_1,
listItems);
lv.setAdapter(adapter);
Populate listview from arraylist of objects
In your activity
AdapterPerson adbPerson;
ArrayList<Person> myListItems = new ArrayList<Person>();
//then populate myListItems
adbPerson= new AdapterPerson (youractivity.this, 0, myListItems);
listview.setAdapter(adbPerson);
Adapter
public class AdapterPerson extends ArrayAdapter<Person> {
private Activity activity;
private ArrayList<Person> lPerson;
private static LayoutInflater inflater = null;
public AdapterPerson (Activity activity, int textViewResourceId,ArrayList<Person> _lPerson) {
super(activity, textViewResourceId, _lProducts);
try {
this.activity = activity;
this.lPerson = _lPerson;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
} catch (Exception e) {
}
}
public int getCount() {
return lPerson.size();
}
public Product getItem(Product position) {
return position;
}
public long getItemId(int position) {
return position;
}
public static class ViewHolder {
public TextView display_name;
public TextView display_number;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
final ViewHolder holder;
try {
if (convertView == null) {
vi = inflater.inflate(R.layout.yourlayout, null);
holder = new ViewHolder();
holder.display_name = (TextView) vi.findViewById(R.id.display_name);
holder.display_number = (TextView) vi.findViewById(R.id.display_number);
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
holder.display_name.setText(lProducts.get(position).name);
holder.display_number.setText(lProducts.get(position).number);
} catch (Exception e) {
}
return vi;
}
}
Populating a List View with two ArrayLists
//Replace your adapter code with this, I have check the size of Arraylist
//inside getCount() and inside getview() method verify value
//of position not greater than size of Arraylist.
public class customDecisionAdapter extends BaseAdapter {
Context context;
private ArrayList<String> list1;
private ArrayList<String> list2;
public customDecisionAdapter(Context context, ArrayList<String>list1, ArrayList<String>list2) {
this.context= context;
this.list1= list1;
this.list2= list2;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
View convertView = view;
if(convertView==null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.customedecisio,viewGroup,false);
}
TextView t1 = (TextView) convertView.findViewById(R.id.textDisease);
TextView t2 = (TextView) convertView.findViewById(R.id.textTotal);
// Verify value of position not greater than size of ArrayList.
if(position < list1.size())
t1.setText(list1.get(position));
if(position< list2.size())
t2.setText(list2.get(position));
return convertView;
}
@Override
public int getCount()
{
if(list1.size() < list2.size())
return list2.size();
else
return list1.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
}
Populating a ListView with an ArrayList - ListView doesn't populate
Your adapter doesn't know you've added new data here:
favouriteShopsList.add(new Shop(jsonObject.getInt("id"),jsonObject.getString("azienda"), false));
try calling notifydatasetchanged()
after you've added new data:
listAdapter.notifyDataSetChanged()
Related Topics
How to Access the Java Method in a C++ Application
What Is a Stringindexoutofboundsexception - How to Fix It
How to Parse Json Array (Not Json Object) in Android
Base 64 Encode and Decode Example Code
Java Can't Connect to X11 Window Server Using 'Localhost:10.0' as the Value of the Display Variable
Stringbuilder VS String Concatenation in Tostring() in Java
Converting Json Data to Java Object
What Causes Error "No Enclosing Instance of Type Foo Is Accessible" and How to Fix It
How Do Getters and Setters Work
Why Is It Bad Practice to Call System.Gc()
Is Short Circuit Evaluation Guaranteed in C++ as It Is in Java
How to Launch an Activity from Another Application in Android
The Asynctask API Is Deprecated in Android 11. What Are the Alternatives
How to Change the Decimal Separator of Decimalformat from Comma to Dot/Point
Manipulating an Access Database from Java Without Odbc