maher2020
maher2020

Reputation: 61

update listview after db insert

I want to update listview after insert new data.

When updating with the main(main activity) interface, the process is performed well. When calling the update from the first interface To the second(SecondActivity) interface updating does not update. What is wrong with updating the second(SecondActivity) interface?

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

Upvotes: 0

Views: 46

Answers (1)

Cherry Bu - MSFT
Cherry Bu - MSFT

Reputation: 10356

According to your code, the sqldbc.adapter and sqldbc.listtable are all null, you can add break point to check it.

If you want to update ListView in another activity, I suggest you can create static ListView and 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);

When you go back, you can see the ListView data update.

Upvotes: 1

Related Questions