-1

So I am making an App for a friend and It's my first time using Android Studio and Kotlin in general so I'm really inexperienced. I have three codes. The main kt being:

import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.erstesachenso.MyAdapter
import com.example.erstesachenso.R


class MainActivity : AppCompatActivity() {

    private lateinit var recyclerView: RecyclerView
    private lateinit var adapter: MyAdapter
    private lateinit var layoutManager: LinearLayoutManager

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Finde die Views aus dem Layout
        val editTextColumnName = findViewById<EditText>(R.id.editTextColumnName)
        val buttonAddColumn = findViewById<Button>(R.id.buttonAddColumn)

        // RecyclerView und Adapter initialisieren
        recyclerView = findViewById(R.id.recyclerView)
        layoutManager = LinearLayoutManager(this)
        recyclerView.layoutManager = layoutManager
        adapter = MyAdapter()
        recyclerView.adapter = adapter

        // Klick-Listener für den Button hinzufügen
        buttonAddColumn.setOnClickListener {
            val columnName = editTextColumnName.text.toString().trim()
            if (columnName.isNotEmpty()) {
                // Spalte hinzufügen
                adapter.addColumn(columnName)
                // Feld leeren
                editTextColumnName.text.clear()
            }
        }
    }
}

Then I have a MyAdapter kt:

package com.example.erstesachenso

import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class MyAdapter : RecyclerView.Adapter<MyAdapter.ViewHolder>() {

    private val columns = mutableListOf<String>()

    override fun onBindViewHolder(holder: ViewHolder, position: Int){
        val columnName = columns[position]
        holder.bind(columnName)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        TODO("Not yet implemented")
    }

    override fun getItemCount(): Int {
        return columns.size
    }

    fun addColumn(columnName: String) {
        TODO("Not yet implemented")
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val textViewColumn: TextView = itemView.findViewById(android.R.id.text1)

        fun bind(columnName: String) {
            textViewColumn.text = columnName
        }
    }
}

and an activity_main xml being:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">


    <EditText
        android:id="@+id/editTextColumnName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Spaltenname eingeben"
        android:imeOptions="actionDone"
        android:inputType="text"
        android:maxLines="1" />

    <Button
        android:id="@+id/buttonAddColumn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Spalte hinzufügen"
        android:layout_gravity="center_horizontal" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="16dp"
        android:orientation="vertical" />

</LinearLayout>

So when I run the app (I tried it on two phones it worked on neither one) It opens up for a split second and immediatly closes again. I have the minimum SDK to Android 7 and I have installed Android 10 to 13.

I asked Chat gpt what to do since I just started today and no one I know is into coding. I also reaserched but the only thing I got is that my code is probably faulty. But I don't know what exactly.

  • 1
    Read [this](https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors#:~:text=In%20simple%20terms%2C%20a%20stack,when%20an%20Exception%20was%20thrown.&text=This%20would%20indicate%20that%20something,null%20in%20the%20above%20code.&text=Again%2C%20with%20this%20exception%20we,at%20line%2022%20of%20Book.) It says it's about Java, but also applies to Kotlin. If your app crashes, you should always include your stack trace in your question if asking about it here. But usually it will be enough for you to solve the problem yourself. – Tenfour04 Jul 22 '23 at 22:26
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Jul 23 '23 at 07:59

1 Answers1

0

When the application reads the line TODO("Not yet implemented") it will produce an error.

If you check your Logcat you might see this error.

Process: com.example.myapplication, PID: 1088
kotlin.NotImplementedError: An operation is not implemented: Not yet implemented

I would suggest you to implement the following methods in MyAdapter.kt.

  1. onCreateViewHolder()

From:

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        TODO("Not yet implemented")
}

To:

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    val viewHolder = ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.your_item_layout, parent, false))
    return viewHolder
}

Note: R.layout.your_item_layout is the layout that recycler view uses. You should provide your item layout id in the inflate() method instead.

  1. addColumn()
fun addColumn(columnName: String) {
    TODO("Not yet implemented")
}
Goutham
  • 156
  • 13