I believe that your issue is that the
ImageButtons are trapping the
onClickEvents, try changing the
ImageButtons to
ImageViews e.g.
<ImageView
android:id="@+id/ibDeleteCustomer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:src="@drawable/ic_launcher_foreground"
/>
Along with :-
ImageView ibUpdateCustomer = (ImageView) view.findViewById(R.id.ibUpdateCustomer);
ImageView ibDeleteCustomer = (ImageView) view.findViewById(R.id.ibDeleteCustomer);
ImageView ibCallCustomer = (ImageView) view.findViewById(R.id.ibCallCustomer);
ImageView ibViewCustomerDetails = (ImageView) view.findViewById(R.id.ibViewCustomerDetails);
However
I suspect that what you want is to handle the buttons/views individually. That is setting the ListView's
onItemClick listener, as per your current code, handles the **Item** rather than a specific Button/View within the item. As such the above fix will invoke the Toast but the View returned from the Listener will be the RelativeLayout. You would not be able to know which Button has been clicked and thus what action (Update/Delete/Call or View) to take.
What you need to do, if you want to handle the buttons/views themselves is set
onCLickListeners for the buttons/views to handle them being clicked.
A quick, but discouraged way, is to use the
onClick attribute within the xml.
For Example :-
Using :-
<ImageView
android:id="@+id/ibDeleteCustomer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:src="@drawable/ic_launcher_foreground"
android:onClick="doDeleteOnClick"
/>
see the last line
And then adding the following to the Activity's code :-
public void doDeleteOnClick(View v) {
Toast.makeText(v.getContext(),"You clicked the delete button for id " + ((String) v.getTag()), Toast.LENGTH_SHORT).show();
}
Would issue the Toast for the respective row, displaying the id retrieved from the Tag.
Working Example
The following code is a working example that handles ListView Item clicks (by adding a row to the database and thus the refreshed ListView) and also ImageView clicks (by Toasting a message specific to the button).
The code is based upon your code, but some changes have been incorporated for convenience (e.g. the underlying database used for the source of the ListView is a single table, also the data is retrieved from the main thread.)
customer_list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layoutDirection="rtl">
<TextView
android:id="@+id/txtLICustomerName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/ibDeleteCustomer"
android:layout_alignBottom="@id/ibDeleteCustomer"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_toStartOf="@id/ibUpdateCustomer"
android:layout_toLeftOf="@+id/ibUpdateCustomer"
android:text=""
android:textSize="24dp" />
<TextView
android:id="@+id/txtLICustomerBalance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/txtLICustomerName"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:text=""
android:textSize="24dp" />
<ImageView
android:id="@+id/ibDeleteCustomer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:src="@drawable/ic_launcher_foreground"
android:onClick="doDeleteOnClick"
/>
<ImageView
android:id="@+id/ibUpdateCustomer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toStartOf="@+id/ibDeleteCustomer"
android:layout_toLeftOf="@+id/ibDeleteCustomer"
android:src="@drawable/ic_launcher_foreground"
android:onClick="doUpdateOnClick"
/>
<ImageView
android:id="@+id/ibCallCustomer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toStartOf="@+id/ibUpdateCustomer"
android:layout_toLeftOf="@+id/ibUpdateCustomer"
android:src="@drawable/ic_launcher_foreground"
android:onClick="doCallOnCLick"
/>
<ImageView
android:id="@+id/ibViewCustomerDetails"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@id/ibDeleteCustomer"
android:layout_alignLeft="@+id/ibDeleteCustomer"
android:layout_alignTop="@id/txtLICustomerBalance"
android:layout_alignEnd="@id/ibDeleteCustomer"
android:layout_alignRight="@+id/ibDeleteCustomer"
android:src="@drawable/ic_launcher_foreground"
android:onClick="doViewOnClick"
/>
</RelativeLayout>
CustomerCursorAdapter.java
public class CustomerCursorAdapter extends CursorAdapter {
public CustomerCursorAdapter(Context context, Cursor c, boolean autoRequery) {
super(context, c, autoRequery);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.customer_list_row, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView txtLICustomerName = (TextView)view.findViewById(R.id.txtLICustomerName);
TextView txtLICustomerBalance = (TextView)view.findViewById(R.id.txtLICustomerBalance);
ImageView ibUpdateCustomer = (ImageView) view.findViewById(R.id.ibUpdateCustomer);
ImageView ibDeleteCustomer = (ImageView) view.findViewById(R.id.ibDeleteCustomer);
ImageView ibCallCustomer = (ImageView) view.findViewById(R.id.ibCallCustomer);
ImageView ibViewCustomerDetails = (ImageView)view.findViewById(R.id.ibViewCustomerDetails);
String customerName="";
String customerBalance="";
String customerId="";
String customerPhone="01287622467";
customerId = Long.toString(cursor.getLong(0));
customerName = cursor.getString(1);
customerBalance = cursor.getString(2);
txtLICustomerName.setText(customerName);
txtLICustomerBalance.setText(customerBalance);
ibCallCustomer.setTag(customerPhone);
ibDeleteCustomer.setTag(customerId);
ibUpdateCustomer.setTag(customerId);
ibViewCustomerDetails.setTag(customerId);
}
}
MainActivity.java (the equivalent of ViewCustomersActivity)
public class MainActivity extends AppCompatActivity {
long townId;
KestSQLiteHelper kestDB;
Cursor cursor;
CustomerCursorAdapter adapterLstTowns;
ListView lstOfCustomers;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lstOfCustomers = this.findViewById(R.id.lstCustomers);
kestDB = new KestSQLiteHelper(this);
addSomeData();
townId = 1;
}
@Override
protected void onResume() {
super.onResume();
manageListView();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (!cursor.isClosed()) {
cursor.close();
}
}
public void manageListView() {
cursor = getDataForListView();
if (adapterLstTowns == null) {
adapterLstTowns = new CustomerCursorAdapter(this,cursor,true);
lstOfCustomers.setAdapter(adapterLstTowns);
lstOfCustomers.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(view.getContext(),"Hiiiiiiiiiiiiiiiiiiii2",Toast.LENGTH_SHORT).show();
kestDB.addCustomer("ANOTHER" + String.valueOf(System.currentTimeMillis()),300);
manageListView();
}
});
} else {
adapterLstTowns.swapCursor(cursor);
}
}
public void doDeleteOnClick(View v) {
Toast.makeText(v.getContext(),"You clicked the DELETE button for id " + ((String) v.getTag()), Toast.LENGTH_SHORT).show();
}
public void doUpdateOnClick(View v) {
Toast.makeText(v.getContext(),"You clicked the UPDATE button for id " + ((String) v.getTag()),Toast.LENGTH_SHORT).show();
}
public void doCallOnCLick(View v) {
Toast.makeText(v.getContext(),"You clicked the CALL button for # " + ((String) v.getTag()),Toast.LENGTH_SHORT).show();
}
public void doViewOnClick(View v) {
Toast.makeText(v.getContext(),"You clicked the VIEW button for id " + ((String) v.getTag()),Toast.LENGTH_SHORT).show();
}
public Cursor getDataForListView() {
SQLiteDatabase db = kestDB.getWritableDatabase();
return db.query(
KestSQLiteHelper.TBL_CUSTOMER,
new String[]{KestSQLiteHelper.COL_CUSTOMER_ID,
KestSQLiteHelper.COL_CUSTOMERNAME,"1000"},
null,null,null,null,null
);
}
private void addSomeData() {
if(DatabaseUtils.queryNumEntries(kestDB.getWritableDatabase(),KestSQLiteHelper.TBL_CUSTOMER) < 1) {
kestDB.addCustomer("Fred",100);
kestDB.addCustomer("Mary",200);
}
}
}
KestSQLiteHelper.java (simplified DB for demo)
public class KestSQLiteHelper extends SQLiteOpenHelper {
public static final String DBNAME = "mydb";
public static final int DBVERSION = 1;
public static final String TBL_CUSTOMER = "Customer";
public static final String COL_CUSTOMER_ID = BaseColumns._ID;
public static final String COL_CUSTOMERNAME = "Customer_Name";
public static final String COL_CUSTOMER_TOWNID = "Town_id";
public KestSQLiteHelper(@Nullable Context context) {
super(context, DBNAME, null, DBVERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS " + TBL_CUSTOMER + "(" +
COL_CUSTOMER_ID + " INTEGER PRIMARY KEY, " +
COL_CUSTOMERNAME + " TEXT," +
COL_CUSTOMER_TOWNID + " INTEGER" +
")");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public long addCustomer(String customerName, long townId) {
ContentValues cv = new ContentValues();
cv.put(COL_CUSTOMERNAME,customerName);
cv.put(COL_CUSTOMER_TOWNID,townId);
SQLiteDatabase db = this.getWritableDatabase();
return db.insert(TBL_CUSTOMER,null,cv);
}
}
activity_main.xml (the equivalent of activity_view_customers.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutDirection="rtl"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_gravity="start"
android:background="@android:color/holo_blue_bright"
android:textColor="#FF0000FF"
android:text="Customer"
android:textSize="24sp" />
<ListView
android:id="@+id/lstCustomers"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="5dp"
android:layout_gravity="start"
android:textDirection="rtl" />
</LinearLayout>
The above handles button Clicks and also Item clicks. However, changing to use ImageButtons instead of ImageViews disables the Item click handling.