gpt4 book ai didi

java - 在Android中向ArrayList添加项目时出错

转载 作者:行者123 更新时间:2023-12-02 09:26:52 24 4
gpt4 key购买 nike

错误:-

 cannot resolve symbol add
cannot resolve symbol setAdapter

我已经导入了ArrayList和ListView

当我运行该应用程序时,它显示您的文件有错误,因此无法运行它。

package com.example.listviewdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
ListView myListView = (ListView)findViewById(R.id.myListView);
ArrayList<String> myFamily = new ArrayList<String>();
myFamily.add("Neha");
myFamily.add("Ishaan");

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myFamily);
myListView.setAdapter(arrayAdapter);

}

最佳答案

您的代码位于 oncreate 之外,并且没有从任何地方调用。就这样做。

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

ListView myListView = (ListView)findViewById(R.id.myListView);
ArrayList<String> myFamily = new ArrayList<String>();
myFamily.add("Neha");
myFamily.add("Ishaan");

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myFamily);
myListView.setAdapter(arrayAdapter);
}

关于java - 在Android中向ArrayList添加项目时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58285257/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com