<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ankit Mishra</title>
    <description>The latest articles on DEV Community by Ankit Mishra (@ankit_mishra_82889376b8e9).</description>
    <link>https://dev.to/ankit_mishra_82889376b8e9</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3054790%2F61f4b928-05d1-4129-a7c7-419fdf48e229.png</url>
      <title>DEV Community: Ankit Mishra</title>
      <link>https://dev.to/ankit_mishra_82889376b8e9</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ankit_mishra_82889376b8e9"/>
    <language>en</language>
    <item>
      <title>Guide on Database in Android Studio</title>
      <dc:creator>Ankit Mishra</dc:creator>
      <pubDate>Wed, 16 Apr 2025 08:14:37 +0000</pubDate>
      <link>https://dev.to/ankit_mishra_82889376b8e9/guide-on-database-in-android-studio-43n2</link>
      <guid>https://dev.to/ankit_mishra_82889376b8e9/guide-on-database-in-android-studio-43n2</guid>
      <description>&lt;p&gt;MainActivity.java&lt;br&gt;
package com.example.restaurantapp;&lt;/p&gt;

&lt;p&gt;import android.content.Context;&lt;br&gt;
import android.content.Intent;&lt;br&gt;
import android.database.Cursor;&lt;br&gt;
import android.database.sqlite.SQLiteDatabase;&lt;br&gt;
import android.database.sqlite.SQLiteOpenHelper;&lt;br&gt;
import android.os.Bundle;&lt;br&gt;
import android.view.View;&lt;br&gt;
import android.widget.*;&lt;br&gt;
import androidx.appcompat.app.AppCompatActivity;&lt;/p&gt;

&lt;p&gt;import java.util.ArrayList;&lt;br&gt;
import java.util.HashMap;&lt;/p&gt;

&lt;p&gt;public class MainActivity extends AppCompatActivity {&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Spinner categorySpinner, itemSpinner;
TextView priceTextView;
Button addButton, checkoutButton;
int totalCost = 0;

DatabaseHelper dbHelper;
ArrayList&amp;lt;String&amp;gt; categoryList = new ArrayList&amp;lt;&amp;gt;();
HashMap&amp;lt;String, Integer&amp;gt; categoryMap = new HashMap&amp;lt;&amp;gt;();

ArrayList&amp;lt;String&amp;gt; itemList = new ArrayList&amp;lt;&amp;gt;();
HashMap&amp;lt;String, Integer&amp;gt; itemPriceMap = new HashMap&amp;lt;&amp;gt;();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    dbHelper = new DatabaseHelper(this);

    categorySpinner = findViewById(R.id.categorySpinner);
    itemSpinner = findViewById(R.id.itemSpinner);
    priceTextView = findViewById(R.id.priceTextView);
    addButton = findViewById(R.id.addButton);
    checkoutButton = findViewById(R.id.checkoutButton);

    loadCategories();

    categorySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override public void onItemSelected(AdapterView&amp;lt;?&amp;gt; parent, View view, int position, long id) {
            if (position &amp;gt; 0) {
                String category = categoryList.get(position);
                loadItems(categoryMap.get(category));
            }
        }
        @Override public void onNothingSelected(AdapterView&amp;lt;?&amp;gt; parent) {}
    });

    itemSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override public void onItemSelected(AdapterView&amp;lt;?&amp;gt; parent, View view, int position, long id) {
            if (position &amp;gt;= 0 &amp;amp;&amp;amp; position &amp;lt; itemList.size()) {
                String item = itemList.get(position);
                int price = itemPriceMap.get(item);
                priceTextView.setText("Price: $" + price);
            }
        }
        @Override public void onNothingSelected(AdapterView&amp;lt;?&amp;gt; parent) {}
    });

    addButton.setOnClickListener(v -&amp;gt; {
        String item = (String) itemSpinner.getSelectedItem();
        if (item != null) {
            totalCost += itemPriceMap.get(item);
            Toast.makeText(MainActivity.this, "Item added! Total: $" + totalCost, Toast.LENGTH_SHORT).show();
        }
    });

    checkoutButton.setOnClickListener(v -&amp;gt; {
        Intent intent = new Intent(MainActivity.this, SummaryActivity.class);
        intent.putExtra("total", totalCost);
        startActivity(intent);
    });
}

private void loadCategories() {
    categoryList.clear();
    categoryList.add("Select Category");

    Cursor cursor = dbHelper.getCategories();
    while (cursor.moveToNext()) {
        int id = cursor.getInt(0);
        String name = cursor.getString(1);
        categoryList.add(name);
        categoryMap.put(name, id);
    }
    cursor.close();

    ArrayAdapter&amp;lt;String&amp;gt; adapter = new ArrayAdapter&amp;lt;&amp;gt;(this, android.R.layout.simple_spinner_item, categoryList);
    categorySpinner.setAdapter(adapter);
}

private void loadItems(int categoryId) {
    itemList.clear();
    itemPriceMap.clear();

    Cursor cursor = dbHelper.getItemsByCategoryId(categoryId);
    while (cursor.moveToNext()) {
        String name = cursor.getString(2);
        int price = cursor.getInt(3);
        itemList.add(name);
        itemPriceMap.put(name, price);
    }
    cursor.close();

    ArrayAdapter&amp;lt;String&amp;gt; adapter = new ArrayAdapter&amp;lt;&amp;gt;(this, android.R.layout.simple_spinner_item, itemList);
    itemSpinner.setAdapter(adapter);
}

private static class DatabaseHelper extends SQLiteOpenHelper {

    public static final String DB_NAME = "restaurant.db";
    public static final int DB_VERSION = 1;

    public DatabaseHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE IF NOT EXISTS categories (id INTEGER PRIMARY KEY, name TEXT)");
        db.execSQL("CREATE TABLE IF NOT EXISTS items (id INTEGER PRIMARY KEY, category_id INTEGER, name TEXT, price INTEGER)");

        db.execSQL("INSERT INTO categories (name) VALUES ('Drinks'), ('Main Course'), ('Dessert')");
        db.execSQL("INSERT INTO items (category_id, name, price) VALUES " +
                "(1, 'Coke', 2), (1, 'Water', 1), (1, 'Juice', 3), " +
                "(2, 'Burger', 5), (2, 'Pizza', 8), (2, 'Pasta', 7), " +
                "(3, 'Ice Cream', 4), (3, 'Cake', 5), (3, 'Pudding', 3)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS items");
        db.execSQL("DROP TABLE IF EXISTS categories");
        onCreate(db);
    }

    public Cursor getCategories() {
        return getReadableDatabase().rawQuery("SELECT * FROM categories", null);
    }

    public Cursor getItemsByCategoryId(int categoryId) {
        return getReadableDatabase().rawQuery("SELECT * FROM items WHERE category_id = ?", new String[]{String.valueOf(categoryId)});
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;activity_main.xml&lt;br&gt;
&amp;lt;?xml version="1.0" encoding="utf-8"?&amp;gt;&lt;br&gt;

    android:layout_width="match_parent"&lt;br&gt;
    android:layout_height="match_parent"&amp;gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="24dp"
    android:gravity="center_horizontal"&amp;gt;

    &amp;lt;TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Select Category"
        android:textSize="18sp"
        android:layout_marginBottom="8dp" /&amp;gt;

    &amp;lt;Spinner
        android:id="@+id/categorySpinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="24dp"/&amp;gt;

    &amp;lt;TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Select Item"
        android:textSize="18sp"
        android:layout_marginBottom="8dp" /&amp;gt;

    &amp;lt;Spinner
        android:id="@+id/itemSpinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="24dp"/&amp;gt;

    &amp;lt;TextView
        android:id="@+id/priceTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Price: $0"
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_marginBottom="32dp"/&amp;gt;

    &amp;lt;Button
        android:id="@+id/addButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Add Item"
        android:layout_marginBottom="20dp"
        android:padding="12dp"/&amp;gt;

    &amp;lt;Button
        android:id="@+id/checkoutButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Checkout"
        android:padding="12dp"/&amp;gt;

&amp;lt;/LinearLayout&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;SummaryActivity.java&lt;br&gt;
package com.example.restaurantapp;&lt;/p&gt;

&lt;p&gt;import android.os.Bundle;&lt;br&gt;
import android.widget.TextView;&lt;br&gt;
import androidx.appcompat.app.AppCompatActivity;&lt;/p&gt;

&lt;p&gt;public class SummaryActivity extends AppCompatActivity {&lt;br&gt;
    &lt;a class="mentioned-user" href="https://dev.to/override"&gt;@override&lt;/a&gt;&lt;br&gt;
    protected void onCreate(Bundle savedInstanceState) {&lt;br&gt;
        super.onCreate(savedInstanceState);&lt;br&gt;
        setContentView(R.layout.activity_summary);&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    int total = getIntent().getIntExtra("total", 0);
    TextView summaryText = findViewById(R.id.summaryText);
    summaryText.setText("Total cost: $" + total);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;activity_summary.xml&lt;br&gt;
&amp;lt;?xml version="1.0" encoding="utf-8"?&amp;gt;&lt;br&gt;

    android:layout_width="match_parent"&lt;br&gt;
    android:layout_height="match_parent"&lt;br&gt;
    android:orientation="vertical"&lt;br&gt;
    android:padding="32dp"&lt;br&gt;
    android:gravity="center"&amp;gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;TextView
    android:id="@+id/summaryText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Total cost: $0"
    android:textSize="28sp"
    android:textStyle="bold"
    android:textColor="#000000"
    android:layout_marginTop="20dp"/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;androidmanifest.xml&lt;br&gt;
only if required&lt;br&gt;
    
        android:allowBackup="true"&lt;br&gt;
        android:dataExtractionRules="&lt;a class="mentioned-user" href="https://dev.to/xml"&gt;@xml&lt;/a&gt;/data_extraction_rules"&lt;br&gt;
        android:fullBackupContent="&lt;a class="mentioned-user" href="https://dev.to/xml"&gt;@xml&lt;/a&gt;/backup_rules"&lt;br&gt;
        android:icon="@mipmap/ic_launcher"&lt;br&gt;
        android:label="&lt;a class="mentioned-user" href="https://dev.to/string"&gt;@string&lt;/a&gt;/app_name"&lt;br&gt;
        android:roundIcon="@mipmap/ic_launcher_round"&lt;br&gt;
        android:supportsRtl="true"&lt;br&gt;
        android:theme="@style/Theme.Restaurantapp"&lt;br&gt;
        tools:targetApi="31"&amp;gt;&lt;br&gt;
        
            android:name=".MainActivity"&lt;br&gt;
            android:exported="true"&amp;gt;&lt;br&gt;
            &lt;br&gt;
                &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            &amp;lt;category android:name="android.intent.category.LAUNCHER" /&amp;gt;
        &amp;lt;/intent-filter&amp;gt;
    &amp;lt;/activity&amp;gt;

    &amp;lt;activity android:name=".SummaryActivity"&amp;gt;&amp;lt;/activity&amp;gt;

&amp;lt;/application&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>android</category>
      <category>java</category>
      <category>xml</category>
    </item>
  </channel>
</rss>
