开发者

Problem overriding getView()

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.开发者_Go百科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'


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜