ListView没有显示任何内容

时间:2020-06-12 14:21:13

标签: android android-studio kotlin

所以我正尝试使用YouTube上的watchin教程中的kotlin创建一个记事应用 我不知道问题出在哪里,因为我只是按照教程中的说明进行操作。我们之间唯一的不同代码是

他的代码

try {
    val bundle: Bundle = intent.extras
    id = bundle.getInt("ID", 0)
    if (id != 0) {
          titleEt.setText(bundle.getString("name"))
          descEt.setText(bundle.getString("des"))
    }
} catch (ex: Exception) {}

但是,该代码对我来说是一个问题。编辑说第二行的“ intent.extras”需要“捆绑吗?”不捆绑。 因此,我对此进行了一些更改

我的代码

try {
    val bundle: Bundle? = intent.extras
    id = bundle!!.getInt("ID", 0)
    if (id != 0) {
          titleEt.setText(bundle.getString("name"))
          descEt.setText(bundle.getString("des"))
    }
} catch (ex: Exception) {}

其余都一样

这是其他文件中的代码段 MainActivity.kt

class MainActivity : AppCompatActivity() {

    var listDiarys = ArrayList<Diary>()

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

        LoadQuery("%")
    }

    override fun onResume() {
        super.onResume()
        LoadQuery("%")
    }

    private fun LoadQuery(title: String) {
        var dbManager = DbManager(this)
        val projections = arrayOf("ID", "Title", "Description")
        val selectionArgs = arrayOf(title)
        val cursor = dbManager.query(projections, "Title like ?", selectionArgs, "Title")
        listDiarys.clear()
        if (cursor.moveToFirst()) {
            do {
                val ID = cursor.getInt(cursor.getColumnIndex("ID"))
                val Title = cursor.getString(cursor.getColumnIndex("Title"))
                val Description = cursor.getString(cursor.getColumnIndex("Description"))

                listDiarys.add(Diary(ID, Title, Description))
            } while (cursor.moveToNext())
        }

        var myDiarysAdapter = MyDiarysAdapter(this, listDiarys)
        diarysLv.adapter = myDiarysAdapter

        val total = diarysLv.count
        val mActionBar = supportActionBar
        if (mActionBar !== null) {
            mActionBar.subtitle = "You have $total note(s) in list."
        }
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.main_menu, menu)

        val searchv: SearchView = menu!!.findItem(R.id.app_bar_search).actionView as SearchView
        val searchm = getSystemService(Context.SEARCH_SERVICE) as SearchManager
        searchv.setSearchableInfo(searchm.getSearchableInfo(componentName))
        searchv.setOnQueryTextListener(object: SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                LoadQuery("%$query%")
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                LoadQuery("%$newText%")
                return false
            }
        })

        return super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        if (item != null) {
            when(item.itemId) {
                R.id.addNote -> {
                    startActivity(Intent(this, AddDiaryActivity::class.java))
                }
                R.id.action_settings -> {
                    Toast.makeText(this, "Settings", Toast.LENGTH_SHORT).show()
                }
            }
        }
        return super.onOptionsItemSelected(item)
    }

    inner class MyDiarysAdapter: BaseAdapter  {
        var context: Context? = null
        var listDiarysAdapter = ArrayList<Diary>()

        constructor(context: Context, listDiarysArray: ArrayList<Diary>) : super() {
            this.listDiarysAdapter = listDiarysAdapter
            this.context = context
        }


        override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
            var myView = layoutInflater.inflate(R.layout.row, null)
            val myDiary = listDiarysAdapter[position]
            myView.titleTv.text = myDiary.nodeName
            myView.descTv.text = myDiary.nodeDes

            myView.deleteBtn.setOnClickListener {
                var dbManager = DbManager(this.context!!)
                val selectionArgs = arrayOf(myDiary.nodeID.toString())
                dbManager.delete("ID=?", selectionArgs)
                LoadQuery("%")
            }

            myView.editBtn.setOnClickListener {
                GoToUpdateFun(myDiary)
            }

            return myView
        }

        override fun getItem(position: Int): Any {
            return listDiarysAdapter[position]
        }

        override fun getItemId(position: Int): Long {
            return position.toLong()
        }

        override fun getCount(): Int  {
            return listDiarysAdapter.size
        }

    }

    private fun GoToUpdateFun(myDiary: Diary) {
        var intent = Intent(this, AddDiaryActivity::class.java)
        intent.putExtra("ID", myDiary.nodeID)
        intent.putExtra("name", myDiary.nodeName)
        intent.putExtra("des", myDiary.nodeDes)
        startActivity(intent)
    }
}

AddDiaryActivity.kt

class AddDiaryActivity : AppCompatActivity() {

    val dbTable = "Diarys"
    var id = 0

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

        try {
            val bundle: Bundle? = intent.extras
            id = bundle!!.getInt("ID", 0)
            if (id != 0) {
                titleEt.setText(bundle.getString("name"))
                descEt.setText(bundle.getString("des"))
            }
        } catch (ex: Exception) {}
    }

    fun addFunc(view: View) {
        var dbManager = DbManager(this)
        var values = ContentValues()
        values.put("Title", titleEt.text.toString())
        values.put("Description", descEt.text.toString())

        if (id == 0) {
            val ID = dbManager.insert(values)
            if (ID > 0) {
                Toast.makeText(this, "Diary is added", Toast.LENGTH_SHORT).show()
                finish()
            } else {
                Toast.makeText(this, "Error adding diary", Toast.LENGTH_SHORT).show()
            }
        } else {
            var selectionArgs = arrayOf(id.toString())
            val ID = dbManager.update(values, "ID=?", selectionArgs)
            if (ID > 0) {
                Toast.makeText(this, "Diary is updated", Toast.LENGTH_SHORT).show()
                finish()
            } else {
                Toast.makeText(this, "Error updating diary", Toast.LENGTH_SHORT).show()
            }
        }
    }
}

activity_main.xml

<LinearLayout 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"
    android:orientation="vertical"
    android:background="@color/gray"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/diarysLv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:divider="@null"
        android:dividerHeight="1dp"
        />

</LinearLayout>

row.xml

<androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:cardBackgroundColor="@color/white"
    app:cardElevation="3dp"
    app:cardCornerRadius="5dp"
    app:cardUseCompatPadding="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:gravity="end|bottom"
        android:orientation="vertical">

        <TextView
            android:id="@+id/titleTv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/title_tv"
            android:textColor="@color/colorPrimary"
            android:textSize="18sp"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/descTv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/desc_tv"/>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/colorPrimaryDark"
            android:layout_marginTop="2dp"
            android:layout_marginBottom="3dp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="end">

            <ImageButton
                android:id="@+id/editBtn"
                android:background="@null"
                android:src="@drawable/ic_action_edit"
                android:layout_marginEnd="5dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

            <ImageButton
                android:id="@+id/deleteBtn"
                android:background="@null"
                android:src="@drawable/ic_action_delete"
                android:layout_marginEnd="5dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

        </LinearLayout>

    </LinearLayout>

</androidx.cardview.widget.CardView>

AndroidManifest.xml

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity"
            android:label="Diary's List">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".AddDiaryActivity"
            android:label="Add Diary"
            android:parentActivityName=".MainActivity">
        </activity>
    </application>

还有另一个文件,例如DbManager.kt,Diary.kt和activity_add_diary.xml。我认为该文件与本案无关。感谢您的时间和帮助

0 个答案:

没有答案