将数据frm List <string []>插入List <string>并映射<striing,striing>

时间:2016-05-24 06:02:43

标签: java hashmap

从CSV文件中读取数据后,我将其存储在List中。

List <String>listDataHeader = new ArrayList<String>();
Map<String,String>listDataChild = new HashMap<String, String>();

List<String[]> termDefinition = csv.read();

然后我在列表上删除并想要存储数据。正如现在写的那样,我选择了正确的键,但不是值:

for (String[] term : termDefinition) {
    listDataHeader.add(term[0]);
    listDataChild.put(term[0], term[1]);
}
你可以帮帮我吗?谢谢!

编辑: 我需要在ExpandavbleListVew中填充数据。 listdataHeader是主题,listdataChild是子项。 CSV中只有2列,格式为术语定义。目前,正确存储了主题(来自listDataheader)。在纠正循环之后(见上文),我对每个主要项目的定义出现的次数与循环的大小一样多(在这种情况下为10)。

这是expandableListView类:

public class ExpandableListAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<String> listDataHeader; // header titles
    // child data in format of header title, child title
    private Map<String, String> listDataChild;

    public ExpandableListAdapter(Context context, List<String> listDataHeader,
                                 Map<String, String> listChildData) {
        this.context = context;
        this.listDataHeader = listDataHeader;
        this.listDataChild = listChildData;
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this.listDataChild.get(this.listDataHeader.get(groupPosition));
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.item, null);
        }

        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.itemList);

        txtListChild.setText(childText);
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this.listDataChild.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this.listDataHeader.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this.listDataHeader.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.header, null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.headerList);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

}

1 个答案:

答案 0 :(得分:0)

我没有看到listDataHeader List的重点。使用它会导致您在listDataHeader.get(0)中反复放置相同的密钥(Map)。

由于您在CSV的每一行中只有2列,因此您只需:

for (String[] term : termDefinition) {
    listDataChild.put(term[0], term[1]);
}