0

I'm trying to connect my ArrayList<> to my ArrayAdapter but Android Studio tells me that it cannot be apiled.

My code:

ArrayList<Events> list = new ArrayList<Events>();

    StableArrayAdapter1 adapter = new StableArrayAdapter1(this, list);



public class StableArrayAdapter1 extends ArrayAdapter<Events> {
        public StableArrayAdapter1(Context context, ArrayList<Events> users) {
            super(context, 0, users);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Events user = getItem(position);
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.listviewlayout, parent, false);
            }
            TextView firsline = (TextView) convertView.findViewById(R.id.firstLine);
            TextView secondline = (TextView) convertView.findViewById(R.id.secondLine);
            firsline.setText(user.title);
            secondline.setText(user.place);
            return convertView;
        }
    }

What i'm doing wrong? If it will needed I can add my Events class.

2 Answers 2

1

Well first of all i guess you are trying to set up a listview with a custom row correct?

So because you extend the ArrayAdapter and it tells you build constructor matching super the super() takes 3 parameters. Context, Layout id, list of (objects) what you extend.

Modify your adapter like this:

List<Events> list = new ArrayList<Events>();

    ArrayAdapter adapter = new StableArrayAdapter1(YourActivity.this, R.layout.your_custom_row_layout_file, list);



public class StableArrayAdapter1 extends ArrayAdapter<Events> {

        public StableArrayAdapter1(Context context,int layoutResId, List<Events> users) {
            super(context, R.layout.your_custom_row_layout_file, users);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Events user = getItem(position);
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.listviewlayout, parent, false);
            }

            TextView firsline = (TextView) convertView.findViewById(R.id.firstLine);
            TextView secondline = (TextView) convertView.findViewById(R.id.secondLine);
            firsline.setText(user.get(position).title);
            secondline.setText(user.get(position).place);
            return convertView;
        }
    }

Hope it helps!!!

Sign up to request clarification or add additional context in comments.

Comments

0

I found the answer:

StableArrayAdapter1 adapter = new StableArrayAdapter1(this.getActivity(),  MainActivity.list);

just need to getActivity(), hope that it will help someone in the future :P

1 Comment

and the this is suppose to refer to which class? Sir?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.