1

I try to implement a ListView with specific icon, title and subtitle at each item.
The data of all items is in an ArrayList of objects from following class:

class ItemObject{
    String title="";
    String subTitle="";
    String unit="";
    int icon;
    int quantity;
    int parentID;
    int orderInList;
}

ArrayList<ItemObject> listViewData;

Here is the code I used first:

class ExtendedArrayAdapter<String> extends ArrayAdapter<String>{
    private Context context;
    private String[] items;

    public ExtendedArrayAdapter(Context context, String[] items){
        super(context,-1,items);
        this.context = context;
        this.items=items;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.lv_item_bg,parent,false);
        TextView itemTitle = (TextView)itemView.findViewById(R.id.itemTitle);
        TextView itemSubTitle = (TextView)itemView.findViewById(R.id.itemSubTitle);
        ImageView itemIcon = (ImageView)itemView.findViewById(R.id.itemIcon);

        itemTitle.setText(listViewData.get(position).title);
        itemSubTitle.setText(listViewData.get(position).subTitle);
        itemIcon.setBackgroundResource(listViewData.get(position).icon);
        return itemView;
    }
}

But the problem regarding this custom ArrayAdapter is that ExtendedArrayAdapter needs a String[] of items to get the number of ListView items.

Question: Is it possible to change the ExtendedArrayAdapter class to get directly the length of ListView instead of String[] items?

EDIT1: In super(), I can use only following set of parameters: enter image description here

and all need Array<String> or List<String>. So I cannot use my ArrayList of objects here.

1 Answer 1

1

Is it possible to change the ExtendedArrayAdapter class to get directly the length of ListView instead of String[] items?

the length of the ListView in terms of item it is handling, is given from Adapter.getCount(). The answer to your question, if I understand it correctly, is then no. The definition of your Adapter should change like

class ExtendedArrayAdapter extends ArrayAdapter<ItemObject>{

you will have to change the constructor and the type of items as well

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

4 Comments

if I remove String[] items from constructor, and consequently from super(context,-1,items);, an empty ListView is shown
not removing. You have to change it in either ItemObject[] or List< ItemObject>
please have a look at <b>EDIT1</b>
did you change the declaration of ExtendedArrayAdapter like: class ExtendedArrayAdapter extends ArrayAdapter<ItemObject>{ ?

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.