我的应用程序显示来自MySql数据库的数据。为此我使用的是AsyncTask。适配器获得更新值但listview不反映该更改。我尝试过NotifyDataSetInvalidated()和Invalidate()但它没有工作。
HomeScreenLayout:
<?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>
CustomViewLayout:
<?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>
修改 AsyncTaskCode:
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();
}
}
}