How to use SQLite Database to display as a list view in Android Studio

How to use SQLite Database to display as a list view in Android Studio

Use room, it will be easy to use local SQLite db using room, all you need to define entity, dao and database using Room annotations.

For displaying list of records in listview, just create list view adapter and bind record data to views in item layout. I suggest you use RecyclerView instead of listview to get performance benefits.

You can find room and recycler view examples here http://www.zoftino.com/android

Create a ListView somewhere (I created a new layout):

<?xml version=1.0 encoding=utf-8?>
<android.support.constraint.ConstraintLayout xmlns_android=http://schemas.android.com/apk/res/android
    xmlns_app=http://schemas.android.com/apk/res-auto
    xmlns_tools=http://schemas.android.com/tools
    android_layout_width=match_parent
    android_layout_height=match_parent
    tools_context=net.pelozo.testjava.MainActivity>


    <ListView
        android_id=@+id/listview_products
        android_layout_width=0dp
        android_layout_height=0dp
        android_layout_margin=8dp
        app_layout_constraintBottom_toBottomOf=parent
        app_layout_constraintEnd_toEndOf=parent
        app_layout_constraintStart_toStartOf=parent
        app_layout_constraintTop_toTopOf=parent />
</android.support.constraint.ConstraintLayout>

Create row_product.xml:

<?xml version=1.0 encoding=utf-8?>
<LinearLayout xmlns_android=http://schemas.android.com/apk/res/android
    android_layout_width=match_parent
    android_layout_height=match_parent >
    <TextView
        android_id=@+id/textview_id
        android_layout_width=wrap_content
        android_layout_height=wrap_content/>
    <TextView
        android_id=@+id/textview_name
        android_layout_width=wrap_content
        android_layout_height=wrap_content/>
</LinearLayout>

Add this method to your database clase:

public ArrayList<Products> getProducts(){

    SQLiteDatabase db = getWritableDatabase();
    String query = SELECT * FROM  + TABLE_PRODUCTS;
    Cursor cursor = db.rawQuery(query, null);
    ArrayList<Products> products = new ArrayList<Products>();
    while(cursor.moveToNext()){
        Products product = new Products();
        product.set_id(cursor.getInt(0));
        product.set_productname(cursor.getString(1));
        products.add(product);
    }
    cursor.close();
    db.close();
    return products;
}

create a ProductsAdapter:

public class ProductsAdapter extends ArrayAdapter<Products> {
// View lookup cache
private static class ViewHolder {
    TextView id;
    TextView name;
}

public ProductsAdapter(Context context, ArrayList<Products> products) {
    super(context, R.layout.row_product, products);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Get the data item for this position
    Products product = getItem(position);
    // Check if an existing view is being reused, otherwise inflate the view
    ViewHolder viewHolder; // view lookup cache stored in tag
    if (convertView == null) {
        // If theres no view to re-use, inflate a brand new view for row
        viewHolder = new ViewHolder();
        LayoutInflater inflater = LayoutInflater.from(getContext());
        convertView = inflater.inflate(R.layout.row_product, parent, false);
        viewHolder.id = (TextView) convertView.findViewById(R.id.textview_id);
        viewHolder.name = (TextView) convertView.findViewById(R.id.textview_name);
        // Cache the viewHolder object inside the fresh view
        convertView.setTag(viewHolder);
    } else {
        // View is being recycled, retrieve the viewHolder object from tag
        viewHolder = (ViewHolder) convertView.getTag();
    }
    // Populate the data from the data object via the viewHolder object
    // into the template view.
    viewHolder.id.setText(id: + product.get_id());
    viewHolder.name.setText(name:  + product.get_productname());
    // Return the completed view to render on screen
    return convertView;
}
}

Now just put it all together:

    //get db
    MyDBHandler dbHandler = new MyDBHandler(this, null, null, 1);
    //add a product
    dbHandler.addProduct(new Products(Product Name));
    //retrieve products from db
    ArrayList<Products> productsList = dbHandler.getProducts();
    // Create the adapter
    ProductsAdapter adapter = new ProductsAdapter(this, productsList);
    // Attach the adapter to a ListView
    ListView listView = (ListView) findViewById(R.id.listview_products);
    //set adapter
    listView.setAdapter(adapter);

How to use SQLite Database to display as a list view in Android Studio

Leave a Reply

Your email address will not be published. Required fields are marked *