3

I keep getting an error when overriding my getview method. This is what I have:

public class UnwatchedEpisodesActivity extends ListActivity{

private String[] values = new String[]{"Row 1", "Row 2", "Row 3"};

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.episode_main);
    setListAdapter(new EpisodeListAdapter());

}

private class EpisodeListAdapter extends ArrayAdapter<String>{

    private LayoutInflater li = LayoutInflater.from(this.getContext());

    public EpisodeListAdapter(){
        super(UnwatchedEpisodesActivity.this, 0, values);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null){
            convertView = li.inflate(R.layout.episode_row, parent, false);
        }

        ((TextView) convertView.findViewById(R.id.text1)).setText(values[position]);
        return convertView;
    }

}

and my xml is this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/ep_layout"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:id="@android:id/text1" />
</LinearLayout>

(I also tried without the linearlayout, it makes no difference) I get this error.

You guys any idea?

Also another question: What should I do if I want to seperate the values in groups? I want a list to show something like:

-----------
first part
-----------
Row 1
Row 2
-----------
second part
-----------
Row 3

Obviously this is just an example; basically I want to know how to add those 'seperators'

2 Answers2

2

you should set your TextView's id by

android:id="@+id/text1"

This part: android:id="@android:id/text1" is incorrect!

For separating your items you should use an ExpandableListActivity rather then a ListActivity, where your groups would be the parts (first, second, etc), and the child list would be your current values.

In this case, you should use a BaseExpandableListAdapter to populate your view.
Though there are a few methods you need to override, with a proper data structure it won't be a problem. You could refer for instance to this example.

Then the body of your getView method should be put inside the BaseExpandableListAdapter's getChildView method, and you should use an other TextView or even more complex layout for the parts in getGroupView override.

This way you can have your episodes grouped into parts.

Community
  • 1
  • 1
rekaszeru
  • 19,130
  • 7
  • 59
  • 73
1

Use android:id="@+id/text1" instead of android:id="@android:id/text1" for the 1st part.

For the 2nd issue , as far as what I understood about your need:

you should inflate different xml for different position in the getView() method. For example:

if (position == 0) {
    convertView = li.inflate(R.layout.episode_row, parent, false);
    //fetch the text view and other things and set their value
    return convertView;
} else if (position == 1) {
    //similarly do here also and return convertview
}

Hope this will help to solve your issue.

sandrstar
  • 12,503
  • 8
  • 58
  • 65
Dinesh Sharma
  • 11,533
  • 7
  • 42
  • 60
  • Is `getView()` looped for each position of the Array? I can see this work if it is, and it looks quite easy.. –  May 08 '11 at 15:13
  • yes , getView() is looped for each position and thats very easy to do and you can customize it according to you need. All the best...:) – Dinesh Sharma May 08 '11 at 15:22
  • Note, that though this solution can be applied to a flat data structure, you either need to insert the header values into you episodes list, or you must be able to determine the first episode of each part. position = 0 applies for the first item, and in the if clause above it displays the header instead of the first iem! For hierarchical data structures the ExpandableListView is the solution. – rekaszeru May 09 '11 at 05:48