0

So I am writing an app that will return common filmography of two actors. I have parsed the JSON containing the data up to the point where I have two ArrayLists containing filmography for each search query. I have then made a commonFilmography list that contains only the titles common to both search queries. I want to display this list in a list view. The research I have done so far has led me to believe I need to do this in an AsyncTask since its a networking operation and my class should return the type ArrayList>. I am not sure where in scope to my return commonFilmography statement. It keeps coming up as undefined. Also in general is the way I am going about this the best way? I am new to Android and Java and learning on the fly. Also I have a warning on line 159 HashSet is a raw type. Not sure what that means. Thanks!

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

import android.widget.EditText;
import android.widget.Button;
import android.view.View;
import android.util.Log;


import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.*;

import org.json.JSONArray;
import org.json.JSONException;

import org.json.JSONObject;



public class Main extends Activity {

private static String personURL = "http://api.themoviedb.org/3/search/person?api_key=bb0b6d66c2899aefb4d0863b0d37dc4e&query=";
private static String creditURlBase = "http://api.themoviedb.org/3/person";
private static String TAG_CAST = "cast";
private static String TAG_ID = "id";
private static String TAG_NAME = "name";
private static String TAG_RESULTS = "results";
private static String TAG_TITLE = "title";

String title = null;

JSONArray idOne = null;
JSONArray idTwo = null;

JSONArray firstCast = null;
JSONArray secondCast = null;


EditText searchOne;
EditText searchTwo;

Button findMovies;

List<String> searchOneFilmography = new ArrayList<String>();
List<String> searchTwoFilmography = new ArrayList<String>();
ArrayList<HashMap<String, String>> commonFilmogrpahy = new ArrayList<HashMap<String, String>>();




@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.totlayout);

    searchOne = (EditText) findViewById(R.id.searchOne);
    searchTwo = (EditText) findViewById(R.id.searchTwo);

    findMovies = (Button) findViewById(R.id.findMovies);



    //getting

    findMovies.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub


            new getResults().execute();

        }
    });
}
public class getResults extends AsyncTask<String, Void, ArrayList<HashMap<String, String>> > {

    @Override
    protected ArrayList<HashMap<String, String>> doInBackground(
            String... params) {
        // TODO Auto-generated method stub

        //get names from each text box
        String nameOne = searchOne.getText().toString();
        String nameTwo = searchTwo.getText().toString();

        nameOne = nameOne.replace(" ", "_");
        nameTwo = nameTwo.replace(" ", "_");

        String searchOneURL = personURL + nameOne;
        String searchTwoURL = personURL + nameTwo;

        //Hashmap for ListView


        //Create JSON Parser Instanece
        JSONParser jParser = new JSONParser();

        //getting JSON string from url
        JSONObject jsonOne = jParser.getJSONFromUrl(searchOneURL);
        JSONObject jsonTwo = jParser.getJSONFromUrl(searchTwoURL);


        try {
            //Get ID of each person
            idOne = jsonOne.getJSONArray(TAG_ID);
            idTwo = jsonTwo.getJSONArray(TAG_ID);


            String firstID = null;
            String secondID = null;
            for(int i = 0; i < idOne.length(); i++){
                JSONObject iDeeOne = idOne.getJSONObject(i);

                //store each json item in variable
                firstID = iDeeOne.getString(TAG_ID);

            }

            for(int i = 0; i < idTwo.length(); i++){
                JSONObject iDeeTwo = idTwo.getJSONObject(i);

                //store each json item in variable
                secondID = iDeeTwo.getString(TAG_ID);
            }
            String creditURlBase = "http://api.themoviedb.org/3/person";

            String firstCreditURL = creditURlBase + firstID;
            String secondCreditURL = creditURlBase + secondID;

            JSONObject jSon = jParser.getJSONFromUrl(firstCreditURL);


            firstCast = jSon.getJSONArray(TAG_CAST);
            for(int i = 0; i < firstCast.length(); i++){
                JSONObject c = firstCast.getJSONObject(i);
                title = c.getString(TAG_TITLE);
                searchOneFilmography.add(title);
            }

            secondCast = jSon.getJSONArray(TAG_CAST);
            for(int i = 0; i < secondCast.length(); i++){
                JSONObject c = firstCast.getJSONObject(i);
                title = c.getString(TAG_TITLE);
                searchTwoFilmography.add(title);
            }

            //create hashmap
            HashMap<String, String> map = new HashMap<String, String>();

            Set hashset = new HashSet(searchOneFilmography);



            for(int i = 0; i < searchTwoFilmography.size(); i++){
                if(hashset.contains(searchTwoFilmography.get(i))){

                    map.put(TAG_TITLE, title);
                    commonFilmogrpahy.add(map);
                }

            }

            }
        catch(JSONException e){
            Log.e("Error", e.toString());
        }

        }


    }



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.totlayout, menu);
    return true;
}
}
user881667
  • 165
  • 1
  • 17

1 Answers1

1

Try intersecting like this.

public <T> List<T> intersection(List<T> list1, List<T> list2) {
    List<T> list = new ArrayList<T>();

    for (T t : list1) {
        if(list2.contains(t)) {
            list.add(t);
        }
    }

    return list;
}

However, you might wanna use the Endless Adapter by commonsware which you can see here Android Endless Adapter

As long as you don't have to wait on your input, you can intersect these two before intiializing the ArrayAdapter that you will pass to your ListView. If you however have to wait for such input, using an asynctask might be a good idea too :)

Community
  • 1
  • 1
Shark
  • 6,513
  • 3
  • 28
  • 50