Unverified

Name

dropDownDatabaseColorSurvey

About

Black White Gray Brown Pink Cyan

Language

Javascript

Rating

Voted: 0 by 0 user(s)

Codevault

Atmit

Scroll down to see more snippets from this codevault.

Wordpress Compatability

The author has indicated that this snippet is compatable up to wordpress version: 6.4

Code Snippet Plugin Sync

Free & Pro

Download this snippet by clicking the download button, then head over to the Code Snippet Plugin settings in your wordpress admin dashboard, select the import menu then upload this file to import into your wordpress site.

Pro Only (Coming Soon)

You will be able to click a button and sync this snippet to your wordpress site automatically and from your dashboard manage all code snippets across all your wordpress sites that have the Code Snippets Pro plugin installed.

Website/ Profile URL:

www.codemitatlab.com

History

Last modified:

17/04/2024

Important Note

This snippet has the following status:

Unverified

This snippet has not been verified, use with caution and at your own risk. See details provided by author in sidebar and click below to find out more.

dropDownDatabaseColorSurvey

 
                    
1package com.example.l9q1;
2 
3import android.content.ContentValues;
4import android.content.DialogInterface;
5import android.database.Cursor;
6import android.database.sqlite.SQLiteDatabase;
7import android.os.Bundle;
8import android.view.View;
9import android.widget.ArrayAdapter;
10import android.widget.Button;
11import android.widget.Spinner;
12import android.widget.Toast;
13 
14import androidx.appcompat.app.AlertDialog;
15import androidx.appcompat.app.AppCompatActivity;
16 
17import java.util.HashMap;
18import java.util.Map;
19 
20public class MainActivity extends AppCompatActivity {
21 
22 private DatabaseHelper databaseHelper;
23 
24 @Override
25 protected void onCreate(Bundle savedInstanceState) {
26 super.onCreate(savedInstanceState);
27 setContentView(R.layout.activity_main);
28 
29 databaseHelper = new DatabaseHelper(this);
30 
31 Spinner colorSpinner = findViewById(R.id.colorSpinner);
32 ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
33 R.array.color_array, android.R.layout.simple_spinner_item);
34 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
35 colorSpinner.setAdapter(adapter);
36 
37 Button submitButton = findViewById(R.id.submitButton);
38 submitButton.setOnClickListener(new View.OnClickListener() {
39 @Override
40 public void onClick(View v) {
41 submitSurvey();
42 }
43 });
44 }
45 
46 private void submitSurvey() {
47 String selectedColor = ((Spinner) findViewById(R.id.colorSpinner)).getSelectedItem().toString();
48 
49 SQLiteDatabase db = databaseHelper.getWritableDatabase();
50 ContentValues values = new ContentValues();
51 values.put(DatabaseHelper.COLUMN_NAME, "Anonymous");
52 values.put(DatabaseHelper.COLUMN_COLOR, selectedColor);
53 db.insert(DatabaseHelper.TABLE_SURVEY, null, values);
54 db.close();
55 
56 Toast.makeText(this, "Survey submitted. Thank you!", Toast.LENGTH_SHORT).show();
57 showSurveyResults();
58 }
59 
60 private void showSurveyResults() {
61 SQLiteDatabase db = databaseHelper.getReadableDatabase();
62 Cursor cursor = db.rawQuery("SELECT " + DatabaseHelper.COLUMN_COLOR + ", COUNT(*) FROM " +
63 DatabaseHelper.TABLE_SURVEY + " GROUP BY " + DatabaseHelper.COLUMN_COLOR, null);
64 
65 Map<String, Integer> colorCounts = new HashMap<>();
66 int totalResponses = 0;
67 
68 // Calculate counts for each color and total responses
69 while (cursor.moveToNext()) {
70 String color = cursor.getString(0);
71 int count = cursor.getInt(1);
72 colorCounts.put(color, count);
73 totalResponses += count;
74 }
75 cursor.close();
76 db.close();
77 
78 // Calculate percentages
79 final Map<String, Float> colorPercentages = new HashMap<>();
80 for (Map.Entry<String, Integer> entry : colorCounts.entrySet()) {
81 String color = entry.getKey();
82 int count = entry.getValue();
83 float percentage = (count / (float) totalResponses) * 100;
84 colorPercentages.put(color, percentage);
85 }
86 
87 // Build and show AlertDialog with survey results
88 AlertDialog.Builder builder = new AlertDialog.Builder(this);
89 builder.setTitle("Survey Results");
90 StringBuilder message = new StringBuilder();
91 for (Map.Entry<String, Float> entry : colorPercentages.entrySet()) {
92 message.append(entry.getKey()).append(": ").append(String.format("%.2f", entry.getValue())).append("%\n");
93 }
94 builder.setMessage(message.toString());
95 builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
96 @Override
97 public void onClick(DialogInterface dialog, int which) {
98 dialog.dismiss();
99 }
100 });
101 AlertDialog dialog = builder.create();
102 dialog.show();
103 }
104}
105 
106 
107// DatabaseHelper.java
108 
109package com.example.l9q1;
110 
111 
112import android.content.Context;
113import android.database.sqlite.SQLiteDatabase;
114import android.database.sqlite.SQLiteOpenHelper;
115 
116public class DatabaseHelper extends SQLiteOpenHelper {
117 private static final String DATABASE_NAME = "survey.db";
118 private static final int DATABASE_VERSION = 1;
119 
120 public static final String TABLE_SURVEY = "survey";
121 public static final String COLUMN_ID = "_id";
122 public static final String COLUMN_NAME = "name";
123 public static final String COLUMN_COLOR = "color";
124 
125 private static final String CREATE_TABLE_SURVEY = "CREATE TABLE " + TABLE_SURVEY +
126 "(" +
127 COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
128 COLUMN_NAME + " TEXT," +
129 COLUMN_COLOR + " TEXT" +
130 ")";
131 
132 public DatabaseHelper(Context context) {
133 super(context, DATABASE_NAME, null, DATABASE_VERSION);
134 }
135 
136 @Override
137 public void onCreate(SQLiteDatabase db) {
138 db.execSQL(CREATE_TABLE_SURVEY);
139 }
140 
141 @Override
142 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
143 db.execSQL("DROP TABLE IF EXISTS " + TABLE_SURVEY);
144 onCreate(db);
145 }
146}

0

Related Snippets

Please see some snippets below related to this snippet..

General

Unverified

0

Dropdown Button Links

Added: 3 weeks ago

Last Updated: 3 weeks ago

General

AI Verified

0

Disable Emojis

Added: 8 months ago

Last Updated: 8 months ago

General

AI Verified

1

Enable Fluent Forms login form capability

Added: 1 year ago

Last Updated: 1 year ago

Fluent forms does not allow the creation of a login form natively at the time of writing. However, this snippet enables that function.

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

General

Unverified

0

mediaPlayer

Added: 2 months ago

Last Updated: 2 months ago

Chippi Chippi SDP Interlude Softcore

General

Unverified

0

DatabaseHelper

Added: 2 months ago

Last Updated: 2 months ago

General

Unverified

0

databasemainActivity

Added: 2 months ago

Last Updated: 2 months ago