import
android.content.SharedPreferences;
import
android.database.Cursor;
import
android.database.sqlite.SQLiteDatabase;
import
android.os.Bundle;
import
android.widget.TextView;
import
androidx.appcompat.app.AppCompatActivity;
import
java.util.ArrayList;
import
java.util.HashMap;
import
java.util.LinkedList;
public
class
MainActivity
extends
AppCompatActivity {
private
final
int
[] myArray = {
1
,
2
,
3
};
private
final
ArrayList<String> myArrayList =
new
ArrayList<>();
private
final
LinkedList<Object> myLinkedList =
new
LinkedList<>();
private
final
HashMap<String, String> myHashMap =
new
HashMap<>();
private
final
android.util.SparseArray<Object> parsingArray =
new
android.util.SparseArray<>();
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView arrayText = findViewById(R.id.array_text);
TextView arrayListText = findViewById(R.id.arraylist_text);
TextView linkedListText = findViewById(R.id.linkedlist_text);
TextView hashMapText = findViewById(R.id.hashmap_text);
TextView sparseArrayText = findViewById(R.id.sparsearray_text);
TextView sharedPreferencesText = findViewById(R.id.sharedpreferences_text);
TextView sqliteText = findViewById(R.id.sqlite_text);
myArrayList.add(
"Hello"
);
myArrayList.add(
"World"
);
myLinkedList.add(
"Hello"
);
myLinkedList.add(
"World"
);
myHashMap.put(
"key1"
,
"value1"
);
myHashMap.put(
"key2"
,
"value2"
);
parsingArray.put(
1
,
"Hello"
);
parsingArray.put(
2
,
"World"
);
SharedPreferences sharedPreferences = getSharedPreferences(
"prefs"
, MODE_PRIVATE);
sharedPreferences.edit().putString(
"key"
,
"value"
).apply();
SQLiteDatabase sqLiteDatabase = openOrCreateDatabase(
"mydb"
, MODE_PRIVATE,
null
);
sqLiteDatabase.execSQL(
"CREATE TABLE IF NOT EXISTS mytable (id INTEGER PRIMARY KEY, data TEXT)"
);
sqLiteDatabase.execSQL(
"INSERT INTO mytable (data) VALUES ('Hello World')"
);
arrayText.setText(
"Array Element: "
+ myArray[
0
]);
arrayListText.setText(
"ArrayList Element: "
+ myArrayList.get(
0
));
linkedListText.setText(
"LinkedList Element: "
+ myLinkedList.get(
0
));
hashMapText.setText(
"HashMap Element: "
+ myHashMap.get(
"key1"
));
sparseArrayText.setText(
"SparseArray Element: "
+ parsingArray.get(
1
));
sharedPreferencesText.setText(
"SharedPreferences Element: "
+ sharedPreferences.getString(
"key"
,
""
));
Cursor cursor = sqLiteDatabase.rawQuery(
"SELECT * FROM mytable"
,
null
);
if
(cursor.moveToFirst()) {
sqliteText.setText(
"SQLite Element: "
+ cursor.getString(
1
));
}
cursor.close();
}
}