是否需要为ArrayList中的对象创建自定义数组适配器类?

时间:2016-04-05 03:20:56

标签: android arraylist android-arrayadapter

我有这个错误,它会说它无法解析构造函数Array Adapter。考虑制作一个但想确定是否有必要。我也确实覆盖了item类中的toString方法。

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);
}
}

同样在ListView上findViewById说它无法解析方法。已经好几个小时了。有什么建议吗?

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


    public ArrayList<MyItem> getMyArrayList() {

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



    }

2 个答案:

答案 0 :(得分:1)

这里发生的一些事情会阻碍你的进步。

每当您致电getItemsArrayList()添加新项目时,系统会创建一个新的ArrayList,这将清除您之前添加的项目。这可以通过在MyApplication的构造函数中创建ArrayList而不是在方法中来修复。

此外,您将MyApplication对象而不是ArrayList<MyItem>对象传递给ArrayAdapter构造函数。这可以通过添加引用myList中的ArrayList的新变量MyApplication来解决。

这里有一些带注释的代码来表明我的意思:

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);
  }
}

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;
  }
}

最后,findViewById()方法仅适用于您的Fragment类子类Activity或其他具有findViewById()方法的类。

答案 1 :(得分:1)

您现有的代码正在尝试将MyAppliction对象传递给ListAdapter构造函数。这将无法编译,因为MyArraylist的类型为MyApplication:

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

相反,你的意思是将实际的ArrayList对象从MyApplication内部传递给像这样的构造函数吗?

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