简体   繁体   English

DialogFragment中的Xamarin.Android ListView不显示

[英]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. 问题是Listview没有显示在片段中。 If I place the view into the main activity the list + its items are displayed. 如果将视图放入主要活动中,则显示列表及其项目。 Maby somebody knows what is wrong with my code: Maby有人知道我的代码出了什么问题:

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 CustomAdapter类

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 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. 我使用了BaseAdapter,因为以后需要添加元素。 The DialogFragment is blank. DialogFragment为空白。 Debugging shows that the items are in the list. 调试显示项目在列表中。

The DialogFragment is blank. DialogFragment为空白。 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. 正如@apineda所提到的,您的DialogFragment背景是白色的,您没有在适配器中实现GetView ,但是由于您可以运行代码,所以我想您已经完成了这项工作,只是没有发布它。

Tested with the very basic layout for listview item: 测试了listview项目的基本布局:

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. 您可以在EnterSerial.axml删除android:background="@android:color/background_light"颜色,也可以在列表视图项中添加文本颜色以测试代码。

Also there're a few issue with your code, besides what apineda mentioned, usually we return the position in GetItemId : 除了apineda提到的内容外,您的代码也存在一些问题,通常我们返回GetItemIdposition

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: 另外,我认为您可能希望将List<string>MainActivity传递到DialogFragment ,然后我们可以例如这样的代码:

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: 在对话框的OnCreate方法中:

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

I see a few issues with the code you posted: 我发现您发布的代码存在一些问题:

Count() doesn't exist as this is not a method for the List Count()不存在,因为这不是List的方法

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

You are not implementing your GetView method in the adapter. 您没有在适配器中实现GetView方法。

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 : 要解决列表中的空白问题,请将适配器创建移至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. 还请记住将颜色设置为将显示数据的TextView,因为DialogFragments上的默认文本颜色为白色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM