Reputation: 13
I am new to the android api and I have some problems with .. the basics. I'm trying to get a list working, but the list displays the wrong text:
Each row has two TextViews (title, subtitle), heres the xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip" >
<TextView
android:id="@+id/row_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TITLE"
android:textSize="15dip"
android:layout_gravity="center_vertical" />
<TextView
android:id="@+id/row_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/row_title"
android:text="SUBTITLE"
android:textSize="10dip"
android:layout_gravity="center_vertical" />
</RelativeLayout>
I extend ListFragment (I need this fragment thing, because of a slidingMenu library), have a custom adapter and an item class. in the onActivityCreated() method I add some random items to the adapter:
package com.slidingmenu.example;
import android.app.Fragment;
import android.app.ListFragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class SampleListFragment extends ListFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.list, null);
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
SampleAdapter adapter = new SampleAdapter(getActivity());
for (int i = 0; i < 20; i++) {
adapter.add(new SampleItem(new Fragment(), "title no. " + i, "some subtitle"));
}
setListAdapter(adapter);
}
private class SampleItem {
public Fragment frag;
public String title;
public String subTitle;
public SampleItem(Fragment frag, String title, String subTitle) {
this.frag = frag;
this.title = title;
this.subTitle = subTitle;
}
}
public class SampleAdapter extends ArrayAdapter<SampleItem> {
public SampleAdapter(Context context) {
super(context, 0);
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row, null);
}
TextView title = (TextView) convertView.findViewById(R.id.row_title);
TextView subTitle = (TextView) convertView.findViewById(R.id.row_subtitle);
return convertView;
}
}
}
So I thougt that the list would display something like this:
title no. 0
some subtitle
-
title no. 1
some subtitle
-
title no. 2
some subtitle
-
etc.
but instead it just displays what I defined in the row.xml:
TITLE
SUBTITLE
-
TITLE
SUBTITLE
-
etc.
I think getView() needs to be changed (at least it doesn't make to much sense to me) but I'm not sure. Everything I try results in some nullpointer exception or worse.
Please, can someone tell me what I need to change?
Upvotes: 1
Views: 322
Reputation: 157457
The error is int the Adapter:
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row, null);
}
TextView title = (TextView) convertView.findViewById(R.id.row_title);
TextView subTitle = (TextView) convertView.findViewById(R.id.row_subtitle);
return convertView;
}
the getView
never fills up the title
and subtitle
. Second you never submit to the superclass
the dataset you want to show in the ListFragment
.
Here you have to ways. Or you ovveride the getCount
and getItem
and getItemId
methods, or feed up the superclass with the dataset, using this constructor. Read carefully the documentation.
Upvotes: 1