Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
576 views
in Technique[技术] by (71.8m points)

android - The getView() method of ArrayAdapter is not getting called

I am very new to android development. I am trying to a build an application with tags (which are added through fragments). In one of the fragments, I am trying to display a list. This list has been populated using ListAdapter which I have extended from ArrayAdapter and I have overloaded getView() method. This is my fragment

public class tabcontentActivity extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        if (container == null) {
            return null;
        }
        View v = (LinearLayout) inflater.inflate(R.layout.tablayout, container,
            false);
        ListView lv = (ListView) v.findViewById(R.id.listview1);
        ListViewAdapter adapter = new ListViewAdapter(container.getContext(),
            android.R.layout.simple_list_item_1, R.id.textview1);
        adapter.notifyDataSetChanged();
        lv.setAdapter(adapter);

        return v;
    }
}

And this is how I have implemented the ListAdapter

public class ListViewAdapter extends ArrayAdapter {
    Context context1;

    public ListViewAdapter(Context context,int resource, int textViewResourceId) {
        super(context,resource,textViewResourceId);
        this.context1 = context;
        System.out.println("here aswell!!!!!!");
        // TODO Auto-generated constructor stub
    }

    public View getView(int arg0, View convertView, ViewGroup arg2) {
        // TODO Auto-generated method stub
        System.out.println("@@@I AM HERE@@@");
        LayoutInflater inflater = LayoutInflater.from(context1);
        convertView = inflater.inflate(R.layout.tablayout, null);

        TextView wv = (TextView) convertView.findViewById(R.id.textview1);
        String summary = "<html><body><h1>This is happening!!!</h1></body></html>";
        wv.setText(Html.fromHtml(summary));

        convertView.setTag(wv);

        return convertView;
    }
}

The layout xml is

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

    <ListView
        android:id="@+id/listview1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>

    <TextView
        android:id="@+id/textview1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="myreader" />

</LinearLayout>

Please help me to find a way by which I can make this work.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You just forgot to provide the data to the constructor and then make the correct call within the constructor:

public ListViewAdapter(Context context,int resource, int textViewResourceId, List<YourObjectType> items) {
    super(context,resource,textViewResourceId, items);
    this.context1 = context;
    // TODO Auto-generated constructor stub
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...