Adding ArrayList Objects to GridView in Android

Pjayness

Hello I'm working on a project where I have to insert each object of type "Person" in an ArrayList of type "Person" to a Gridview in eclipse android. My intentions are to display a picture along with the Person's name (similar to Instagram). Here's what I have tried so far but this doesn't seem to work as I want it to and hard to understand.

Do yall have any better solutions?

ArrayList<Person> dbPersons = dop.getPersons(dop); //This is where I populate my list
int[] imageId = {
            R.drawable.image1,
            R.drawable.image2,
            R.drawable.image3
    };

            gridView = (GridView)findViewById(R.id.grid);
            gridView.setAdapter(new function.CustomGrid(this, dbPersons, imageId));
            gridView.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    // TODO Auto-generated method stub

                }

            });

My Person's class generally contains:

private String FName;
private String LName;
private String Biography;

Anyway I didn't want to bombard this post with my faulty code since I'm looking for a cleaner and better alternative. I just want to display the name as the gridview item's title and the picture below it and do the same for the rest of the objects in the arraylist. Could you please help me out guys :)

Elltz

your adapter

 public class Benildus_Adapter extends ArrayAdapter<Person> {

ArrayList<Person> list; // your person arraylist
Context context; // the activity context
int resource; // this will be your xml file

public Benildus_Adapter(Context context, int resource,ArrayList<Person> objects) {
    super(context, resource, objects);
    // TODO Auto-generated constructor stub
    this.list = objects;
    this.context = context;
    this.resource = resource;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    if(list.size() == 0){
        return 0;
    }else{
        return list.size();
    }
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View child = convertView;
    RecordHolder holder;
    LayoutInflater inflater = ((Activity) context).getLayoutInflater(); // inflating your xml layout

    if (child == null) {            
        child = inflater.inflate(resource, parent, false);
        holder = new RecordHolder();
        holder.fname = (TextView) child.findViewById(R.id.fname); // fname is the reference to a textview
        holder.lname = (TextView) child.findViewById(R.id.lname); // in your xml layout file 
        holder.bio =(TextView) child.findViewById(R.id.bio); // you are inflating.etc
        child.setTag(holder);
    }else{
        holder = (RecordHolder) child.getTag();
    }

    final Person user = list.get(position); // you can remove the final modifieer.

    holder.fname.setText(user.getFName());      
    holder.lname.setText(user.getLName());
    holder.bio.setText(user.getBiography());
    holder.image.setImageBitmap(user.getImage()); // if you use string then you download the image using
    // the string as url and set it to your imageview..
    return child;
}

static class RecordHolder {
    TextView fname,lname,bio;
    ImageView image;    
}


@Override
public void notifyDataSetChanged() { // you can remove this..
    // TODO Auto-generated method stub      
    if(getCount() == 0){
        //show layout or something that notifies that no list is in..
    }else{
        // this is to make sure that you can call notifyDataSetChanged in any place and any thread
        new Handler(getContext().getMainLooper()).post(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                Benildus_Adapter.super.notifyDataSetChanged();
            }
        });
    }
}

} 

your person class

 public class Person {

private String FName;
private String LName;
private String Biography;
private Bitmap image; // add the image too to your class, you can store the url of the image 
// or save it using bitmap.. if you store the url then = String image; the load the image
// in the getview method.. any way you choose..
public String getFName() {
    return FName;
}
public void setFName(String fName) {
    FName = fName;
}
public String getLName() {
    return LName;
}
public void setLName(String lName) {
    LName = lName;
}
public String getBiography() {
    return Biography;
}
public void setBiography(String biography) {
    Biography = biography;
}
public Bitmap getImage() {
    return image;
}
public void setImage(Bitmap image) {
    this.image = image;
}

}

Edit i just forget the set adapter..

 gridView = (GridView)findViewById(R.id.grid);
 Benildus_Adapter bA = new Benildus_Adapter(this, R.layout.myxml,dbPersons);
 gridView.setAdapter(bA);

hope it helps, let me know

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Adding ArrayList Objects to GridView in Android

From Dev

How to display ArrayList Objects in GridView in Android in fragment

From Dev

arraylist of objects, adding objects

From Dev

ArrayList not adding objects?

From Dev

Adding objects in ArrayList in iteration

From Dev

Android ArrayList of String objects size remains at 0 despite adding to it

From Dev

Adding integer arraylist to arraylist in android

From Dev

Adding new objects to an ArrayList in constructor

From Dev

Mockito: mocking objects and adding to ArrayList

From Dev

Adding new objects to an ArrayList in constructor

From Dev

Adding objects from Jsoup to an ArrayList

From Dev

How to display arraylist in gridview in android

From Dev

Adding objects to ArrayList from another class

From Dev

Adding objects to an ArrayList from another class

From Dev

Adding objects to ArrayList from another class

From Dev

ArrayList adding objects - no suitable method found for add

From Dev

Adding objects to an ArrayList from another class

From Dev

What name to give to objects when adding to ArrayList

From Dev

Android: Redraw a GridView after adding a child

From Dev

Adding more elements to a GridView using adapters in Android

From Dev

Populating a CardView with an ArrayList of Objects android

From Dev

java - Adding of cursor values into an ArrayList in Android Eclipse

From Dev

Android adding and removing to a different ListView using ArrayList

From Dev

NullPointerException when adding an object to ArrayList in Android

From Dev

ArrayList adding item error Android (Eclipse)

From Dev

Android adding and removing to a different ListView using ArrayList

From Dev

How to prevent adding item to ArrayList (Android)

From Dev

Java - How to iterate through an objects arraylist and adding elements that meet a certain condition to another objects arraylist

From Dev

how to clear the ArrayList after adding that ArrayList to HashMap in Android?

Related Related

  1. 1

    Adding ArrayList Objects to GridView in Android

  2. 2

    How to display ArrayList Objects in GridView in Android in fragment

  3. 3

    arraylist of objects, adding objects

  4. 4

    ArrayList not adding objects?

  5. 5

    Adding objects in ArrayList in iteration

  6. 6

    Android ArrayList of String objects size remains at 0 despite adding to it

  7. 7

    Adding integer arraylist to arraylist in android

  8. 8

    Adding new objects to an ArrayList in constructor

  9. 9

    Mockito: mocking objects and adding to ArrayList

  10. 10

    Adding new objects to an ArrayList in constructor

  11. 11

    Adding objects from Jsoup to an ArrayList

  12. 12

    How to display arraylist in gridview in android

  13. 13

    Adding objects to ArrayList from another class

  14. 14

    Adding objects to an ArrayList from another class

  15. 15

    Adding objects to ArrayList from another class

  16. 16

    ArrayList adding objects - no suitable method found for add

  17. 17

    Adding objects to an ArrayList from another class

  18. 18

    What name to give to objects when adding to ArrayList

  19. 19

    Android: Redraw a GridView after adding a child

  20. 20

    Adding more elements to a GridView using adapters in Android

  21. 21

    Populating a CardView with an ArrayList of Objects android

  22. 22

    java - Adding of cursor values into an ArrayList in Android Eclipse

  23. 23

    Android adding and removing to a different ListView using ArrayList

  24. 24

    NullPointerException when adding an object to ArrayList in Android

  25. 25

    ArrayList adding item error Android (Eclipse)

  26. 26

    Android adding and removing to a different ListView using ArrayList

  27. 27

    How to prevent adding item to ArrayList (Android)

  28. 28

    Java - How to iterate through an objects arraylist and adding elements that meet a certain condition to another objects arraylist

  29. 29

    how to clear the ArrayList after adding that ArrayList to HashMap in Android?

HotTag

Archive