1

Here is my constructor for my custom BaseAdapter

public MyAdapterAds(Activity activity, BaseAdapter delegate) {
    this.activity = activity;
    this.delegate = delegate;
}

I have an arraylist of objects named mObjects. How do I get them into this method from my activity?

How do I initialize my baseAdapter

this is what I tried

 myAdapter = new myAdapterAds(MainActivity.this, new ArrayAdapter<myObject>(context, R.layout.my_object_cell, myObjectArray));

No reason for this to work, but I had just began rewriting this baseadapter, how would I rewrite that last part to get my array of objects in it?

I keep getting "IllegalStateException: ArrayAdapter requires the resource to be a textview" but I am using an object with several parameters, why would I display a textview?

CQM
  • 42,592
  • 75
  • 224
  • 366

2 Answers2

2

           ExampleAdapter adapter = new ExampleAdapter();
        adapter.notifyDataSetChanged();
        ls.setAdapter(adapter);

public class ExampleAdapter extends BaseAdapter { ViewHolder holder;

    public View getView(final int position, View convertView,
            ViewGroup parent) {
        if (convertView == null) {

            LayoutInflater inflater = getLayoutInflater();
            convertView = inflater.inflate(R.layout.listitem, null);

            holder = new ViewHolder();
            holder.name = (TextView) convertView
                    .findViewById(R.id.textView1);


            convertView.setTag(holder);
        } else {

            holder = (ViewHolder) convertView.getTag();

        }


        holder.name.setText("name");


        return convertView;
    }

    private class ViewHolder {

        private TextView name;

    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public int getCount() {
        return size;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

}
Tarun
  • 51
  • 7
1

Is there a reason your adapter is not a subclass of BaseAdapter? Typically you want to do something like this: https://stackoverflow.com/a/2265712/832776

The reason for the warning is that the default ArrayAdapter constructor expects the layout to only contain one TextView; so I would check my_object_cell.xml is valid. If you're planning to have multiple TextViews or different widgets then you can use setViewBinder() to assign properties from your object to each view, along with the ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects) constructor.

Community
  • 1
  • 1
Oleg Vaskevich
  • 12,444
  • 6
  • 63
  • 80