Carlitos
Carlitos

Reputation: 419

Is it Necessary to create a custom array adapter class for Objects in an ArrayList?

I had this error where it would say it cannot resolve constructor Array Adapter. Thinking about making one but wanted to make sure if it was necessary. I did override the toString method in the item class as well.

public class Myfragment extends Fragment {

@Override
public View onCreateView(   LayoutInflater inflater,
                             ViewGroup container,
                             Bundle savedInstanceState) {

    MyApplication MyArraylist;


    ArrayAdapter <MyItem> arrayAdapter;

    MyItem item1 = new MyItem();
    item1.setName("MyFirstHomework");
    item1.setGrade(60);
    item1.setCategory("Homework");

    MyArraylist = new MyApplication();
    MyArraylist.getItemsArrayList().add(item1);





    arrayAdapter = new ArrayAdapter<MyItem>
    (this,R.layout.Myview,MyArraylist);  //THIS IS WHERE THE CONSTRUCTOR       
    ERROR IS


    ListView v = (ListView) findViewById(R.id.MyXML); 
    v.setAdapter(arrayAdapter);




    return inflater.inflate(R.layout.list_fragment,container, false);
}
}

Also on ListView findViewById says it cannot resolve method. Been trying for hours now. Any Suggestions?

public class MyApplication extends Application {
    ArrayList<MyItem> MyArrayList;


    public ArrayList<MyItem> getMyArrayList() {

            MyArrayList = new ArrayList<MyItem>();
            return MyArrayList;
    }



    }

Upvotes: 0

Views: 169

Answers (2)

br3nt
br3nt

Reputation: 9596

A couple of things happening here that will block your progress.

Each time you call getItemsArrayList() to add a new item, a new ArrayList is created which is going to wipe out the previous items you have added. This can be fixed by creating the ArrayList in the constructor of MyApplication rather than in the method.

Also, you are passing the MyApplication object rather than the ArrayList<MyItem> object to the ArrayAdapter constructor. This can be resolved by adding a new variable, myList that references the ArrayList in MyApplication.

Here's some code with comments to show what I mean:

public class Myfragment extends Fragment {

  @Override
  public View onCreateView(   LayoutInflater inflater,
                               ViewGroup container,
                               Bundle savedInstanceState) {

    MyApplication app; // renamed from MyArraylist
    ArrayAdapter <MyItem> arrayAdapter;
    ArrayList<MyItem> myList; // this is a new variable to reference the list

    // create application and get the items list
    app = new MyApplication();
    myList = app.getItemsArrayList();

    // create items and add to list
    MyItem item1 = new MyItem();
    item1.setName("Title");
    item1.setGrade(60);
    item1.setCategory("Type");
    myList.add(item1);

    // use myList instead of app (renamed from MyArraylist) in the constructor
    arrayAdapter = new ArrayAdapter<MyItem>(this, R.layout.Myview, myList); 

    ListView v = (ListView) findViewById(R.id.MyXML); 
    v.setAdapter(arrayAdapter);

    return inflater.inflate(R.layout.list_fragment,container, false);
  }
}

And:

public class MyApplication extends Application {
  private ArrayList<MyItem> myArrayList;

  public MyApplication() {
    // create the list in the constructor
    myArrayList = new ArrayList<MyItem>();
  }

  public ArrayList<MyItem> getMyArrayList() {
    // return the already initialized list rather than creating a new one
    return myArrayList;
  }
}

Lastly, findViewById() method will only work if your Fragment class subclasses Activity or some other class that has a findViewById() method.

Upvotes: 1

Doug Stevenson
Doug Stevenson

Reputation: 317758

Your existing code is trying to pass a MyAppliction object to the ListAdapter constructor. This won't compile because MyArraylist is of type MyApplication:

MyArraylist = new MyApplication();
MyArraylist.getItemsArrayList().add(item1);
arrayAdapter = new ArrayAdapter<MyItem>(this,R.layout.Myview,MyArraylist);

Instead, did you mean to pass the actual ArrayList object from inside MyApplication to the constructor like this?

MyApplication myapp = new MyApplication();
ArrayList<MyItem> list = myapp.getItemsArrayList();
list.add(item1);
arrayAdapter = new ArrayAdapter<MyItem>(this, R.layout.Myview, list);

Upvotes: 1

Related Questions