0

I have been searching for ways to put an Arraylist onto a ListView in android but have not been able to do so. This is the program that I use, but it seems to work only for a String array and not for a List element.

List<Students> studentList = new ArrayList<Students>();
ArrayAdapter<Students> arrayAdapter = new ArrayAdapter<Students>(
                this,
                android.R.layout.simple_list_item_single_choice,
                studentList);
listView.setAdapter(arrayAdapter);

I need to find a solution where I can put this arraylist onto the listview without having to convert this into a STring array. Is any such thing possible? Thanks in advance. :-)

2
  • You have to make a Custom Adapter Commented Aug 28, 2014 at 9:11
  • Use Custom Adapter class Commented Aug 28, 2014 at 9:12

1 Answer 1

1

You will have to override the adapter's getView method to implement your desired functioning:

ArrayAdapter<Students> arrayAdapter = new ArrayAdapter<Students>(
        this,
        android.R.layout.simple_list_item_single_choice,
        studentList) {
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Let the system create and re-use the views
        View view = super.getView(position, convertView, parent);

        // Get the default android layout's TextView
        TextView textView = (TextView) view.findViewById(android.R.id.text1);

        // Get the students info (name or w/e)
        Students student = getItem(position);
        String studentName = student.getName();
        textView.setText(studentName);

        return view;
    }
};
Sign up to request clarification or add additional context in comments.

Comments

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.