[插入数据库后更新listview

问题描述 投票:0回答:1

我想在插入新数据后更新列表视图。

使用主(主要活动)界面进行更新时,该过程执行得很好。从第一个接口调用更新到第二个(SecondActivity)接口时,更新不会更新。更新第二个(SecondActivity)接口有什么问题?

//main activity update work well
   public ListView listtable;
    public ArrayAdapter<string> adapter;
    public static List<string> Original;

//update

  fillarray();
                adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, Original);
                listtable.Adapter = adapter;

//SecondActivity  not work

  MainActivity sqldbc = new MainActivity();
 sqldbc.fillarray();

                sqldbc.adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, MainActivity.Original);
                sqldbc.listtable.Adapter = sqldbc.adapter;

xamarin.android
1个回答
1
投票
如果要在另一个活动中更新ListView,建议您创建静态ListView和List。

Mainactivity.cs:

public static ListView listview1; public static List<string> list; //insert data into db method. adddata(); listview1.Adapter = new ArrayAdapter(this,Android.Resource.Layout.SimpleListItem1, list);

SecondActivity.cs:

 private MainActivity mactivity;
 mactivity = new MainActivity();
        mactivity.adddata();
        MainActivity.listview1.Adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1, MainActivity.list);

返回时,可以看到ListView数据更新。

© www.soinside.com 2019 - 2024. All rights reserved.