2013-08-31 23 views
0

似乎無法找出爲什麼我得到這個錯誤「ArrayList<HashMap<String,String>>, int, String[], int[])是不確定的」的ArrayList HashMap的是未定義

// THE ERROR IS RIGHT HERE TO 
      ListAdapter adapter = new SimpleAdapter(this, contactList, 
        R.layout.main, 
        new String[] { TAG_FIRSTNAME, TAG_EMAIL, TAG_PHONE }, new int[] { 
          R.id.nameFirst, R.id.email, R.id.mobile }); 
      //HERE 

這裏是我的代碼。這個想法是從應用程序引擎數據存儲中獲取JSON,然後將其分成列表視圖。

package com.indeeditis; 




public class FinderActivity extends ListActivity { 


private static final String TAG_ID = "id"; 
private static final String TAG_FIRSTNAME = "nameFirst"; 
private static final String TAG_LASTNAME = "nameLast"; 
private static final String TAG_EMAIL = "emailAddress"; 
private static final String TAG_ADDRESS = "address"; 
private static final String TAG_STATE = "state"; 

private static final String TAG_PHONE = "phone"; 
JSONArray contacts = null; 

ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>(); 

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

    // Button start = (Button)findViewById(R.id.button9000); 
    //start.setOnClickListener(this); 
    new EndpointsTask().execute(getApplicationContext()); 

} 



public class EndpointsTask extends AsyncTask<Context, Integer, Long> { 

    public Long doInBackground(Context... contexts) { 

     Contactinfoendpoint.Builder endpointBuilder = new Contactinfoendpoint.Builder(
      AndroidHttp.newCompatibleTransport(), 
      new JacksonFactory(), 
      new HttpRequestInitializer() { 
      public void initialize(HttpRequest httpRequest) { } 
      }); 
    Contactinfoendpoint endpoint = CloudEndpointUtils.updateBuilder(
    endpointBuilder).build(); 

    try { 

    // final TextView detail = (TextView)findViewById(R.id.textView100); 

    String apples = endpoint.listContactInfo().execute().toString(); 

    JSONObject jObject = new JSONObject(apples); 

      try{ 
     //Get the element that holds the earthquakes (JSONArray) 
     JSONArray contacts = jObject.getJSONArray("item"); 

     for(int i = 0; i < contacts.length(); i++){ 
      JSONObject c = contacts.getJSONObject(i); 

      // Storing each json item in variable 
      String id = c.getString(TAG_ID); 
      String nameFirst = c.getString(TAG_FIRSTNAME); 
      String nameLast = c.getString(TAG_LASTNAME); 
      String email = c.getString(TAG_EMAIL); 
      String address = c.getString(TAG_ADDRESS); 
      String phone = c.getString(TAG_PHONE); 

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

      // adding each child node to HashMap key => value 
      map.put(TAG_ID, id); 
      map.put(TAG_FIRSTNAME, nameFirst); 
      map.put(TAG_EMAIL, email); 
      map.put(TAG_PHONE, phone); 

      // adding HashList to ArrayList 
      contactList.add(map); 



     } 

      }catch(JSONException e)  { 
      Log.e("log_tag", "Error parsing data "+e.toString()); 
      } 


      // THE ERROR IS RIGHT HERE TO 
      ListAdapter adapter = new SimpleAdapter(this, contactList, 
        R.layout.main, 
        new String[] { TAG_FIRSTNAME, TAG_EMAIL, TAG_PHONE }, new int[] { 
          R.id.nameFirst, R.id.email, R.id.mobile }); 
      //HERE 
      setListAdapter(adapter); 

      // selecting single ListView item 
      ListView lv = getListView(); 

    } catch (IOException e) { 
    e.printStackTrace(); 
    } catch (JSONException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
     return (long) 0; 
    } 


    } 

} 
+1

你從哪裏得到這個錯誤?並且請從那裏刪除不相關的代碼。特別是進口。 –

+1

粘貼確切完整的錯誤信息,並告訴我們它指的是哪一行。 –

+0

@JBNizet構造函數SimpleAdapter(FinderActivity.EndpointsTask,ArrayList >,int,String [],int [])未定義 – johnsonjp34

回答

2

SimpleAdapter只有一個constructor

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

您正在使用

new SimpleAdapter(this, // not a Context 
       contactList, // List 
       R.layout.main, // int 
       new String[] { TAG_FIRSTNAME, TAG_EMAIL, TAG_PHONE }, // String[] 
       new int[] {R.id.nameFirst, R.id.email, R.id.mobile }); // int[] 

其中this不是Context,但對EndpointsTask實例的引用。如果EndpointsTaskFinderActivity的內部類別,則使用FinderActivity.this

+0

修復它。謝謝! – johnsonjp34

+0

@ coconuts4eva不客氣。考慮在可以的時候接受答案。 –

1

從Android的文檔,

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

Added in API level 1 

Constructor 
Parameters 
context  The context where the View associated with this SimpleAdapter 
      is running 

data  A List of Maps. Each entry in the List corresponds to one row 
      in the list. The Maps contain the data for each row, and should 
      include all the entries specified in "from" 

resource Resource identifier of a view layout that defines the views for this 
      list item. The layout file should include at least those named views 
      defined in "to" 

from  A list of column names that will be added to the Map associated with 
      each item. 

to   The views that should display column in the "from" parameter. 
      These should all be TextViews. The first N views in this list are 
      given the values of the first N columns in the from parameter. 

但在你的代碼不是context按構造的規範。

使用FinderActivity.this。它會工作