How to use getview() with a SimpleAdapter in Andro

2019-07-18 20:29发布

问题:

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(...)