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.
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
}
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
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.
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.
adapter.notifyDataSetChanged();
lv.setAdapter(adapter);
swap the positions
Layoutinflator=getLayoutInflator();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With