0

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.

Alberto
  • 11
  • 1
  • 4

2 Answers2

0

I have used EditText inside listview. But got problems with EditText behavior. Then I switched to adding-views method by using a loop.

val results = List<String>
    for(i in results.size downTo 0 ){
        val inflatedView = LayoutInflater.from(this@MainActivity).inflate(R.layout.myView, card_container, false)
         .......
         .......
        card_container.addView(inflatedView)
    }

Here card_container is a LinearLayout. I suppose girdview also have the problem as listview. So you may try this solution to see if it works.

0

Here I Have found how to get the view of a specific position.

Next I put an example of code to obtain the text of an item and how to send text to an item

var itemView = gvMatrix1.getChildAt(4)  //obtain view of the position 4
var editText = itemView.findViewById(R.id.etItem) as EditText //cast view as EditText
var text = editText.text //getText from editText
editText.setText("8") //setText in editText
Alberto
  • 11
  • 1
  • 4