我有一个customAdapter类和一个listView。我使用3个textviews和一个imageButton制作了single_row布局。 现在我的问题是我如何获得数据库ID字段,只是点击图像按钮并忽略整行的点击? 我知道我可以获得身份证明:
mListView.onItemClickListener = AdapterView.OnItemClickListener { adapterView, view, position, id ->
view.delete.setOnClickListener(){
Toast.makeText(context,""+id,Toast.LENGTH_SHORT).show()
}
但这只有在我点击整行时才有效。
我在xml的根目录中尝试了android:descendantFocusability="blocksDescendants"
,在imageButton节点中尝试了android:focusable="false"
,并且在识别整行时无法识别imageButton。只有我使用button.onClickListener()...
点击被识别。但那样我不知道如何获得身份证。
我的适配器类代码:
class MyAdapter(activity: Activity, context: Context, cursor: Cursor) : CursorAdapter(context, cursor, true) {
var inflater: LayoutInflater
lateinit var mListView: ListView
init {
this.inflater = LayoutInflater.from(context)
mListView = activity.listView
}
override fun newView(context: Context?, cursor: Cursor?, parent: ViewGroup?): View {
val view = LayoutInflater.from(context).inflate(R.layout.single_row, parent, false)
return view;
}
override fun bindView(view: View, context: Context, cursor: Cursor) {
val latitude = view.findViewById(R.id.latitude) as TextView
val longtitude = view.findViewById(R.id.longtitude) as TextView
val date = view.findViewById(R.id.date) as TextView
val asu = view.findViewById(R.id.asu) as TextView
latitude.setText(cursor.getString(cursor.getColumnIndex(Constants.Signal.LATITUDE)))
longtitude.setText(cursor.getString(cursor.getColumnIndex(Constants.Signal.LONGTITUDE)))
date.setText(cursor.getString(cursor.getColumnIndex(Constants.Signal.DATE)))
asu.setText(cursor.getString(cursor.getColumnIndex(Constants.Signal.ASU)))
}
}
我的single_row布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:id="@+id/list_item"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants">
<TextView
android:id="@+id/latitude"
android:layout_width="23dp"
android:layout_height="53dp"
android:layout_weight="0.20"
android:text="latitude"
android:gravity="center" />
<TextView
android:id="@+id/longtitude"
android:layout_width="23dp"
android:layout_height="53dp"
android:layout_weight="0.20"
android:text="longtitude"
android:gravity="center" />
<TextView
android:id="@+id/date"
android:layout_width="23dp"
android:layout_height="53dp"
android:layout_weight="0.20"
android:gravity="center"
android:text="date" />
<TextView
android:id="@+id/asu"
android:layout_width="23dp"
android:layout_height="53dp"
android:layout_weight="0.20"
android:gravity="center"
android:focusable="false"
android:text="asu"/>
<ImageButton
android:id="@+id/delete"
android:layout_width="0.23dp"
android:layout_height="53dp"
android:layout_weight="0.20"
android:focusable="false"
android:src="@android:drawable/ic_menu_delete" />
</LinearLayout>
答案 0 :(得分:0)
您正在使用游标适配器,您应该使用ArrayAdapter。 Here is why custom-cursoradapter-and-why-not-use 和example of ArrayAdapter