I'm trying to create an application that shows an EditText matrix and that allows users to enter text. This I have achieved using a GridView where the items are EditText.
I have the problem when I want to read or update the EditText data.
Here I show something of the code that I have.
Class MainActivity
class MainActivity : AppCompatActivity() {
var matrix1: Array<Array<Int>> = Array(3, {Array(3, {0})})
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var adapter = GridViewAdapter(this, matrix1)
gvMatrix1.numColumns = 3
gvMatrix1.adapter = adapter
}
}
Class Adapter
class GridViewAdapter(val context: Context, var matrix: Array<Array<Int>>) : BaseAdapter() {
override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {
var inflator = context!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
var view = inflator.inflate(R.layout.grid_view_item, null)
return view
}
override fun getItem(p0: Int): Any = matrix[p0%matrix.size][p0-(p0-p0%matrix.size)]
override fun getItemId(p0: Int): Long = p0.toLong()
override fun getCount(): Int = matrix.size * matrix[0].size
}
GridView XML within activity_main.xml
<GridView
android:id="@+id/gvMatrix1"
android:layout_width="100dip"
android:layout_height="200dip"
android:columnWidth="2dp"
android:numColumns="1"/>
GridView Item XML
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/etItem"
android:layout_width="match_parent"
android:layout_height="match_parent">
My intention is to read the contents of all the editText to save them in matrix1, modify matrix1 and show their new content.
Is GridView a good choice for this use or there are some layout or view better?
Thanks in advance.