Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 897
user1272934 Avatar asked Mar 16 '12 00:03

user1272934


5 Answers

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
}
like image 200
fischer_zh Avatar answered Nov 06 '22 20:11

fischer_zh


You are not supplying any data to it currently. If you still want to see whats happening in the list, override the getCount() function.

I think it is something of this sort.

public int getCount(){
    return 1;
}

This will draw one item in your list. And also call your getView(). And ya once you find out how to supply your data source, change the getCount() to the size of you list. Check out a tutorial here

http://www.shubhayu.com/android/listview-with-arrayadapter-and-customized-items

like image 45
Shubhayu Avatar answered Nov 06 '22 18:11

Shubhayu


When we are creating a custom arrayadapter we must be using either

public ArrayAdapter (Context context, int textViewResourceId, T[] objects)

public ArrayAdapter (Context context, int resource, int textViewResourceId, T[] objects)

public ArrayAdapter (Context context, int textViewResourceId, List<T> objects)

public ArrayAdapter (Context context, int resource, int textViewResourceId, List<T> objects)

if we are not using above mention adapter, we need to override getCount() method.

like image 9
siva Avatar answered Nov 06 '22 19:11

siva


it looks like you haven't specified a data source for your adapter. A data source is any thing which provides data to be displayed , for ex an arraylist or a cursor. If there is no data from a data source, getview() wont be called at all.

like image 2
Akhil Avatar answered Nov 06 '22 19:11

Akhil


  adapter.notifyDataSetChanged();
  lv.setAdapter(adapter);

swap the positions

Layoutinflator=getLayoutInflator();
like image 1
Aashish Bhatnagar Avatar answered Nov 06 '22 19:11

Aashish Bhatnagar