How to use getview() with a SimpleAdapter in Android?

筅森魡賤 提交于 2019-12-11 02:53:35

问题


I am generating a ListView using a SimpleAdapter. My SimpleAdapter code is as follows:

ListAdapter k = new SimpleAdapter(this, val1, R.layout.mytask, new String[]{"TaskId", "heading", "status"}, new int[]{R.id.View1, R.id.View2, R.id.ViewStatus}); 

My activity is MainActivity which extends Activity. I want to override the getview() method. How can I do that?


回答1:


You can do it this way:

ListAdapter k = new SimpleAdapter(...) {
    @Override
    public View getView (int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        ...
        return view;
    }
}



回答2:


Try this,

  public class Adapter extends SimpleAdapter{
        HashMap<String, String> map = new HashMap<String, String>();
        public Adapter(Context context, List<? extends Map<String, String>> data,
                int resource, String[] from, int[] to) {
            super(context, data, resource, from, to);
        }
    @Override
        public View getView(int position, View convertView, ViewGroup parent){


          // You can customize here.

        }

    }

Instead of calling new SimpleAdapter you can call something like this

 Adapter mAdapter = new Adapter(params);

Keep this class as a subclass in your activity itself,




回答3:


Create class which extends SimpleAdapter and override it's methods. don't forget to handle the constructor and call super(...)



来源:https://stackoverflow.com/questions/22904495/how-to-use-getview-with-a-simpleadapter-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!