Xamarin C#-每秒刷新gridview的最快方法是什么

时间:2019-01-03 22:24:31

标签: c# android gridview xamarin.forms textview

我有问题。

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

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中创建刷新功能:

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

但是GridviewWallet永远不会被填充或无法显示。我究竟做错了什么?

编辑:

WalletListAdapter也许有问题,所以这是该类的代码:

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;
    }
}

1 个答案:

答案 0 :(得分:0)

希望此讨论提供一些见解,以使用Xamarin修复gridview刷新。根据它,必须重新创建网格。 https://forums.xamarin.com/discussion/115256/refresh-a-gridview