Android ViewFlow setFlowIndicator IndexOutOfBoundException

478 Views Asked by At

I am having a problem with the ViewFlow class from pakerfeldt on Github.

I am trying to make a 'Different View View Flow'. Here is my activity code:

package com.suncco.silent_communities.activity;

import org.taptwo.android.widget.TitleFlowIndicator;
import org.taptwo.android.widget.ViewFlow;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

import com.suncco.silent_communities.R;

public class HousingActivity extends BaseActivity {

private ViewFlow viewFlow;
private ListView listView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.housing_activity);
    initMenu();
    viewFlow = (ViewFlow) findViewById(R.id.viewflow);
    DiffAdapter adapter = new DiffAdapter(this);
    viewFlow.setAdapter(adapter);
    TitleFlowIndicator indicator = (TitleFlowIndicator) findViewById(R.id.viewflowindic);
    indicator.setTitleProvider(adapter);
    viewFlow.setFlowIndicator(indicator);

    /** To populate ListView in diff_view1.xml */
    // listView = (ListView) findViewById(R.id.listView1);
    // String[] names = new String[] { "Cupcake", "Donut", "Eclair",
    // "Froyo",
    // "Gingerbread", "Honeycomb", "IceCream Sandwich" };
    // listView.setAdapter(new ArrayAdapter<String>(this,
    // android.R.layout.simple_list_item_1, names));

    }
}

As you can see this is pretty similar to the example code.

Here is the adapter:

package com.suncco.silent_communities.view;

import org.taptwo.android.widget.TitleProvider;

import com.suncco.silent_communities.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class HousingViewflowAdapter extends BaseAdapter implements
        TitleProvider {

    private static final int VIEW1 = 0;
    private static final int VIEW2 = 1;
    private static final int VIEW_MAX_COUNT = VIEW2 + 1;
    private final String[] names = { "View1", "View2" };

    private LayoutInflater mInflater;

    public HousingViewflowAdapter(Context context) {
        mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return VIEW_MAX_COUNT;
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int view = getItemViewType(position);
        if (convertView == null) {
            switch (view) {
            case VIEW1:
                convertView = mInflater.inflate(
                        R.layout.housing_flowview_view1, null);
                break;
            case VIEW2:
                convertView = mInflater.inflate(
                        R.layout.housing_flowview_view2, null);
                break;
            }
        }
        return convertView;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.taptwo.android.widget.TitleProvider#getTitle(int)
     */
    public String getTitle(int position) {
        return names[position];
    }

}

Also similar to the example and the end the view:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.suncco.silent_communities"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <org.taptwo.android.widget.TitleFlowIndicator
            android:id="@+id/viewflowindic"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dip"
            app:clipPadding="5dp"
            app:footerColor="#FFFFC445"
            app:footerLineHeight="2dp"
            app:footerTriangleHeight="10dp"
            app:selectedColor="#FFFFC445"
            app:selectedSize="12dp"
            app:textColor="#FFFFFFFF"
            app:textSize="11sp"
            app:titlePadding="10dp" />
    </LinearLayout>

    <org.taptwo.android.widget.ViewFlow
        android:id="@+id/viewflow"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        app:sidebuffer="5" />

</LinearLayout>

And here my exception:

04-03 15:19:59.538: E/AndroidRuntime(1677): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
04-03 15:19:59.538: E/AndroidRuntime(1677):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
04-03 15:19:59.538: E/AndroidRuntime(1677):     at java.util.ArrayList.get(ArrayList.java:311)
04-03 15:19:59.538: E/AndroidRuntime(1677):     at org.taptwo.android.widget.TitleFlowIndicator.onDraw(TitleFlowIndicator.java:157)
04-03 15:19:59.538: E/AndroidRuntime(1677):     at android.view.View.draw(View.java:6880)
04-03 15:19:59.538: E/AndroidRuntime(1677):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
04-03 15:19:59.538: E/AndroidRuntime(1677):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)

Does someone knows what the problem is? Thanks in advance.

1

There are 1 best solutions below

0
On BEST ANSWER
@Override
public int getItemViewType(int position) {
    return position % 2;
}

Hope this will help. The method you overridded is returning exact value as position. And also getCount method is returning 0, it should be number of elements to be added to the ListView based on your DataProvider. You can return names.length instead.