所以我正在编写一个应用程序,它将返回两个演员的共同电影作品。我已经解析了包含数据的 JSON,直到我有两个 ArrayLists 包含每个搜索查询的电影。然后,我制作了一个 commonFilmography 列表,其中仅包含两个搜索查询共有的标题。我想在列表视图中显示此列表。到目前为止我所做的研究使我相信我需要在 AsyncTask 中执行此操作,因为它是一个网络操作,并且我的类应该返回 ArrayList> 类型。我不确定我的 return commonFilmography 声明的范围在哪里。它不断出现未定义。总的来说,我的方法是最好的方法吗?我是 Android 和 Java 的新手,并且正在快速学习。另外我在第 159 行有一个警告 HashSet 是原始类型。不确定那是什么意思。谢谢!
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;
}
}