androidandroid-fragmentsarraylist

Only returns an index of -1 when there is a match


I am trying to get the index of an item in an array that is selectable in a list view.

The issue is that when I click on the item, it only returns the index of -1, which means that it doesn't match when in fact there should be a match.

What am I doing wrong?

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();
    }
}

Solution

  • you define glimpse two times, could you try to make those changes,

    List<String> glimpse; 
    

    and inside onCreate initialize it

    glimpse = new ArrayList<>(); 
    

    and do the same for titles and universalListings.