Reputation: 33
I have implemented a custom adapter in a ListView but when i try to use setOnItemClickListener the function does not work. I tried using a toast to notify me if the click respond but it did not work
Here is my activity and the adapter
class PasswordListActivity : AppCompatActivity() {
private lateinit var dataSource: SavedPasswordDB
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_password_list)
user = intent.getSerializableExtra("loggedUser") as User
loadPasswords()
btAddOpt.setOnClickListener {
// ...
}
}
override fun onPause() {
super.onPause()
this.loadPasswords()
}
override fun onResume() {
super.onResume()
this.loadPasswords()
}
internal class CustomAdapter(context: Context, data: List<SavedPassword>): ArrayAdapter<SavedPassword>(context, R.layout.activity_password_list, data) {
@SuppressLint("ViewHolder", "SetTextI18n")
private lateinit var dataSource: SavedPasswordDB
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val inflater = LayoutInflater.from(context).inflate(R.layout.custom_row, parent, false)
val pwd = getItem(position)
dataSource = SavedPasswordDB(context)
inflater.tvTitle.text = pwd!!.fromTitle
inflater.tvUser.text = "User: " + pwd.pwdUser
inflater.tvCont.text = "Password: " + pwd.pwdContent
inflater.tvOptLink.text = "Site link: " + pwd.optionalLink
inflater.tvCreated.text = "Created At: " + pwd.createdAt
inflater.btDeleteCurrent.setOnClickListener {
// ...
}
return inflater
}
}
private fun loadPasswords () {
dataSource = SavedPasswordDB(this)
var passwords = mutableListOf<SavedPassword>()
val cursor = dataSource.selectPwdByUser(user.id)
while (cursor.moveToNext()) {
val col = SavedPassword(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getInt(6))
passwords.add(col)
}
val adapter = CustomAdapter(this, passwords)
lvPwds.adapter = adapter
lvPwds.setOnItemClickListener { adapterView, view, i, id ->
val intent = Intent(this, PasswordDetailActivity::class.java)
startActivity(intent)
}
}
}
Also, i was thinking that the error could be in the XML files, maybe there is a property that im missging, so here they are:
ListView Activity:
<ListView
android:id="@+id/lvPwds"
android:layout_width="353dp"
android:layout_height="593dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/btAddOpt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.87"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.90999997"
android:src="@drawable/add_icon"/>
Custom Adapter:
<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:padding="15dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="160dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="260dp"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tvTitle"
android:layout_width="35dp"
android:layout_height="wrap_content"
android:text="@string/site"
android:textColor="#000000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.046"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvUser"
android:layout_width="61dp"
android:layout_height="wrap_content"
android:text="@string/userc"
app:layout_constraintStart_toStartOf="@+id/tvTitle"
app:layout_constraintTop_toBottomOf="@+id/tvTitle" />
<TextView
android:id="@+id/tvCont"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/content"
app:layout_constraintStart_toStartOf="@+id/tvTitle"
app:layout_constraintTop_toBottomOf="@+id/tvTitle" />
<TextView
android:id="@+id/tvOptLink"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/optional_link"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@+id/tvTitle"
app:layout_constraintTop_toBottomOf="@+id/tvCont"
app:layout_constraintVertical_bias="0.043" />
<TextView
android:id="@+id/tvCreated"
android:layout_width="wrap_content"
android:layout_height="27dp"
android:text="@string/created_at"
app:layout_constraintStart_toStartOf="@+id/tvUser"
app:layout_constraintTop_toBottomOf="@+id/tvUser" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageButton
android:id="@+id/btDeleteCurrent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.952"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@android:drawable/ic_delete" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Upvotes: 0
Views: 214
Reputation: 1875
Add below line to your ListViewtag in xml -
android:descendantFocusability="blocksDescendants"
It will help you.
Upvotes: 1