Не срабатывает setOnItemClickListener для нажатия кнопки редактирования записи
Задача: по нажатию кнопки в элементе ListView должна открываться Activity где в дальнейшем будет редактироваться запись. Проблема в том, что все перепробованные мной методы на похожие проблемы не работают, сейчас остановился на setOnItemClickListener, но суть в том, что он не срабатывает ни каким образом. Т.е даже лог не выводит. Что делать не знаю, битый час сижу
class MainActivity : ComponentActivity() {
var myListView: ListView? = null
var db: MainDB? = null
var dao: Dao? = null
var csr: Cursor? = null
var sca: SimpleCursorAdapter? = null
var btn_edit1: ImageButton?= null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//binding = MainActivityBinding.inflate(layoutInflater)
setContentView(R.layout.main_activity)
myListView = findViewById(R.id.lvMain);
db = MainDB.getDB(this)
dao = db!!.getDao()
/*
val p1 = Item(null,null,null,null,null)
p1.plant_description = "Ромашка"
p1.plant_room = "комната"
p1.plant_send_push = true
p1.plant_time_to_watering = 24
dao!!.insertItem(p1)
*/
Log.e("LOG","KJFKJFJFKJFKFKKF")
btn_edit1 = this.findViewById<ImageButton>(R.id.btn_edit)
myListView!!.setOnItemClickListener { parent, view, position, id ->
val buttonEdit = view.findViewById<ImageButton>(R.id.btn_edit)
Log.e("LOG","НАЖАТИЕ ЕСТЬ")
buttonEdit.setOnClickListener {
val intent = Intent(this@MainActivity, ItemActivity::class.java)
startActivity(intent)
}
}
}
@SuppressLint("Range")
private fun setUpOrRefreshListView() {
csr = getCursor()
if (sca == null) {
sca = SimpleCursorAdapter(
this,
R.layout.row_item,
csr,
arrayOf<String>("plant_description","plant_time_to_watering","plant_room","plant_send_push"),
intArrayOf(R.id.plant_description,R.id.plant_time_to_watering,R.id.plant_room,R.id.plant_send_push),
0
)
myListView!!.adapter = sca
} else {
sca!!.swapCursor(csr)
}
}
private fun getCursor(): Cursor {
val plants: List<Item> = dao!!.getAllItem()
val mxcsr = MatrixCursor(
arrayOf(
BaseColumns._ID,
"plant_description",
"plant_time_to_watering",
"plant_room",
"plant_send_push"
),
0
)
//dao!!.getAllItem() // plants
for (p in plants) {
mxcsr.addRow(arrayOf<Any>(p.id.toString(),
p.plant_description.toString(),
p.plant_time_to_watering.toString(),
p.plant_room.toString(),
p.plant_send_push.toString()))
}
return mxcsr
}
/* If resuming this activity then refresh the ListView */
override fun onResume() {
super.onResume()
setUpOrRefreshListView()
}
/* if the activity is detsroyed then close the Cursor */
override fun onDestroy() {
super.onDestroy()
csr!!.close()
}
}
Элемент списка
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@drawable/shape"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:elevation="2dp">
<ImageView
android:id="@+id/plant_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/imagepng"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="top">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/linearLayout2"
android:layout_alignParentTop="true"
android:layout_marginTop="3sp"
android:layout_marginBottom="3sp">
<TextView
android:id="@+id/plant_description"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="6"
android:fontFamily="@font/montserrat_medium"
android:text="kfkpomremkremmrpeomvpremvopporvmpormolmlmlllmlmlmlmllmmlmlllmmlmlmlmpvreoprevmporeovmeopvmepovmrepovmevpoem"
android:layout_marginEnd="5sp"
android:maxLength="100">
</TextView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageButton
android:id="@+id/btn_edit"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
android:foreground="@drawable/edit_button"
android:drawableTint="@color/white"
android:drawableTintMode="multiply"
android:background="@drawable/edit_ripple_button"
android:padding="5dp"/>
<CheckBox
android:id="@+id/plant_send_push"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text=""
android:focusableInTouchMode="false"
android:focusable="false"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:layout_marginBottom="1sp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:fontFamily="@font/montserrat_medium"
android:text="Поливать каждые:">
</TextView>
<TextView
android:id="@+id/plant_time_to_watering"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="6dp"
android:layout_weight="3"
android:fontFamily="@font/montserrat_medium"
android:text="24ч"
android:textAlignment="viewStart">
</TextView>
<TextView
android:id="@+id/plant_room"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:fontFamily="@font/montserrat_medium"
android:text="Кухня"
android:maxLength="18"
android:textAlignment="center">
</TextView>
</LinearLayout>
</RelativeLayout>
</LinearLayout>