Dynamically add items to list view using custom adapter for Android app

user1282637

So, right now I have a custom adapter class that takes in an array of Locations and adds them to a ListView. This is all fine and dandy, but I would like to add Locations to this listview after this initialization. For example, someone can "add a Location" and it will add it to this ListView. Here is my Main Activity:

package com.example.listviewtest;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

public class MainActivity extends Activity {

private ListView listView1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Location location_data[] = new Location[]
    {
        new Location(R.drawable.ic_launcher, "Location 1", "Fruit!", "2 miles", "8-4 mon-fri\nclosed sun"),
        new Location(R.drawable.ic_launcher, "Location 2", "Veggies!", "2 miles", "8-5"),
        new Location(R.drawable.ic_launcher, "Location 3", "Plants!", "2 miles", "8-5"),
        new Location(R.drawable.ic_launcher, "Location 4", "Flowers!", "2 miles", "8-5"),
        new Location(R.drawable.ic_launcher, "Location 5", "Baked Goods!", "2 miles", "8-5")
    };

   LocationAdapter adapter = new LocationAdapter(this, 
            R.layout.listview_item_row, location_data);

   //adapter.add(new Location(R.drawable.ic_launcher, "Location 6", "Veggies!", "2 miles", "8-5"));


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

    View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
    listView1.addHeaderView(header);

    listView1.setAdapter(adapter);
}
}

This works. I want to now do something like adapter.add(new Location(R.drawable.ic_launcher, "Location 6", "Veggies!", "2 miles", "8-5")); AFTER filling it with the array.

Here is my LocationAdapter class:

package com.example.listviewtest;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class LocationAdapter extends ArrayAdapter<Location>{

Context context; 
int layoutResourceId;    
Location data[] = null;

public LocationAdapter(Context context, int layoutResourceId, Location[] 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;
    LocationHolder holder = null;

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

        holder = new LocationHolder();
        holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
        holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
        holder.details = (TextView)row.findViewById(R.id.details);
        holder.distance = (TextView)row.findViewById(R.id.distance);
        holder.hours = (TextView)row.findViewById(R.id.hours);

        row.setTag(holder);
    }
    else
    {
        holder = (LocationHolder)row.getTag();
    }

    Location location = data[position];
    holder.txtTitle.setText(location.title);
    holder.imgIcon.setImageResource(location.icon);
    holder.details.setText(location.details);
    holder.distance.setText(location.distance);
    holder.hours.setText(location.hours);

    return row;
}

static class LocationHolder
{
    ImageView imgIcon;
    TextView txtTitle;
    TextView details;
    TextView distance;
    TextView hours;
}
}

Any ideas on how I can implement this? Thanks.

LordRaydenMK
  1. In your adapter change the Locations data[] from array to ArrayList<Location> and override the appropriate constructor
  2. In your activity, make your variable data a field (type ArrayList<Location>)
  3. When you add a location you can use data.add(location)
  4. Then you can call notifyDatasetChanged() on your adapter

Example code.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Android list view using custom adapter and view holder

From Dev

android custom List Adapter not displaying items

From Dev

How to dynamically set imageView source for a List View from Custom Adapter?

From Dev

Implement list view using adapter for chatpage in android

From Dev

Android getText from dynamically created adapter list view

From Dev

Get list of items in custom adapter

From Dev

Android app, dynamically add custom component

From Dev

Custom Adapter with Base adapter list items repeating

From Dev

Custom Adapter with Base adapter list items repeating

From Dev

ArrayList Index Out Of Bound exception in android custom list view adapter

From Dev

Using an external xml file for custom list view adapter?

From Dev

Editing list view items dynamically when using cursor adapters and loaders

From Dev

Editing list view items dynamically when using cursor adapters and loaders

From Dev

Custom text view adapter not working on list view

From Dev

Custom text view adapter not working on list view

From Dev

Dynamically add textviews to custom adapter from ArrayList

From Dev

Adding Items to List<int> By Index Dynamically without using .Add()

From Dev

Android: How to dynamically add a "Custom View" into a linear layout

From Dev

How to I add an image to list view items in Android?

From Dev

add icons in dynamic list view using android

From Dev

Cannot resolve layout in adapter for custom list view

From Dev

How to create custom array adapter when items defined dynamically

From Dev

How to dynamically add ImageView to layout using Adapter

From Dev

Custom adapter for a list of items that have multiple child items?

From Dev

Android Custom Adapter - Won't return view?

From Dev

Android Grid View with Custom base Adapter

From Dev

Android Custom Adapter - Won't return view?

From Dev

Android App stopped when custom list view calls

From Dev

I have to Display Contacts info (Name, Number, Image) of all contacts in a List View, using custom adapter which extends base adapter

Related Related

  1. 1

    Android list view using custom adapter and view holder

  2. 2

    android custom List Adapter not displaying items

  3. 3

    How to dynamically set imageView source for a List View from Custom Adapter?

  4. 4

    Implement list view using adapter for chatpage in android

  5. 5

    Android getText from dynamically created adapter list view

  6. 6

    Get list of items in custom adapter

  7. 7

    Android app, dynamically add custom component

  8. 8

    Custom Adapter with Base adapter list items repeating

  9. 9

    Custom Adapter with Base adapter list items repeating

  10. 10

    ArrayList Index Out Of Bound exception in android custom list view adapter

  11. 11

    Using an external xml file for custom list view adapter?

  12. 12

    Editing list view items dynamically when using cursor adapters and loaders

  13. 13

    Editing list view items dynamically when using cursor adapters and loaders

  14. 14

    Custom text view adapter not working on list view

  15. 15

    Custom text view adapter not working on list view

  16. 16

    Dynamically add textviews to custom adapter from ArrayList

  17. 17

    Adding Items to List<int> By Index Dynamically without using .Add()

  18. 18

    Android: How to dynamically add a "Custom View" into a linear layout

  19. 19

    How to I add an image to list view items in Android?

  20. 20

    add icons in dynamic list view using android

  21. 21

    Cannot resolve layout in adapter for custom list view

  22. 22

    How to create custom array adapter when items defined dynamically

  23. 23

    How to dynamically add ImageView to layout using Adapter

  24. 24

    Custom adapter for a list of items that have multiple child items?

  25. 25

    Android Custom Adapter - Won't return view?

  26. 26

    Android Grid View with Custom base Adapter

  27. 27

    Android Custom Adapter - Won't return view?

  28. 28

    Android App stopped when custom list view calls

  29. 29

    I have to Display Contacts info (Name, Number, Image) of all contacts in a List View, using custom adapter which extends base adapter

HotTag

Archive