首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Xamarin C# -每秒刷新网格视图的最快方法是什么?

Xamarin C# -每秒刷新网格视图的最快方法是什么?
EN

Stack Overflow用户
提问于 2019-01-04 06:24:31
回答 1查看 338关注 0票数 0

我有个问题。

在我的Android应用程序中,我使用:Android.Support.V4.View.ViewPager在摘要和钱包之间切换。摘要页用3个TextViews填充,钱包用1个GridView创建。这两个页面都填充了来自HTTPS调用的数据,其中的响应将被解析为一个列表。现在我想每秒刷新这两个页面。所以我试了一下:

代码语言:javascript
运行
复制
public void LoadOrderPage()
{
    Android.Support.V4.View.ViewPager SummaryWalletSwitcher = FindViewById<Android.Support.V4.View.ViewPager>(Resource.Id.SummaryWalletSwitcher);

    List<View> viewlist = new List<View>();
    viewlist.Add(LayoutInflater.Inflate(Resource.Layout.AgentSummary, null, false));
    viewlist.Add(LayoutInflater.Inflate(Resource.Layout.AgentWallet, null, false));
    SummaryWalletAdapter ViewSwitchAdapter = new SummaryWalletAdapter(viewlist);
    SummaryWalletSwitcher.Adapter = ViewSwitchAdapter;

    Timer AgentInfo_Timer = new Timer();
    AgentInfo_Timer.Interval = 1000;
    AgentInfo_Timer.Elapsed += LoadAgentInfo;
    AgentInfo_Timer.Enabled = true;
}

public void LoadAgentInfo(object sender, ElapsedEventArgs e)
{
    TextView TextPortfolioValue = FindViewById<TextView>(Resource.Id.txtPortfolioValue);
    TextView TextValueUSDT = FindViewById<TextView>(Resource.Id.txtValueUSDT);
    TextView TextTotalValue = FindViewById<TextView>(Resource.Id.txtTotalValue);
    GridView GridviewWallet = FindViewById<GridView>(Resource.Id.GridviewWallet);

    if (FirstWalletRun == true)
    {
        List<wallet> EmptyWalletList = new List<wallet>();
        WalletListAdapter = new WalletListAdapter(this, EmptyWalletList);
        GridviewWallet.Adapter = WalletListAdapter;
        FirstWalletRun = false;
    }

    PortfolioValue = 0;
    ValueUSDT = 0;
    TotalValue = 0;
    string response = "";

    AgentId = getSelectedAgentId();
    if (AgentId == 0)
    {
        AgentId = 1;
    }

    try
    {
        WebClient client = new WebClient();
        var reqparm = new System.Collections.Specialized.NameValueCollection();
        reqparm.Add("agentid", AgentId.ToString());
        reqparm.Add("devicetoken", DeviceToken);
        byte[] responsebytes = client.UploadValues("https://www.test.nl/getagentwallet.php", "POST", reqparm);
        IgnoreBadCertificates();
        response = Encoding.UTF8.GetString(responsebytes);
        response = response.Replace("\n", "").Replace("\t", "");
    }
    catch (Exception ex)
    {

        string exFullName = (ex.GetType().FullName);
        string ExceptionString = (ex.GetBaseException().ToString());

        TextPortfolioValue.Text = "Unknown";
        TextValueUSDT.Text = "Unknown";
        TextTotalValue.Text = "Unknown";
    }

    if (response != "No updates")
    {

        //Parse json content
        var jObject = JObject.Parse(response);

        //Create Array from everything inside Node:"Coins"
        var walletPropery = jObject["Wallet"] as JArray;

        //Create List to save Coin Data
        walletList = new List<wallet>();

        //Find every value in Array: coinPropery
        foreach (var property in walletPropery)
        {
            //Convert every value in Array to string
            var propertyList = JsonConvert.DeserializeObject<List<wallet>>(property.ToString());

            //Add all strings to List
            walletList.AddRange(propertyList);
        }

        //Get all the values from Name, and convert it to an Array
        string[][] NamesArray = walletList.OrderBy(i => i.AgentId)
            .Select(i => new string[] { i.AgentId.ToString(), i.Coin, i.Quantity.ToString(), i.AvgPrice.ToString(), i.Value.ToString() })
            .Distinct()
            .ToArray();

        foreach (string[] str in NamesArray)
        {
            if (str[1] != "USDT")
            {
                PortfolioValue += decimal.Parse(str[4]);
            }
            else
            {
                ValueUSDT += decimal.Parse(str[4]);
            }
        }

        TotalValue = PortfolioValue + ValueUSDT;
        TextPortfolioValue.Text = Math.Round(PortfolioValue, 8).ToString();
        TextValueUSDT.Text = Math.Round(ValueUSDT, 8).ToString();
        TextTotalValue.Text = Math.Round(TotalValue, 8).ToString();

        SortedWalletList = walletList.OrderBy(o => o.Coin).ToList();

        if (WalletListAdapter == null)
        {
            //Fill the DataSource of the ListView with the Array of Names
            WalletListAdapter = new WalletListAdapter(this, SortedWalletList);
            GridviewWallet.Adapter = WalletListAdapter;
        }
        else
        {
            WalletListAdapter.refresh(SortedWalletList);
            AgentInfoNeedsUpdate = true;
        }
    }
    else
    {
        AgentInfoNeedsUpdate = false;
    }
}

在我的WalletListAdapter中,我创建了刷新函数:

代码语言:javascript
运行
复制
public void refresh(List<wallet> mItems)
{
    this.mItems = mItems;
    NotifyDataSetChanged();
}

但是GridviewWallet永远不会填满或者不显示。我做错了什么?

编辑:

也许WalletListAdapter中有一些错误,所以下面是类的代码:

代码语言:javascript
运行
复制
public class WalletListAdapter : BaseAdapter<wallet>
{
    public List<wallet> mItems;
    private Context mContext;

    public WalletListAdapter(Context context, List<wallet> items)
    {
        mItems = items;
        mContext = context;
    }

    public override int Count
    {
        get { return mItems.Count; }
    }

    public void refresh(List<wallet> mItems)
    {
        this.mItems = mItems;
        NotifyDataSetChanged();
    }

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

    public override wallet this[int position]
    {
        get { return mItems[position]; }
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        View row = convertView;

        if (row == null)
        {
            row = LayoutInflater.From(mContext).Inflate(Resource.Layout.walletlist_row, null, false);

            var txtWalletCoin = row.FindViewById<TextView>(Resource.Id.txtWalletCoin);
            var txtWalletQuantity = row.FindViewById<TextView>(Resource.Id.txtWalletQuantity);
            var txtAvgPrice = row.FindViewById<TextView>(Resource.Id.txtWalletAvgPrice);
            var txtWalletValue = row.FindViewById<TextView>(Resource.Id.txtWalletValue);
            var txtProfitUSDT = row.FindViewById<TextView>(Resource.Id.txtWalletProfitUSDT);
            var txtProfitPerc = row.FindViewById<TextView>(Resource.Id.txtWalletProfitPerc);

            row.Tag = new WalletViewHolder()
            {
                txtWalletCoin = txtWalletCoin,
                txtWalletQuantity = txtWalletQuantity,
                txtAvgPrice = txtAvgPrice,
                txtWalletValue = txtWalletValue,
                txtProfitUSDT = txtProfitUSDT,
                txtProfitPerc = txtProfitPerc
            };
        }

        var holder = (WalletViewHolder)row.Tag;

        holder.txtWalletCoin.Text = mItems[position].Coin;
        holder.txtWalletQuantity.Text = Math.Round(mItems[position].Quantity, 2).ToString();
        holder.txtAvgPrice.Text = Math.Round(mItems[position].AvgPrice, 2).ToString();
        holder.txtWalletValue.Text = Math.Round(mItems[position].Value, 2).ToString();

        if (mItems[position].Coin != "USDT")
        {
            holder.txtProfitUSDT.Text = Math.Round(mItems[position].ProfitUSDT, 2).ToString();
            holder.txtProfitPerc.Text = Math.Round(mItems[position].ProfitPerc, 1).ToString();
        }
        else
        {
            holder.txtProfitUSDT.Text = Math.Round(0.00, 2).ToString();
            holder.txtProfitPerc.Text = Math.Round(0.00, 1).ToString();
        }

        return row;
    }
}
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54030637

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档