MacaronLover MacaronLover - 2 months ago 5
Android Question

Fragment GridView first item not being displayed despite setting properties

After creating a gridview within a scrollview, the view itself appears as normal but the first value does not apper at all. I've tried looking at similar questions but I still don't understand what has gone wrong. What needs to change in order to fix this problem?

xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin">

<TextView
android:id="@+id/History_textView0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@android:color/white"
android:text="Years"
style="@android:style/TextAppearance.Medium" />
<com.ages.events.GridViewHistory
android:id="@+id/History_GridView0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:columnWidth="100dp"
android:numColumns="auto_fit"
android:layout_marginBottom="20dp"
android:stretchMode="columnWidth"
android:layout_below="@+id/History_textView0"/>

<TextView
android:id="@+id/History_textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@android:color/white"
android:text="Events"
style="@android:style/TextAppearance.Medium"
android:layout_below="@+id/History_GridView0" />
</RelativeLayout>




java

public class TabFragmentHistory extends android.support.v4.app.Fragment {

static final String[] years = new String[]{
"1863", "1864", "1868", "1871", "1890", "1898", "1900", "1906", "1968", "1979"
};

GridView mGridView0;
public MyAdapter adapter;

public TabFragmentHistory() {
// Required empty constructor
}

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.tab_fragment_history, container, false);
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
View v = getView();
assert v != null;

mGridView0 = (GridView) v.findViewById(R.id.HistoryGridView0);
adapter = new MyAdapter(getActivity().getApplicationContext(), 0);
mGridView0.setAdapter(adapter);
for (int i = 1; i < years.length; i++) {
adapter.addAdapterItem(new AdapterItem(years[i]));
}

super.onActivityCreated(savedInstanceState);
}

public class MyAdapter extends ArrayAdapter< AdapterItem > {
private List< AdapterItem > items = new ArrayList< AdapterItem >();

// private static LayoutInflater inflater = null;
public MyAdapter(Context context, int textviewid) {
super(context, textviewid);
}

public void addAdapterItem(AdapterItem item) {
items.add(item);
}

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

@Override
public AdapterItem getItem(int position) {
return ((null != items) ? items.get(position) : null);
}

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

@SuppressWarnings("deprecation")
@Override
public View getView(final int position, View convertView,
final ViewGroup parent) {
View rowView;
if (convertView == null) {
rowView = getActivity().getLayoutInflater().inflate(R.layout.gridview_item, parent, false);
} else {
rowView = convertView;
}
TextView tv = (TextView) rowView.findViewById(R.id.item_gridview);
tv.setText(items.get(position).first);

return rowView;
}
}

public class AdapterItem {
public String first;

// add more items
public AdapterItem(String first) {
this.first = first;

}
}
}

Answer
for (int i = 1; i < years.length; i++) {
    adapter.addAdapterItem(new AdapterItem(years[i]));
}

Should this be starting at i = 0?

Comments