Arraylist没有正确返回索引

时间:2015-12-16 10:58:05

标签: java android arraylist indexof

您好,我有一个ID的Arraylist,我正在搜索该ID的匹配索引我正在这样做

    var vectorSource = new ol.source.Vector(),
    vectorLayer = new ol.layer.Vector({
      source: vectorSource
    }),
    olview = new ol.View({
        center: [0, 0],
        zoom: 4,
        minZoom: 2,
        maxZoom: 20
    }),
    map = new ol.Map({
        target: document.getElementById('map'),
        view: olview,
        layers: [
            new ol.layer.Tile({
                style: 'Aerial',
                source: new ol.source.MapQuest({ layer: 'osm' })
            }),
            vectorLayer
        ]
    });


var iconStyle = new ol.style.Style({
    image: new ol.style.Icon({
        anchor: [0.5, 46],
        anchorXUnits: 'fraction',
        anchorYUnits: 'pixels',
        opacity: 0.75,
        src: '//openlayers.org/en/v3.8.2/examples/data/icon.png'
    }),
    text: new ol.style.Text({
        font: '12px Calibri,sans-serif',
        fill: new ol.style.Fill({ color: '#000' }),
        stroke: new ol.style.Stroke({
            color: '#fff', width: 2
        }),
        text: 'lon and lat'
    })
});


map.on('click', function(evt){
    var feature = new ol.Feature(
        new ol.geom.Point(evt.coordinate)
    );
    feature.setStyle(iconStyle);
    vectorSource.clear();
    vectorSource.addFeature(feature);
    getLocation();
});
function getLocation() {
alert('how to get lon and lat ?!?'); }

我正在传递arraylist和类别名称以找到索引我有多个类别所以我想要多个匹配值索引所以我在下面的循环检查中这样做

 private int getCategoryPos(List<String> list_ids, String category) {
    return list_ids.indexOf(category);
}

它正在正确返回第一个索引,其他是-1,这意味着找不到索引,但它出现在我的Arraylist下面是我的原始列表

 for(int i=0;i<getmusicvalue.size();i++) {

            Log.i("spinposlistse", String.valueOf(getmusicvalue.get(i)));
            String replace = String.valueOf(getmusicvalue.get(i)).replace("[", "");
            String replace1 = replace.replace("]", "");

            int spinnerPosition = getCategoryPos(musiclist_ids, replace1);
           spinposlist.add(String.valueOf(spinnerPosition));

        }

搜索列表是:

[72894F9E-B925-479F-8D74-DEC36F81D2A6, 85FB184F-92E0-4816-9715-9E85218AF994, E4853E42-E721-4259-873F-1017A1CAC554, 73445B72-6E45-4D3F-91A3-DAEAD3E3E1EF, 84EFC496-691E-4F78-BF71-62148C34317E, 4AD6E3AD-5BD3-4531-B13B-F6EA17C1FD1A, D739E7BF-DBF6-47FB-90C8-68EC2711C73C, 46139642-4639-42DB-BEC2-AF7CBFA7BB83, BA895652-5D22-45EC-A1BC-BC457856C343, 74ED6675-241E-4EB2-B0A7-6CC78D3EC80D, 2FC2CE27-370D-4D27-BD0C-8FC0F6613A18]

你可以看到两个ID都出现在原始列表中,但它给出了错误的输出,下面是我的输出

[[84EFC496-691E-4F78-BF71-62148C34317E,  85FB184F-92E0-4816-9715-9E85218AF994]]

任何人都可以帮我解决我的代码中的错误吗?

1 个答案:

答案 0 :(得分:0)

请你这样试试吗?

for(int i=0;i<getmusicvalue.size();i++) {
    Log.i("spinposlistse", String.valueOf(getmusicvalue.get(i)));
    String replace = String.valueOf(getmusicvalue.get(i)).replace("[", "");
    String replace1 = replace.replace("]", "");
    int spinnerPosition = getIndex(musiclist_ids, replace1);
    Log.i("Got Index", ""+spinnerPosition);
    spinposlist.add(String.valueOf(spinnerPosition));
 }

添加方法以获取实际索引:

   private int getIndex(List<String> list, String item){
        int position = -1;
        for (int i=0; i<list.size(); i++){
            if(item.equalsIgnoreCase(list.get(i))){
                position = i;
                break;
            }
        }
        return position;
    }

希望这会对你有所帮助。