Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在两个列表之间获取公共元素的ArrayList,这些列表可以填充到listview中。_Java_Android - Fatal编程技术网

Java 如何在两个列表之间获取公共元素的ArrayList,这些列表可以填充到listview中。

Java 如何在两个列表之间获取公共元素的ArrayList,这些列表可以填充到listview中。,java,android,Java,Android,因此,我正在编写一个应用程序,它将返回两位演员的共同电影记录。我已经解析了包含数据的JSON,在这里我有两个ArrayList,其中包含每个搜索查询的从影记录。然后我制作了一个公共电影记录列表,其中只包含两个搜索查询所共有的标题。我想在列表视图中显示此列表。到目前为止,我所做的研究使我相信我需要在异步任务中执行此操作,因为这是一个网络操作,我的类应该返回类型ArrayList>。我不确定我的复述范围在哪里。它一直被认为是未定义的。总的来说,我这样做是最好的方式吗?我对安卓和Java还不熟悉,而且

因此,我正在编写一个应用程序,它将返回两位演员的共同电影记录。我已经解析了包含数据的JSON,在这里我有两个ArrayList,其中包含每个搜索查询的从影记录。然后我制作了一个公共电影记录列表,其中只包含两个搜索查询所共有的标题。我想在列表视图中显示此列表。到目前为止,我所做的研究使我相信我需要在异步任务中执行此操作,因为这是一个网络操作,我的类应该返回类型ArrayList>。我不确定我的复述范围在哪里。它一直被认为是未定义的。总的来说,我这样做是最好的方式吗?我对安卓和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;
}
}
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.app.Activity;
导入android.view.Menu;
导入android.widget.EditText;
导入android.widget.Button;
导入android.view.view;
导入android.util.Log;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入java.util.*;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
公共类主要扩展活动{
私有静态字符串personURL=”http://api.themoviedb.org/3/search/person?api_key=bb0b6d66c2899aefb4d0863b0d37dc4e&query=";
私有静态字符串creditURlBase=”http://api.themoviedb.org/3/person";
私有静态字符串标记_CAST=“CAST”;
私有静态字符串标记\u ID=“ID”;
私有静态字符串标记_NAME=“NAME”;
私有静态字符串TAG_RESULTS=“RESULTS”;
私有静态字符串TAG_TITLE=“TITLE”;
字符串标题=null;
JSONArray idOne=null;
JSONArray idTwo=null;
JSONArray firstCast=null;
JSONArray secondCast=null;
编辑文本搜索;
编辑文本搜索二;
按钮式汽车;
List searchOneFilmography=new ArrayList();
List searchTwoFilmography=new ArrayList();
ArrayList commonFilmogrpahy=新ArrayList();
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.totlayout);
searchOne=(EditText)findViewById(R.id.searchOne);
searchTwo=(EditText)findViewById(R.id.searchTwo);
findMovies=(按钮)findViewById(R.id.findMovies);
//得到
findMovies.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
新建getResults().execute();
}
});
}
公共类getResults扩展了异步任务{
@凌驾
受保护的ArrayList doInBackground(
字符串…参数){
//TODO自动生成的方法存根
//从每个文本框中获取名称
字符串nameOne=searchOne.getText().toString();
String nameTwo=searchTwo.getText().toString();
nameOne=nameOne.replace(“,”);
nameTwo=nameTwo.replace(“,”);
字符串searchOneURL=personURL+nameOne;
字符串searchTwoURL=personURL+nametwor;
//ListView的Hashmap
//创建JSON解析器实例
JSONParser jParser=新的JSONParser();
//从url获取JSON字符串
JSONObject jsonOne=jParser.getJSONFromUrl(searchOneURL);
JSONObject jsonTwo=jParser.getJSONFromUrl(searchTwoURL);
试一试{
//获取每个人的身份证
idOne=jsonOne.getJSONArray(TAG_ID);
idTwo=jsonTwo.getJSONArray(TAG_ID);
字符串firstID=null;
字符串secondID=null;
for(int i=0;i
尝试这样交叉

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;
}
公共列表交叉点(列表1、列表2){
列表=新的ArrayList();
for(T:list1){
if(列表2.contains(t)){
列表。添加(t);
}
}
退货清单;
}
但是,您可能希望使用您可以在此处看到的Commonware提供的无止境适配器

阿伦