Android: Problem with listView and getView-Method

53 Views Asked by At

Hey guys I am pretty new to android programming and I have a few questions. I hope somebody can help.

I am using the code:

new ArrayAdapter<Customer> (this, android.R.layout.simple_list_item_multiple_choice,emptyListForInitialization) {...

But I want to use my own layout I created ("R.layout.custom_customer_layout").

I can use it, if I CHANGE the code-line with:

new ArrayAdapter<Customer> (this, android.R.layout.simple_list_item_multiple_choice,emptyListForInitialization)

with my layout ("R.layout.custom_customer_layout")

AND REMOVE THE CODE

View view =  super.getView(position, convertView, parent);

And UNCOMMENT the section (code between /* ... */ ) below it.


But I don't get the getView-Method.

WHY DO I HAVE TO REFER TWICE TO MY LAYOUT?

First time during creating the new ArrayList

Second time in the getView-Method???


This is the important part of my code:

    private ListView myTestListView;

    private void initializeCustomerListView() {
        myTestListView = (ListView) findViewById(R.id.listview_customers);

        List<Customer> emptyListForInitialization = new ArrayList<>();

        ArrayAdapter<Customer> customerArrayAdapter = new ArrayAdapter<Customer> (
                this, android.R.layout.simple_list_item_multiple_choice,
                emptyListForInitialization) {

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view =  super.getView(position, convertView, parent);

                /*
                View view = getLayoutInflater().inflate(R.layout.custom_customer_layout,null);

                TextView textViewName = (TextView)convertView.findViewById(R.id.textView20);
                TextView textViewContact = (TextView)convertView.findViewById(R.id.textView21);
                TextView textViewMobile = (TextView)convertView.findViewById(R.id.textView25);

                textViewName.setText(this.getItem(position).getCustomerName());
                textViewContact.setText(this.getItem(position).getCounterpart());
                textViewMobile.setText(this.getItem(position).getMobilePhoneNumber());
                */
                return view;
            }
        };

        myTestListView.setAdapter(customerArrayAdapter);
    }
0

There are 0 best solutions below