Reputation: 567
I'm trying to display information from User class in a ListView. When I run the application, I get only an empty screen. I'm using a ListView in activity_main.xml which looks like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
I have a data model xml which will be inflated as each item in the list. This xml looks like this:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textline"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:text="Description"
android:textSize="12sp"
android:textColor="@android:color/black"
/>
I wrote ListAdapter which extends BaseAdapter. I declared an ArrayList. I populated the ArrayList in the constructor of ListAdapater. The override methods of ListAdapter looks like this:
@Override
public int getCount() {
return userList.size();
}
@Override
public Object getItem(int position) {
return userList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
if(view==null){
Log.e("Info","Inflating view");
inflater=LayoutInflater.from(parent.getContext());
view=inflater.inflate(R.layout.data_model, parent,false);
}
User user=userList.get(position);
TextView tv=(TextView)view.findViewById(R.id.textline);
tv.setText(user.getName());
return null;
}
The onCreate() of MainActivity looks like this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv=(ListView)findViewById(R.id.listview);
ListAdapter adapter=new ListAdapter();
lv.setAdapter(adapter);
}
I have no idea why am I getting the empty white screen. Also, when I placed Log() statements as first statement of each method of ListAdapter, I saw the Log() statements being printed twice in LogCat.
Any pointers towards the solution will be helpful.
Upvotes: 0
Views: 962
Reputation: 454
@Override
public View getView(int position, View view, ViewGroup parent) {
if(view==null){
Log.e("Info","Inflating view");
inflater=LayoutInflater.from(parent.getContext());
view=inflater.inflate(R.layout.data_model, parent,false);
}
User user=userList.get(position);
TextView tv=(TextView)view.findViewById(R.id.textline);
tv.setText(user.getName());
return view;
}
Upvotes: 0
Reputation: 255
The getView method is returning null for each position. You need to return your view.
Upvotes: 0
Reputation: 30804
In Adapter.getView
you're returning null
when you should be returning view
, in your case.
Upvotes: 1