0

我的应用程序显示来自 MySql 数据库的数据。为此,我使用 AsyncTask。适配器获取更新的值,但列表视图没有反映该更改。我尝试过 NotifyDataSetInvalidated() 和 Invalidate() 但它没有用。

主屏幕布局:

<?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">
    <ListView
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listView1" />
</LinearLayout>

自定义视图布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/Text"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/Text1"
        android:layout_width=".50px"
        android:layout_weight=".50"
        android:gravity="center"
        android:textSize="20px"
        android:layout_height="wrap_content"
        android:textColor="#FF267F00" />
    <TextView
        android:id="@+id/Text2"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="20px"
        android:layout_width=".50px"
        android:layout_weight=".50"
        android:textColor="#FF267F00" />
</LinearLayout>  

编辑 异步任务代码:

public class FetchData : AsyncTask<string, string, string>
    {
        public ListView listView;
        public Activity HomeScreenActivity;
        public HomeScreenAdapter adapter;
        protected override void OnPreExecute()
        {
            base.OnPreExecute();

        }
        protected override Java.Lang.Object DoInBackground(params Java.Lang.Object[] native_parms)
        {
            CallSoap callSoap = new CallSoap();
            return callSoap.Call();
        }

        protected override string RunInBackground(params string[] @params)
        {
            CallSoap callSoap = new CallSoap();
            return callSoap.Call();
        }
        protected override void OnPostExecute(string result)
        {

            base.OnPostExecute(result);
            adapter.items = new List<TableItem>();


            System.String result0 = result.Replace("string=", "").Trim();
            System.String result1 = result0.Remove(0, 8);
            System.String result2 = result1.Remove(result1.Length - 1, 1);
            if (result2 == "Server not available; ")
            {
                adapter.items.Add(new TableItem("", "Server not available", Android.Graphics.Color.Red));
            }
            else if (result2 == "Internet connection not available;")
            {
                adapter.items.Add(new TableItem("Internet connection not available", "", Android.Graphics.Color.Red));
            }
            else
            {
                List<System.String> result3 = result2.Split(';').ToList();


                for (int i = 0; i < result3.Count - 1; i++)
                {
                    if (result3[i + 2].ToString().Trim().Equals("1"))
                    {
                        if (result3[i + 3].ToString().Trim().Equals("1"))
                        {
                            adapter.items.Add(new TableItem(result3[i], result3[i + 1].ToString(), Android.Graphics.Color.Blue));
                        }
                        else
                        {
                            adapter.items.Add(new TableItem(result3[i], result3[i + 1].ToString(), Android.Graphics.Color.Red));
                        }
                    }
                    i = i + 3;
                }
            }
            adapter.UpdateListView(adapter.items);
            listView.Adapter = adapter;
            adapter.NotifyDataSetInvalidated();


 }
   }
  }
4

0 回答 0