Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 是否需要为ArrayList中的对象创建自定义数组适配器类?_Android_Arraylist_Android Arrayadapter - Fatal编程技术网

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

Android 是否需要为ArrayList中的对象创建自定义数组适配器类?,android,arraylist,android-arrayadapter,Android,Arraylist,Android Arrayadapter,我有一个错误,它会说它无法解析构造函数数组适配器。正在考虑制作一个,但想确定是否有必要。我也在item类中重写了toString方法 public class Myfragment extends Fragment { @Override public View onCreateView( LayoutInflater inflater, ViewGroup container,

我有一个错误,它会说它无法解析构造函数数组适配器。正在考虑制作一个,但想确定是否有必要。我也在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);
}
}
公共类Myfragment扩展了片段{
@凌驾
创建视图上的公共视图(更平坦的充气机,
视图组容器,
Bundle savedInstanceState){
MyApplication MyArraylist;
ArrayAdapter ArrayAdapter;
MyItem item1=新的MyItem();
项目1.设置名称(“我的第一个家庭作业”);
项目1.设置等级(60);
项目1.设置类别(“家庭作业”);
MyArraylist=新的MyApplication();
MyArraylist.getItemsArrayList().add(item1);
arrayAdapter=新的arrayAdapter
(this,R.layout.Myview,MyArraylist);//这是构造函数
错误是
ListView v=(ListView)findViewById(R.id.MyXML);
v、 设置适配器(阵列适配器);
返回充气机。充气(右侧布局。列出碎片,容器,错误);
}
}
同样在ListView上,findViewById表示它无法解析该方法。我已经试了好几个小时了。有什么建议吗

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


    public ArrayList<MyItem> getMyArrayList() {

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



    }
公共类MyApplication扩展应用程序{
ArrayList MyArrayList;
公共ArrayList getMyArrayList(){
MyArrayList=新建ArrayList();
返回MyArrayList;
}
}

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

每次调用
getItemsArrayList()
以添加新项时,都会创建一个新的
ArrayList
,它将删除以前添加的项。这可以通过在
MyApplication
的构造函数中而不是在方法中创建ArrayList来解决

另外,您正在将
MyApplication
对象而不是
ArrayList
对象传递给
ArrayAdapter
构造函数。这可以通过添加一个新变量
myList
来解决,该变量引用
MyApplication
中的
ArrayList

下面是一些带有注释的代码来说明我的意思:

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);
  }
}
公共类Myfragment扩展了片段{
@凌驾
创建视图上的公共视图(更平坦的充气机,
视图组容器,
Bundle savedInstanceState){
MyApplication app;//从MyArraylist重命名
ArrayAdapter ArrayAdapter;
ArrayList myList;//这是引用列表的新变量
//创建应用程序并获取项目列表
app=新的MyApplication();
myList=app.getItemsArrayList();
//创建项目并添加到列表中
MyItem item1=新的MyItem();
项目1.设置名称(“名称”);
项目1.设置等级(60);
项目1.设置类别(“类型”);
添加(第1项);
//在构造函数中使用myList而不是app(从MyArraylist重命名)
arrayAdapter=新的arrayAdapter(this,R.layout.Myview,myList);
ListView v=(ListView)findViewById(R.id.MyXML);
v、 设置适配器(阵列适配器);
返回充气机。充气(右侧布局。列出碎片,容器,错误);
}
}
以及:

公共类MyApplication扩展应用程序{
私人ArrayList myArrayList;
公共应用程序(){
//在构造函数中创建列表
myArrayList=新建ArrayList();
}
公共ArrayList getMyArrayList(){
//返回已初始化的列表,而不是创建新列表
返回myArrayList;
}
}

最后,
findViewById()
方法只有在
片段
类子类或其他具有
findViewById()
方法的类时才有效。

这里发生的一些事情会阻碍您的进度

每次调用
getItemsArrayList()
以添加新项时,都会创建一个新的
ArrayList
,它将删除以前添加的项。这可以通过在
MyApplication
的构造函数中而不是在方法中创建ArrayList来解决

另外,您正在将
MyApplication
对象而不是
ArrayList
对象传递给
ArrayAdapter
构造函数。这可以通过添加一个新变量
myList
来解决,该变量引用
MyApplication
中的
ArrayList

下面是一些带有注释的代码来说明我的意思:

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);
  }
}
公共类Myfragment扩展了片段{
@凌驾
创建视图上的公共视图(更平坦的充气机,
视图组容器,
Bundle savedInstanceState){
MyApplication app;//从MyArraylist重命名
ArrayAdapter ArrayAdapter;
ArrayList myList;//这是引用列表的新变量
//创建应用程序并获取项目列表
app=新的MyApplication();
myList=app.getItemsArrayList();
//创建项目并添加到列表中
MyItem item1=新的MyItem();
项目1.设置名称(“名称”);
项目1.设置等级(60);
项目1.设置类别(“类型”);
添加(第1项);
//在构造函数中使用myList而不是app(从MyArraylist重命名)
arrayAdapter=新的arrayAdapter(this,R.layout.Myview,myList);
ListView v=(ListView)findViewById(R.id.MyXML);
v、 设置适配器(阵列适配器);
返回充气机。充气(右侧布局。列出碎片,容器,错误);
}
}
以及:

公共类MyApplication扩展应用程序{
私人ArrayList myArrayList;
公共应用程序(){
//在构造函数中创建列表
myArrayList=新建ArrayList();
}
公共ArrayList getMyArrayList(){
//返回已初始化的列表,而不是创建新列表
返回myArrayList;
}
}

最后,
findViewById()
方法仅在
片段
类子类或具有
findViewById()
方法的某个其他类时有效。

现有代码正试图传递
MyArraylist = new MyApplication();
MyArraylist.getItemsArrayList().add(item1);
arrayAdapter = new ArrayAdapter<MyItem>(this,R.layout.Myview,MyArraylist);
MyApplication myapp = new MyApplication();
ArrayList<MyItem> list = myapp.getItemsArrayList();
list.add(item1);
arrayAdapter = new ArrayAdapter<MyItem>(this, R.layout.Myview, list);