I have a very basic ListView in android and had set a very basic adapter. My problem is that the list view does not show anything, regardless of the adapter and the notifyDataSetChanged();
Here is my code: XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="@string/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></TextView>
<ListView android:id="@+id/selectView"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</ListView>
</RelativeLayout>
The Activity code:
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import com.androidcourse.phonemapper.R;
import com.androidcourse.phonemapper.model.SelectViewAdapter;
public class SelectActivity extends Activity {
private ListView mListView;
private SelectViewAdapter mAdapter;
@Override
public void onCreate(Bundle savedState) {
super.onCreate(savedState);
setContentView(R.layout.select_activity);
initializeListView();
}
private void initializeListView() {
mListView = (ListView) findViewById(R.id.selectView);
mAdapter = new SelectViewAdapter(this);
mListView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
}
@Override
public void onResume() {
super.onResume();
}
}
And the Adapter code:
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class SelectViewAdapter extends BaseAdapter {
private Context mContext;
private TextView mMockTextView;
public SelectViewAdapter(Context cnt) {
mContext = cnt;
mMockTextView = new TextView(mContext);
mMockTextView.setText("Test text");
mMockTextView.setBackgroundColor(Color.CYAN);
}
@Override
public int getCount() {
return 3;
}
@Override
public Object getItem(int position) {
return mMockTextView;
}
@Override
public long getItemId(int position) {
return 3;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return mMockTextView;
}
}
The problem is that nothing is shown on the screen. A black screen (and the first text view from the XML) is all I get. I cannot see the mockTextView and its text. Apparently I am doing something quite wrong, but I cant figure out what.
A list view is an adapter view that does not know the details, such as type and contents, of the views it contains. Instead list view requests views on demand from a ListAdapter as needed, such as to display new views as the user scrolls up or down. In order to display items in the list, call setAdapter(android.
If you only want to set width and height of listview. you can set it by right clicking on listview in xml and select set width / set height and set it, for ex. 20dp, 50 dp etc..
A few things I can think of.
First, Your RelativeLayout has no relative positioning information. I would assume you meant to put this in a LinearLayout with orientation set to vertical from what you describe. My guess is that the list is not actually being drawn since it isn't even anchored to anything in the current RelativeLayout. If you stick with the RelativeLayout, make sure to put an id on the app_name TextView and position the ListView under it via layout_below.
LinearLayout Solution
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="@string/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></TextView>
<ListView android:id="@+id/selectView"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</ListView>
</LinearLayout>
RelativeLayout Solution:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/app_name_text"
android:text="@string/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
></TextView>
<ListView android:id="@+id/selectView"
android:layout_below="@id/app_name_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</ListView>
</RelativeLayout>
Next, your getView() returns the same textView for all 3 indexes. It's not a problem to display the same view over multiple indexes however with a list size of three, I am betting that the screen can display all three at the same time. And since a View can't be in more than one position at a time, I actually would expect this to fail so I doubt it is even getting to this code yet. Try creating a new TextView for each getView(). Also your MockTextView doesn't have layout params of it's own. So laying it out within a listView cell might not be happening either. So you can give it params of type AbsListView.LayoutParams(WRAP_CONTENT, WRAP_CONTENT). Again though I would expect this to error if it got to the original code.
getView() tidy up:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
TextView textView = new TextView(parent.getContext());
textView.setText("Position is:"+position);
textView.setBackgroundColor(Color.CYAN);
textView.setLayoutParams(new AbsListView.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
convertView = textView;
}
return mMockTextView;
}
And lastly the wrap_content height of your list can sometimes be problematic. I am not aware of all the scenarios. If you end up changing to a LinearLayout try setting your layout_height of the list view to 0 and then set the layout_weight=1. This forces the linear layout to inflate it into more space.
LinearLayout Weight Solution:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="@string/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></TextView>
<ListView android:id="@+id/selectView"
android:layout_weight="1"
android:layout_height="0dp"
android:layout_width="wrap_content">
</ListView>
</LinearLayout>
I was using the Data Binding example from the book Hello Android by Ed Burnette (great book).
I changed the item layout from A RelativeLayout to a LinearLayout; however, I did not add an orientation when I made the change.
Once I added android:orientation="vertical" everything worked fine.
Two hours of my life on this one.
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