匹配时仅返回-1的索引

时间:2016-04-23 18:08:50

标签: android android-fragments arraylist

我正在尝试获取列表视图中可选择的数组中项目的索引。唯一的问题是,当我点击该项时,它只返回-1的索引,这意味着它实际上应该匹配时不匹配。先谢谢!

package com.goldleaf.branden.goldleafcomics;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.kosalgeek.android.json.JsonConverter;
import com.kosalgeek.genasync12.*;

import java.io.FileInputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class GlimpseListFragment extends ListFragment {
    List<String> glimpse = new ArrayList<String>();
    List<String> titles = new ArrayList<String>();
    List<UniverseListing> universalListings = new ArrayList<UniverseListing>();

    public GlimpseListFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        ViewGroup rootView = (ViewGroup)inflater.inflate(R.layout.fragment_glimpse_list, container, false);

        return rootView;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String url = "http://goldleafcomics.com/application/UniverseGlimpse.JSON";
        PostResponseAsyncTask task = new PostResponseAsyncTask(getActivity(), new AsyncResponse() {
            @Override
            public void processFinish(String s) {
                universalListings = new JsonConverter<UniverseListing>().toArrayList(s, UniverseListing.class);
                Toast.makeText(getActivity(), "Application Data Refreshed", Toast.LENGTH_LONG).show();

                ArrayList<String> glimpse = new ArrayList<String>();
                for(UniverseListing value: universalListings){
                    glimpse.add(value.universeGlimpse);
                }

                ArrayList<String> titles = new ArrayList<String>();
                for(UniverseListing value: universalListings){
                    titles.add(value.universeId);
                }

                ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, titles);
                setListAdapter(adapter);


            }
        });
        task.execute(url);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        String value = (String)getListAdapter().getItem(position);

        int index = this.titles.indexOf(value);
        String value2 = Integer.toString(index);

        Toast.makeText(getActivity(), value2, Toast.LENGTH_LONG).show();
    }
}

2 个答案:

答案 0 :(得分:0)

你定义了两次瞥见,你能不能尝试做出这些改变,

List<String> glimpse; 

并在onCreate里面初始化它

glimpse = new ArrayList<>(); 

并为标题 universalListings 执行相同操作。

答案 1 :(得分:0)

尝试使用这两部分代码。您是shadowing具有相同名称的局部变量的成员变量。

public class GlimpseListFragment extends ListFragment {
    List<String> glimpse;
    List<String> titles;
    List<UniverseListing> universalListings;

稍后......

            glimpse = new ArrayList<String>();
            for(UniverseListing value: universalListings){
                glimpse.add(value.universeGlimpse);
            }

            titles = new ArrayList<String>();
            for(UniverseListing value: universalListings){
                titles.add(value.universeId);
            }

您无需使用该类初始化Arraylists。您通常应该懒洋洋地初始化它们(换句话说,当您需要时)