2012-09-11 43 views
0

我正嘗試用ArrayList<HashMap<String, String>>中的內容更新列表視圖。我不明白爲什麼我收到以下錯誤:更新ArrayList <HashMap <字符串到ListView(undefined simpleadapter錯誤)

構造SimpleAdapter(Main.getResults, ArrayList<HashMap<String,String>>, int, String[])未定義

public class Main extends Activity { 

private static String personURL = "http://api.themoviedb.org/3/search/person?api_key=bb0b6d66c2899aefb4d0863b0d37dc4e&query="; 

private static String TAG_CAST = "cast"; 
private static String TAG_ID = "id"; 

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 

     ArrayList<HashMap<String, String>> commonFilms = new ArrayList<HashMap<String, String>>(); 
     ArrayList<HashMap<String, String>> firstFilms = new ArrayList<HashMap<String, String>>(); 
     ArrayList<HashMap<String, String>> secondFilms = new ArrayList<HashMap<String, String>>(); 
     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); 

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

       //and node to map 
       map.put(TAG_TITLE, title); 
       firstFilms.add(map); 

      } 
      JSONObject jSonTwo = jParser.getJSONFromUrl(secondCreditURL); 

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

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

       mapTwo.put(TAG_TITLE, title); 
       secondFilms.add(mapTwo); 


      } 

      if(firstFilms.size() > secondFilms.size()){ 
       for(int i = 0; i < firstFilms.size(); i++){ 
        if(firstFilms.contains(secondFilms.get(i))){ 

         HashMap<String, String> mapThree = new HashMap<String, String>(); 

         mapThree.put(TAG_TITLE, secondFilms.get(i).toString()); 
         commonFilms.add(mapThree); 
        } 

       } 

      }else{ 
       for(int i = 0; i < secondFilms.size(); i++){ 
        if(secondFilms.contains(firstFilms.get(i))){ 
         HashMap<String, String> map = new HashMap<String, String>(); 
         map.put(TAG_TITLE, firstFilms.get(i).toString()); 
         commonFilms.add(map); 
        } 
       } 
      } 
      return commonFilms; 




      } 
     catch(JSONException e){ 
      Log.e("Error", e.toString()); 
      return null; 
     } 
     ListAdapter adapter = new SimpleAdapter(this, commonFilms, R.layout.resultslayout, new String[] {TAG_NAME}); 
     setListAdapter(adapter); 

我也曾嘗試

ListAdapter adapter = new SimpleAdapter(Main.this, commonFilms, R.layout.resultslayout, new String[] {TAG_NAME}); 

我仍然得到同樣的未定義的錯誤只是Main.this代替。

回答

3

難道你錯過了一個參數嗎? The Javadoc for SimpleAdapter說,構造函數的參數是:

(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)

你沒有傳遞任何參數爲參數。

編輯補充:此外,您getResults類擴展AsyncTask,所以它不是Context一個子類。

相關問題