b.holz
b.holz

Reputation: 237

Xamarin.Android ListView in DialogFragment is not Displayed

I tried to create a listview with a custom adapter in a dialog fragment. The problem is that the Listview is not showing in the fragment. If I place the view into the main activity the list + its items are displayed. Maby somebody knows what is wrong with my code:

MainActivity class

public class MainActivity : Activity
{
    private List<string> serialNumbers = new List<string> { "test1", "test2" };
    private SerialDialog dialog = new SerialDialog();
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.Main);
        dialog.Show(FragmentManager, "SerialDialog");
    }
}

SerialDialog class
class SerialDialog : DialogFragment
{
    private View view = null;
    private List<string> serialNumbers = new List<string> { "test" };
    private ListView listView;

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        base.OnCreateView(inflater, container, savedInstanceState);
        view = inflater.Inflate(Resource.Layout.EnterSerial, container, false);
        listView = view.FindViewById<ListView>(Resource.Id.SerialListView);
        listView.Adapter = new CustomAdapter(Activity, serialNumbers);

        return view;
    }

    public override void OnActivityCreated(Bundle savedInstanceState)
    {
        Dialog.Window.RequestFeature(WindowFeatures.NoTitle);
        base.OnActivityCreated(savedInstanceState);
    }

}

CustomAdapter class

public class CustomAdapter : BaseAdapter<string>
{
    private List<string> items = new List<string>();
    private Activity context;
    public override string this[int position]
    {
        get
        {
            return items[position];
        }
    }
    public override int Count
    {
        get
        {
            return items.Count();
        }
    }

    public CustomAdapter(Activity context, List<string> items) : base()
    {
        this.context = context;
        this.items = items;
    }
    public void Add(string item)
    {
        items.Add(item);
        NotifyDataSetChanged();
    }

    public override long GetItemId(int position)
    {
        return 0;
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        return null;
    }
}

EnterSerial.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:minWidth="25px"
    android:minHeight="25px"
    android:background="@android:color/background_light">
    <ListView
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="200.5dp"
        android:id="@+id/SerialListView"
        android:layout_marginBottom="0.0dp" />
</LinearLayout>

I used a BaseAdapter because I need to Add elements later. The DialogFragment is blank. Debugging shows that the items are in the list.

Upvotes: 0

Views: 687

Answers (2)

Grace Feng
Grace Feng

Reputation: 16652

The DialogFragment is blank. Debugging shows that the items are in the list.

As @apineda mentioned, your background of DialogFragment is white and you didn't implement the GetView in your adapter, but since you can run the code, I suppose that you've done this work just didn't post it.

Tested with the very basic layout for listview item:

public override View GetView(int position, View convertView, ViewGroup parent)
{
    View view = convertView; // re-use an existing view, if one is available
    if (view == null) // otherwise create a new one
        view = context.LayoutInflater.Inflate(Android.Resource.Layout.SimpleListItem1, null);
    var tv = view.FindViewById<TextView>(Android.Resource.Id.Text1);
    tv.Text = items[position];
    return view;
}

By default the text's color is also white, so the items cannot be seen. You can either remove the color android:background="@android:color/background_light" in your EnterSerial.axml or add a text color to your listview item to test your code.

Also there're a few issue with your code, besides what apineda mentioned, usually we return the position in GetItemId:

public override long GetItemId(int position)
{
    return position;
}

In addition, I think you may want to pass List<string> from your MainActivity to your DialogFragment, then we can for example code like this:

FragmentTransaction ft = FragmentManager.BeginTransaction();
ft.AddToBackStack(null);
Bundle args = new Bundle();
args.PutStringArrayList("list", serialNumbers);
dialog.Arguments = args;
dialog.Show(ft, "TAG");

In the OnCreate method of your Dialog:

public override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    var args = Arguments;
    serialNumbers = args.GetStringArrayList("list").ToList();
}

Upvotes: 1

pinedax
pinedax

Reputation: 9346

I see a few issues with the code you posted:

Count() doesn't exist as this is not a method for the List

public override int Count
{
    get
    {
        return items.Count();
    }
}

You are not implementing your GetView method in the adapter.

public override View GetView(int position, View convertView, ViewGroup parent)
{
    return null;
}

To fix the issue you have with the list in blank move the adapter creation to the OnViewCreated:

public override void OnViewCreated (View view, Bundle savedInstanceState)
{
    base.OnViewCreated (view, savedInstanceState);

    listView.Adapter = new CustomAdapter (Activity, serialNumbers);
}

Also remember to set the color to the TextView that will be displaying your data as the default text color on the DialogFragments is white.

Upvotes: 0

Related Questions