Created
July 28, 2021 21:48
-
-
Save s3va/cee2db42b294aeba3dedd64e51e95a9f to your computer and use it in GitHub Desktop.
Content provider basics... fuck up.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.constraintlayout.widget.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=".MainActivity"> | |
<TextView | |
android:id="@+id/textView" | |
android:layout_width="0dp" | |
android:layout_height="0dp" | |
android:text="Hello World!" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintRight_toRightOf="parent" | |
app:layout_constraintTop_toBottomOf="@+id/wordsListView" /> | |
<Button | |
android:id="@+id/button" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:onClick="insertword" | |
android:text="@string/insert" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintTop_toBottomOf="@+id/textInputLayout" /> | |
<com.google.android.material.textfield.TextInputLayout | |
android:id="@+id/textInputLayout" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent"> | |
<com.google.android.material.textfield.TextInputEditText | |
android:id="@+id/editText" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:hint="@string/enter_word_for_insert_in_to_the_user_dictionary" /> | |
</com.google.android.material.textfield.TextInputLayout> | |
<ListView | |
android:id="@+id/wordsListView" | |
android:layout_width="0dp" | |
android:layout_height="0dp" | |
android:layout_marginBottom="2dp" | |
app:layout_constraintBottom_toTopOf="@+id/textView" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toBottomOf="@+id/button" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tk.kvakva.contentproviderfuckup | |
import android.content.ContentValues | |
import android.database.Cursor | |
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
import android.provider.UserDictionary | |
import android.util.Log | |
import android.view.View | |
import android.widget.ListView | |
import android.widget.TextView | |
import androidx.cursoradapter.widget.SimpleCursorAdapter | |
import com.google.android.material.textfield.TextInputEditText | |
private const val TAG = " ** MainActivity ** " | |
class MainActivity : AppCompatActivity() { | |
// Defines a new Uri object that receives the result of the insertion | |
lateinit var _editText: TextInputEditText | |
lateinit var cursorAdapter: SimpleCursorAdapter | |
// Defines a list of View IDs that will receive the Cursor columns for each row | |
val wordListItems = intArrayOf(android.R.id.text1, android.R.id.text2) | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
_editText = findViewById(R.id.editText) | |
listWords() | |
} | |
fun insertword(view: View) { | |
val text: String = _editText.text.toString() | |
// Defines an object to contain the new values to insert | |
val newValues = ContentValues().apply { | |
/* | |
* Sets the values of each column and inserts the word. The arguments to the "put" | |
* method are "column name" and "value" | |
*/ | |
//put(UserDictionary.Words.APP_ID, "example.user") | |
put(UserDictionary.Words.LOCALE, "en_US") | |
put(UserDictionary.Words.WORD, text) | |
//put(UserDictionary.Words.FREQUENCY, "100") | |
} | |
val newUri = contentResolver.insert( | |
UserDictionary.Words.CONTENT_URI, // the user dictionary content URI | |
newValues // the values to insert | |
) | |
findViewById<TextView>(R.id.textView).append("\n${newUri}") | |
cursorAdapter.swapCursor(getCursor()) | |
} | |
fun getCursor(): Cursor? { | |
// Does a query against the table and returns a Cursor object | |
val mCursor = contentResolver.query( | |
UserDictionary.Words.CONTENT_URI, // The content URI of the words table | |
null, // The columns to return for each row | |
null, | |
null,// Either null, or the word the user entered | |
null | |
) | |
when (mCursor?.count) { | |
null -> { | |
Log.d(TAG, "listWords: NULL!!!!!!!!!!!!!!!!!!!") | |
} | |
0 -> { | |
Log.d(TAG, "listWords: No Words!!!!!!!!!!!!!!!!!!!") | |
} | |
else -> { | |
Log.i(TAG, "listWords: cursor,count ============= ${mCursor.count}") | |
} | |
} | |
return mCursor | |
} | |
fun listWords() { | |
// Defines a list of columns to retrieve from the Cursor and load into an output row | |
val wordListColumns: Array<String> = arrayOf( | |
//UserDictionary.Words._ID, // Contract class constant containing the word column name | |
UserDictionary.Words.WORD, | |
UserDictionary.Words.SHORTCUT | |
) | |
// Creates a new SimpleCursorAdapter | |
val cursor=getCursor() | |
cursor?.let { | |
cursorAdapter = SimpleCursorAdapter( | |
this, // The application's Context object | |
android.R.layout.two_line_list_item, // A layout in XML for one row in the ListView | |
it, // The result from the query | |
wordListColumns, // A string array of column names in the cursor | |
wordListItems, // An integer array of view IDs in the row layout | |
0 // Flags (usually none are needed) | |
) | |
findViewById<ListView>(R.id.wordsListView).adapter = cursorAdapter | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment