Difficulty listing Sqlite result in Listview Android

Asked

Viewed 180 times

1

I’m creating an application to train mobile development, I’m doing for Android in Visual Studio, using Xamarin. I’m having difficulty listing a result of Sqlite.

I created two Activity, one to register and already list, and the other just to list the data registered in a Listview. The problem is, in the first Activity, when entering a data, it lists the others (that already exist in the bank) normally, but in the second Activity, when loading the app, it throws an exception: "Unhandled Exception: System.Nullreferenceexception: Object Reference not set to an instance of an Object. occurred". The exception is found in the following code snippet:

lista.Adapter = adapter; 

Follows the codes:

Function that brings the results:

public List<Abastece> GetAbastecimentos()
    {
        try
        {
            using (var conexao = new SQLiteConnection(System.IO.Path.Combine(pasta, "Abastece.db")))
            {
                return conexao.Table<Abastece>().ToList();
            }
        }
        catch (SQLiteException ex)
        {
            Log.Info("SQLiteEx", ex.Message);
            return null;
        }
    } 

Code in C#:

namespace Control_Car
{
[Activity(Label = "ListaAbast", MainLauncher = true)]
public class ListaAbast : Activity
{
    Button button;
    ListView lista;
    DataBase db;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        CriarBancoDeDados();
        base.OnCreate(savedInstanceState);

        button = (Button)FindViewById(Resource.Id.bt_registrar);
        lista = (ListView)FindViewById(Resource.Id.lista);
        SetContentView(Resource.Layout.ListaAbast);

        var listaAbast = db.GetAbastecimentos();
        ArrayAdapter adapter = new ArrayAdapter<Abastece>(this, 
        Android.Resource.Layout.SimpleListItemMultipleChoice, listaAbast);
        lista.Adapter = adapter;
        lista.Activated = true;
    }

    private void CriarBancoDeDados()
    {
        db = new DataBase();
        db.CriarBanco();
    }
}
}

Just to reinforce, this code in C# is in both Ctivity, but if I try to call the function of filling the Listview in Oncreate it throws the exception. To illustrate I will put the code of the other Activity, in which I only list the data when entering some other:

namespace Control_Car
{
[Activity(Label = "Abastecer", ScreenOrientation = 
ScreenOrientation.Portrait)]
public class Abastecer : Activity
{
    Button btn;
    EditText valorKM, preco, litros, nomePosto;
    ListView ltAbastecimento;
    DataBase db;
    string[] items = { };
    List<Abastece> listaAbast = new List<Abastece>();

    protected override void OnCreate(Bundle savedInstanceState)
    {
        CriarBancoDeDados();


        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.Abastecer);

        btn = (Button)FindViewById(Resource.Id.bt_registrar);
        valorKM = (EditText)FindViewById(Resource.Id.valor_km);
        preco = (EditText)FindViewById(Resource.Id.valor_preco);
        litros = (EditText)FindViewById(Resource.Id.valor_litros);
        nomePosto = (EditText)FindViewById(Resource.Id.nome_posto);
        ltAbastecimento = (ListView)FindViewById(Resource.Id.lista);
        CarregaDados();

        btn.Click += delegate
        {
            Abastece abastece = new Abastece()
            {
                ValorKM = Double.Parse(valorKM.Text),
                Preco = Double.Parse(preco.Text),
                Litros_qnt = Double.Parse(litros.Text),
                PostoUt = nomePosto.Text
            };
            db.InserirAbastecimento(abastece);
            CarregaDados();
        };
    }

    private void CarregaDados()
    {
        listaAbast = db.GetAbastecimentos();
        ArrayAdapter<Abastece> adapter = new ArrayAdapter<Abastece>(this, 
    Android.Resource.Layout.SimpleListItem1, listaAbast);
        ltAbastecimento.Adapter = adapter;
    }

    private void CriarBancoDeDados()
    {
        db = new DataBase();
        db.CriarBanco();
    }
}
}
  • Have you tried breakpoint debug mode on the line lista.Adapter = adapter and see if the Adapter variable is null? This exception is given when you try to use a variable that has null value

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.