HashMap重复了Android Studio中的最后一个元素

时间:2018-07-05 19:35:36

标签: java android android-studio hashmap

我正在使用HashMap来获取列表视图适配器的值。 问题是重复了我的HashMap的最后两个元素。 发布此问题之前,我尝试了不同的解决方案。 谢谢您的帮助。

这是我的HashMap的代码

org.slf4j:slf4j-log4j12:1.7.25

这是我的CustomListView适配器的代码。

CustomListView适配器

final String[] bookTitles = new String[]{
            "android-iconify",
            "Calligraphy",
            "SmartTabLayout",
            "android-viewpager-transformers",
            "Android-Rate",
            "FloatingActionButton",
            "TapTargetView",
            "MaterialDrawer"

    };
    final String[] authors = new String[]{

            "Version 2.2.2",
            "Version 2.2.0",
            "Version 1.6.1",
            "Version 1.0.1",
            "Version 1.0.1",
            "Version 1.6.4",
            "Version 1.11.0",
            "Version 6.0.8"


    };
    final String[] bookPages = new String[]{
            "Copyright 2015 Joan Zapata",
            "Copyright 2013 Christopher Jenkins",
            "Copyright (C) 2015 ogaclejapan\nCopyright (C) 2013 The Android Open Source Project",
            "Copyright 2015 Georgi Eftimov",
            "Maintained by hotchemi",
            "Copyright 2015 Dmytro Tarianyk",
            "Copyright 2016 Keepsafe Software Inc.",
            "Copyright 2018 Mike Penz"


    };
    ArrayList<HashMap<String, String>> authorList = new ArrayList<>();
    for (int i = 0; i < 8; i++){

        HashMap<String, String> data = new HashMap<>();
        data.put("title", bookTitles[i]);
        data.put("pages", bookPages[i]);
        data.put("author", authors[i]);


        authorList.add(data);

    }


    //Setup adapter
    customListViewAdapter = new CustomListViewAdapter(getApplicationContext(), authorList);
    listView.setAdapter(customListViewAdapter);

这是我得到的输出

output of CustomListView Adapter
remaining output of CustomListView Adapter

请帮助我。

1 个答案:

答案 0 :(得分:0)

尝试一下:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view;
    if (convertView == null){
        view = inflater.inflate(R.layout.list_row, null);
    } else {
        view = convertView;
    }

    TextView title = (TextView) view.findViewById(R.id.libraryName);
    TextView author = (TextView) view.findViewById(R.id.libraryVersion);
    TextView pages = (TextView) view.findViewById(R.id.libraryCopyright);

    HashMap<String, String> mBook = new HashMap<>();
    mBook = books.get(position);

    title.setText(mBook.get("title"));
    author.setText(mBook.get("author"));
    pages.setText(mBook.get("pages"));

    return view;
}

希望有帮助!