猿问

我的 ArrayList 中的数据重复了数组最后输入的数据。这是什么原因?

当我使用ArrayList自定义适配器在列表视图中列出图像时,代码没有显示任何错误。问题是,在使用代码时,只有我最后输入的数据在列表视图中显示。为什么不能查看之前输入的数据?


我的活动课:


public class SocialActivity extends AppCompatActivity {


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_social);

        String json;

        ImageView babu = (ImageView) findViewById(R.id.babu);

        ListView lvsoc = (ListView) findViewById(R.id.list_social);

        LinearLayout l1 = (LinearLayout) findViewById(R.id.layhead);

        LayoutInflater inflater = null;

        ArrayList<SubjectData> arrayList = new ArrayList<SubjectData>();

        ArrayAdapter<SubjectData> a;

        /*ArrayList<String> allcategory = new ArrayList<>();

        ArrayList<String> allcategory1 = new ArrayList<>();

        ArrayList<String> img = new ArrayList<>();

        ArrayList<String> img1 = new ArrayList<>();*/

        arrayList.add(new SubjectData("JAVA", "https://www.tutorialspoint.com/java/", "https://s18955.pcdn.co/wp-content/uploads/2017/11/Facebook-share-icon.png"));

        arrayList.add(new SubjectData("Python", "https://www.tutorialspoint.com/python/", "https://www.tutorialspoint.com/python/images/python-mini.jpg"));

        /*arrayList.add(new SubjectData("Javascript", "https://www.tutorialspoint.com/javascript/", "https://www.tutorialspoint.com/javascript/images/javascript-mini-logo.jpg"));

        arrayList.add(new SubjectData("Cprogramming", "https://www.tutorialspoint.com/cprogramming/", "https://www.tutorialspoint.com/cprogramming/images/c-mini-logo.jpg"));

        arrayList.add(new SubjectData("Cplusplus", "https://www.tutorialspoint.com/cplusplus/", "https://www.tutorialspoint.com/cplusplus/images/cpp-mini-logo.jpg"));


森栏
浏览 89回答 1
1回答

DIEA

您SubjectData拥有所有静态字段。所以每个实例SubjectData都有相同的信息。反而:public class SubjectData {&nbsp; &nbsp; public String SubjectName;&nbsp; &nbsp; public String Link;&nbsp; &nbsp; public String Image;&nbsp; &nbsp; public SubjectData(String subjectName, String link, String image) {&nbsp; &nbsp; &nbsp; &nbsp; this.SubjectName = subjectName;&nbsp; &nbsp; &nbsp; &nbsp; this.Link = link;&nbsp; &nbsp; &nbsp; &nbsp; this.Image = image;&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答