【问题标题】:Error: The non-generic type 'GenericTypeIndicator' cannot be used type arguments错误:不能使用非泛型类型“GenericTypeIndicator”类型参数
【发布时间】:2017-06-26 18:32:45
【问题描述】:

我不熟悉在 firebase 中执行查询。在我的代码下面,我使用快照将数据提取到我的列表视图中。我正在传递 List,但进一步阅读,我发现 List 无法转换为 Datasnaphot,答案需要使用 GenericTypeIndicator。我尝试使用它来将我的数据放入我的 listView 中,但这并不能解决问题。

为什么我会收到此错误Error: The non-generic type 'GenericTypeIndicator' cannot be used type arguments.

活动

     GenericTypeIndicator<List<GetUserContent>>messages = new GenericTypeIndicator<List<GetUserContent>>(){};


 public void OnDataChange(DataSnapshot snapshot)
        {
            messages.Clear();
            var items = snapshot.Child(post_key);
            messages.Add((GetUserContent)items);
            ViewAdapter adapter = new ViewAdapter(this, messages);
            Console.WriteLine("Data being fetched " + items);
            mylistview.Adapter = adapter;
        }

适配器

internal class ViewAdapter: BaseAdapter
    {
        private  List<GetUserContent> messages;

        public ViewAdapter(Activity activity,List<GetUserContent> messages)
        {
            this.activity = activity;
            this.messages = messages;
        }

GetUserContent 类

internal class GetUserContent
    {
        public string Email { get; set; }
        public string Message { get; set; }

        public GetUserContent()
        {

        }

        public GetUserContent(string Email, string Message){

            this.Email = Email;
            this.Message = Message;
        }

}

发帖

mLike.Child(post_key).Child(mAuth.CurrentUser.Uid).Push().SetValue(edtChat.Text);

【问题讨论】:

  • 发布GenericTypeIndicator的定义
  • @dcg 我没有任何定义:)。我在这里进行了进一步的阅读stackoverflow.com/questions/30744224/…
  • @dcg,你的陈述思想是什么意思?
  • 我以为是你的代码,我只是想看看它的定义来分析错误并给你(如果可能的话)我的帮助。
  • @dcg 我能够获取正在获取的项目,但我很难将其传递到我的列表视图

标签: c# android xamarin xamarin.android


【解决方案1】:

为什么我会收到此错误错误:非泛型类型 'GenericTypeIndicator' 不能使用类型参数。?

我不知道这个库是如何设计的,以及为什么GenericTypeIndicator 不能与类型参数一起使用。您可以在GooglePlayServicesComponents Github Repo提出问题。

但指的是Work with Lists of Data。您检索到的对象是Java.Lang.IIterable&lt;DataSnapShot&gt; 对象,不能直接转换为List。您可以使用以下代码强制转换为 List:

public void OnDataChange(DataSnapshot snapshot)
{
    var children = snapshot.Child("users")?.Children?.ToEnumerable<DataSnapshot>();
    List<HashMap> list = new List<HashMap>();

    foreach (DataSnapshot s in children)
    {
        list.Add((HashMap)s.Value);
    }
}

我的 Firebase 数据库结构如下:

更新:

目前,我无法直接发布我的自定义对象,我不断收到 序列化错误:

`Firebase.Database.DatabaseException: No properties to serialize found on class md5f5cedb36bdcec41a9de7aed50a9aade0.User`. 

但我通过使用HashMap 作为中间类型来解决它:

public class User:Java.Lang.Object
{
    public User() {}

    // convert current User to HashMap
    public HashMap ToMap()
    {
        HashMap map = new HashMap();
        map.Put("username", this.username);
        map.Put("email", this.email);
        return map;
    }


    public string username;
    public string email;

    public User(string username, string email)
    {
        this.username = username;
        this.email = email;
    }
}

然后像这样发布数据:

FirebaseDatabase.Instance
                .Reference
                .Child("users")
                .Push()
                .SetValue(new User("username", "email").ToMap());

并且没有完成检索HashMap并将其转换回User列表:

public void OnDataChange(DataSnapshot snapshot)
{
    var children = snapshot.Child("users")?.Children?.ToEnumerable<DataSnapshot>();
    List<User> list = new List<User>();

    HashMap map;
    foreach (DataSnapshot s in children)
    {
        map = (HashMap)s.Value;
        list.Add(new User(map.Get("username")?.ToString(), map.Get("email")?.ToString()));
    }
}

【讨论】:

  • 所以我必须将适配器中的列表也更改为 HashMap?因为在尝试了你的答案之后,我试图将列表传递给我的适配器,它给出了一个错误,说 cannot convert from HashMap to GetUserContent
  • 不,HashMap 只是我用来发布数据的类型。这只是一个例子,你应该用你的GetUserContent来替换它。
  • GetUserContent 实际上是我创建的一个类,用于返回用户的消息时间和消息内容,我相信它不能像 HashMap 那样工作。你可能想看看我的 GetUserContent
  • 当我用 GetUserContent 替换 HashMap 时,这一行 `list.Add((GetUserContent)s.Value);我收到错误 cannot convert Java.Lang.Object to GetuserContent `
  • 您确定使用GetuserContent 对象发布数据吗?另外,请确保您的数据库结构中只有我的答案中图片的级别列表数据。
猜你喜欢
  • 2019-01-09
  • 2016-10-07
  • 1970-01-01
  • 2018-09-20
  • 2011-10-19
  • 1970-01-01
  • 2021-09-01
  • 1970-01-01
  • 2022-11-07
相关资源
最近更新 更多