1

I want to fill the array from the sqlite database .

I filled the database with data, but I could not view the data inside the array to view it in listview again.

Note that the listview consists of a view inside (Heading,SubHeading).

  public  List<TableItem> tableitems;

 public void fillarray()
        {
           var db = new SQLiteConnection(dbPath);
            tableitems = new List<TableItem>();
            var table = db.Table<SqliteDB.Departments>();
          foreach (var s in table)
            {
             new TableItem() { Heading = s.Id.ToString(), SubHeading = s.FullName.ToString() };
            }
           }

1 Answer 1

1

According to your code,you have one table name Departments in sqlite database, you want to get data from this table, and populating these data into tableitems and display into ListView, am I right?

If yes, there are two steps you need to do, first you need to get data from sqlite database , second you need to create custom row in ListView to display tableitems.

 private SQLiteConnection db;
 private List<TableItem> items;

 private void Btnget_Click(object sender, System.EventArgs e)
    {
       string dpPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "Persons.db3");
       db = new SQLiteConnection(dpPath);
       foreach(var de in db.Table<Departments>())
        {
            TableItem item = new TableItem();
            item.Heading = de.id.ToString();
            item.SubHeading = de.FullName;
            items.Add(item);
        }

        listview1.Adapter = new ListViewAdapter(this,items);
    }

The Custom row in ListView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:id="@+id/textView1"/>
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:id="@+id/textView2"/>
</LinearLayout>

The ListViewAdapter.cs:

 public class ListViewAdapter : BaseAdapter<TableItem>
{
    List<TableItem> items;
    Activity context;

    public ListViewAdapter(Activity context, List<TableItem> items) :base()
    {
        this.context = context;
        this.items = items;
    }
    public override TableItem this[int position]
    {
        get { return items[position]; }
    }

    public override int Count
    {
        get { return items.Count; }
    }

    public override long GetItemId(int position)
    {
        return position;
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        var item = items[position];

        View view = convertView;
        if (view == null) // no view to re-use, create new
            view = context.LayoutInflater.Inflate(Resource.Layout.CustomRow, null);
        view.FindViewById<TextView>(Resource.Id.textView1).Text = item.Heading;
        view.FindViewById<TextView>(Resource.Id.textView2).Text = item.SubHeading;


        return view;
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Can you add an image and view it in the same way?
@maher2020 You mean that you get data that having image from sqlite database, you also want to display the image in listview? Can you describe more detail info that you want to do.
Yes, save or insert the image as a sqllite data byte, and replay it with a display in listview
@maher2020 I do one sample, to simulate saving data (including image) into sqlite data , and then getting data from sqlite database and display in ListView, you can take a look.This is my sample:github.com/CherryBu/FillListView
Thank you Cherry Bu You did a lot with me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.