0

I have CustomAdapter which extends ArrayAdapter. Constructor is called properly also data for ArrayList contains 20 items. But my getView() method is not called at all. I dont understand what is wrong. Here is my code. There are two getViev() methods commented original and simplified test getView(). Non them are called:

package web.php5.learn2codelist;


import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;

import java.util.ArrayList;


public class CustomListAdapter extends ArrayAdapter<String>
{

    Context context;
    ArrayList<String> data;

    public CustomListAdapter(@NonNull Context context, ArrayList<String> data)
    {
        super(context, R.layout.row);
        this.context = context;
        this.data = data;
        Log.d("111", "count: " + String.valueOf(data.size()));  // 20 items
        Log.d("111", "getView: 11111111111111111111111111111");  // Log is ok
    }

    @Override @NonNull
    public View getView(int position, View convertView, @NonNull ViewGroup parent)
    {

        Log.d("222", "getView: 2222222222222222222222222222222");  // No log
        return convertView;
    }

    /*@Override
    public @NonNull View getView(final int position, View convertView, @NonNull ViewGroup parent)   // final umoznuje pouzit tuto premennu v onClick
    {
        Log.d("333", "getView: 33333333333333333333333333333333");  // No log
        View row = convertView;

        Log.d("TAG1", "row: " + String.valueOf(row));

        if( row == null )
        {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.row, parent, false);
        }

        TextView label = row.findViewById(R.id.row_label);
        Button btn = row.findViewById(R.id.row_button);

        label.setText(data.get(position));

        btn.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                data.remove(position);
                notifyDataSetChanged();
            }
        });

        return row;
    }*/
}
Čamo
  • 2,352
  • 6
  • 29
  • 66
  • 1
    Your `Adapter`'s `getCount()` method is returning `0`. Either pass `data` in the `super` constructor call, or override `getCount()` to return its `size()`. If you go with the first option, then you should get rid of your `ArrayList data;` field, and let `ArrayAdapter` use its internal lists; otherwise, you might end up with issues later on. – Mike M. May 02 '20 at 15:45
  • Thank a lot it works. If I can which of your two ways is better for this purpose? Set super data or override getCount()? Is there any other methods like getcount() which can make problems if I dont override it? – Čamo May 02 '20 at 16:24
  • It depends. `ArrayAdapter` isn't always the right class to extend; it's just the most "popular", for some reason. `ArrayAdapter` is basically designed for a simple list of `String`s. If you're going to be modifying it to do much more than that, or if you really do need to track the list yourself, then it's arguably better to extend `BaseAdapter` instead, in which case you won't miss any necessary methods, 'cause they're `abstract`, and you must implement them. Otherwise, if `ArrayAdapter` is a good fit, then you should let it handle the list itself; i.e., pass it in the constructor, ... – Mike M. May 02 '20 at 16:43
  • ... and do not keep a field yourself. `ArrayAdapter` keeps lists internally, and if you try to keep yet another, it can lead to issues elsewhere; e.g., with searching and filtering. You would use `ArrayAdapter`'s corresponding methods instead; e.g., `getItem(position)` instead of `data.get(position)`. – Mike M. May 02 '20 at 16:43

0 Answers0