zhydian
zhydian

Reputation: 76

Trying to create a view at runtime with a baseadapter

I am using a baseadapter to create my own listview adapter and I want to dynamically create the view at runtime without using the inflater. When I do this not using the baseadapter it works fine but not when I put the code in the baseadapter.

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

        int type = getItemViewType(position);
        System.out.println("getView " + position + " " + convertView + " type = " + type);
        if (convertView == null) {

            switch (type) {
                case TYPE_ITEM:
                    //convertView = mInflater.inflate(R.layout.item1, null);
                    LinearLayout ll = new LinearLayout(MainActivity.this);
                    TextView question = new TextView(MainActivity.this);
                    question.setTextSize(1,14);
                    question.setText("This is question");

                    ll = new LinearLayout(MainActivity.this);
                            ll.setOrientation(android.widget.LinearLayout.VERTICAL);
                    ll.addView(question); 
                    convertView=ll;
                    break;
                case TYPE_SEPARATOR:
                    convertView = mInflater.inflate(R.layout.item2, null);
                    break;
            }
        } else {
        }
        return convertView;
    }

Upvotes: 0

Views: 401

Answers (1)

Simon Meyer
Simon Meyer

Reputation: 2044

You first check if convertView is null and then return null if type is TYPE_ITEM - you at least have to return ll.

BTW: you should try to recycle the convertView for performance reasons if its not null. See http://www.vogella.com/articles/AndroidListView/article.html for more information about ListAdapters

Upvotes: 1

Related Questions