简体   繁体   English

适用于ListView的Android LazyAdapter

[英]Android LazyAdapter for ListView

I think I messed up with my Context. 我想我搞砸了我的情境。 Could somebody correct my codes? 有人可以纠正我的代码吗? I cant seems to figure out how to implement Context into my lazyadapter. 我似乎无法弄清楚如何将Context实现到我的lazyadapter中。 please take a look at my LOGCAT. 请看看我的LOGCAT。 Thanks. 谢谢。

AndroidFragment.java 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 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);
}

I got this error : 我收到了这个错误:

Type mismatch: cannot convert from Context to Application

Remove the (Activity) cast you use in the constructor. 删除您在构造函数中使用的(Activity)强制转换。 You already used inflator.getContext.getApplicationContext when you created an instance and you cannot cast it to an Activity context again. 您在创建实例时已经使用了inflator.getContext.getApplicationContext ,并且无法再将其Activity转换为Activity上下文。 Either you pass the inflator.getContext() only (if you use the Activity context, you may get a memory leak), or you use the Application context, so you remove the Activity cast. 您只传递inflator.getContext() (如果您使用Activity上下文,可能会出现内存泄漏),或者您使用Application上下文,因此您删除了Activity cast。

Edit: 编辑:

You already have the ApplicationContext passed to the constructor, so there is no need to cast it again to Activity: 您已将ApplicationContext传递给构造函数,因此无需再将其强制转换为Activity:

activity = (Activity) appContext;

Then get the Application context again: 然后再次获取Application上下文:

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

Just set activity = appContext; 只需设置activity = appContext; (the variable should be named Application now) and pass it directly imageLoader=new MainPageImageLoader(activity); (该变量现在应该命名为Application)并直接传递imageLoader=new MainPageImageLoader(activity);

look to MainPageLazyAdapter constructor code: activity = (Activity) appContext; 查看MainPageLazyAdapter构造函数代码: activity = (Activity) appContext;
from other hand - class AndroidFragment.onCreateView : appContext = inflater.getContext().getApplicationContext(); 来自其他手工类AndroidFragment.onCreateView :appContext = inflater.getContext()。getApplicationContext();


and in AndroidFragment.onPostExecute : 并在AndroidFragment.onPostExecute
adapter=new MainPageLazyAdapter(appContext, menuItems);
so - you are casting Application Context to Activity and it сouses cast error. 所以 - 您正在将应用程序上下文转换为活动,并且它会导致转换错误。

to avoid this situation you might override AndroidFragment.onAttach method and add one member to AndroidFragment : 要避免这种情况,您可以覆盖AndroidFragment.onAttach方法并向AndroidFragment添加一个成员:

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


then replace 然后更换
adapter=new MainPageLazyAdapter(appContext, menuItems);
with
adapter=new MainPageLazyAdapter(fragmentActivity, menuItems);


and ... consider move imageLoader=new MainPageImageLoader(activity.getApplicationContext()); 和...考虑移动imageLoader=new MainPageImageLoader(activity.getApplicationContext());
to onAttach() to onAttach()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM