2012-10-06 43 views
0

我想我搞亂了我的上下文。有人可以糾正我的代碼嗎?我似乎不知道如何實現上下文到我的lazyadapter。請看看我的LOGCAT。謝謝。用於ListView的Android LazyAdapter

AndroidFragment.java

public class AndroidFragment extends SherlockListFragment implements ActionBar.TabListener{ 

    static final String URL = "https://myxml.xml"; 
    static final String KEY_SONG = "song"; 
    static final String KEY_ID = "id"; 
    static final String KEY_TITLE = "title"; 
    static final String KEY_CAT_ARTIST = "artistcat"; 
    static final String KEY_DURATION = "duration"; 
    static final String KEY_THUMB_URL = "thumb_url"; 
    static final String KEY_CAT_URL = "cat_url"; 
    ArrayList<HashMap<String, String>> menuItems; 
    ListAdapter adapter; 
    Context appContext; 
    ListView list; 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ 
     appContext = inflater.getContext().getApplicationContext(); 

     new loadListView().execute(); 

    return super.onCreateView(inflater, container, savedInstanceState); 
    } 


    public class loadListView extends AsyncTask<Integer, String, String> { 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
     } 

     @Override 
     protected String doInBackground(Integer... args) { 
      // updating UI from Background Thread 
      menuItems = new ArrayList<HashMap<String, String>>(); 


      XMLParser parser = new XMLParser(); 
      String xml = parser.getXmlFromUrl(URL); // getting XML 
      Document doc = parser.getDomElement(xml); // getting DOM element 

      NodeList nl = doc.getElementsByTagName(KEY_SONG); 
      // looping through all item nodes <item> 
      for (int i = 0; i < nl.getLength(); i++) { 
       // creating new HashMap 
       HashMap<String, String> map = new HashMap<String, String>(); 
       Element e = (Element) nl.item(i); 
       // adding each child node to HashMap key => value 
       map.put(KEY_ID, parser.getValue(e, KEY_ID)); 
       map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE)); 
       map.put(KEY_CAT_ARTIST, parser.getValue(e, KEY_CAT_ARTIST)); 
       map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION)); 
       map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL)); 
       map.put(KEY_CAT_URL, parser.getValue(e, KEY_CAT_URL)); 

       // adding HashList to ArrayList 
       menuItems.add(map); 
      } 
       return null; 
      } 


     @Override 
     protected void onPostExecute(String args) { 


      adapter=new MainPageLazyAdapter(appContext, menuItems); 
      list.setAdapter(adapter); 
     } 
    } 


    @Override 
    public void onTabSelected(Tab tab, FragmentTransaction ft) { 
     ft.add(android.R.id.content, this); 

     ft.attach(this); 
    } 

    @Override 
    public void onTabUnselected(Tab tab, FragmentTransaction ft) { 
     ft.detach(this); 
    } 

    @Override 
    public void onTabReselected(Tab tab, FragmentTransaction ft) { 
    } 

} 

MainPageLazyAdapter.java

public class MainPageLazyAdapter extends BaseAdapter { 

private Application activity; 
private ArrayList<HashMap<String, String>> data; 
private static LayoutInflater inflater=null; 
public MainPageImageLoader imageLoader; 

public MainPageLazyAdapter(Context appContext, ArrayList<HashMap<String, String>> d) { 
    activity = appContext; 
    data=d; 
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    imageLoader=new MainPageImageLoader(activity); 
} 

我得到這個錯誤:

Type mismatch: cannot convert from Context to Application 

回答

1

刪除您在構造函數中使用(Activity)演員。您在創建實例時已經使用inflator.getContext.getApplicationContext,並且無法再將其轉換爲Activity上下文。您只能通過inflator.getContext()(如果使用Activity上下文,則可能會發生內存泄漏),或者使用Application上下文,因此您會刪除Activity強制轉換。

編輯:

您已經ApplicationContext的傳遞到構造,所以沒有必要再投它到活動:

activity = (Activity) appContext; 

然後再次獲取應用程序上下文:

imageLoader=new MainPageImageLoader(activity.getApplicationContext()); 

只需設置activity = appContext;(該變量應該立即命名爲Application)並將其直接傳遞給imageLoader=new MainPageImageLoader(activity);

+0

謝謝,但我不明白你的意思是「或你使用應用程序的上下文,所以你刪除活動轉換」 –

+1

如果該片段沒有attached.onCreateView getActivity()將返回null。 –

+0

檢查編輯。 –

1

外觀MainPageLazyAdapter構造函數代碼:activity = (Activity) appContext;
從另一方面 - 類AndroidFragment.onCreateView: appContext = inflater.getContext()getApplicationContext();


AndroidFragment.onPostExecute
adapter=new MainPageLazyAdapter(appContext, menuItems);
所以 - 你是鑄造應用上下文中的活動,它сouses施放的錯誤。

避免這種情況下,你可能會覆蓋AndroidFragment.onAttach方法,並添加一個成員AndroidFragment

protected Activity fragmentActivity;  
@Override public void onAttach (Activity activity){ 
    fragmentActivity = activity; 
} 


然後更換
adapter=new MainPageLazyAdapter(appContext, menuItems);

adapter=new MainPageLazyAdapter(fragmentActivity, menuItems);


和...考慮移動imageLoader=new MainPageImageLoader(activity.getApplicationContext());
onAttach()