Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3450704
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:04:36+00:00 2026-05-18T09:04:36+00:00

What i am trying to do is catch a Button click that is inside

  • 0

What i am trying to do is catch a Button click that is inside a ListView managed by a CustomCursorAdapter. when clicked i need to make the button invisible and update a value in the database. here is the code i am using for the ListActivity and the CursorAdapter.

public class MainTabView extends ListActivity{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    fillListData();
}


private void fillListData(){
 DataBaseNamesHelper myDbNamesHelper = new DataBaseNamesHelper(this);
 myDbNamesHelper.openDataBase();
 Cursor cursor = myDbNamesHelper.getCursorQueryWithAllTheTaxiStations();
 startManagingCursor(cursor);
   // the desired columns to be bound
    String[] columns = new String[] { DataBaseNamesHelper.COLUMN_NAME, DataBaseNamesHelper.COLUMN_PEOPLE};
    // the XML defined views which the data will be bound to
    int[] to = new int[] { R.id.name_entry, R.id.number_entry };

    // create the adapter using the cursor pointing to the desired data as well as the layout information
    CustomCursorAdapter mAdapter = new CustomCursorAdapter(this, R.layout.list_entry, cursor, columns, to);
    // set this adapter as your ListActivity's adapter
    this.setListAdapter(mAdapter);   
    this.getListView().setOnItemClickListener(mAdapter);
    myDbNamesHelper.close();

}

and the Adapter:

public class CustomCursorAdapter extends SimpleCursorAdapter implements SectionIndexer,Filterable,
     android.widget.AdapterView.OnItemClickListener{

private Context context;
private int layout;
private AlphabetIndexer alphaIndexer;

public CustomCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to) {
    super(context, layout, c, from, to);
    this.context = context;
    this.layout = layout;
    alphaIndexer=new AlphabetIndexer(c, c.getColumnIndex(DataBaseNamesHelper.COLUMN_NAME), " ABCDEFGHIJKLMNOPQRSTUVWXYZ");
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {

    Cursor c = getCursor();

    final LayoutInflater inflater = LayoutInflater.from(context);
    View v = inflater.inflate(layout, parent, false);

    int nameCol = c.getColumnIndex(DataBaseNamesHelper.COLUMN_NAME);

    String name = c.getString(nameCol);

    /**
     * Next set the name of the entry.
     */
    TextView name_text = (TextView) v.findViewById(R.id.name_entry);
    if (name_text != null) {
        name_text.setText(name);
    }

    int favCol = c.getColumnIndex(DataBaseNamesHelper.COLUMN_FAVOURITED);
    int fav = c.getInt(favCol);

    Button button = (Button) v.findViewById(R.id.Button01);
    if(fav==1){
     button.setVisibility(View.INVISIBLE);
    }

    return v;
}

@Override
public void bindView(View v, Context context, Cursor c) {

    int nameCol = c.getColumnIndex(DataBaseNamesHelper.COLUMN_NAME);

    String name = c.getString(nameCol);

    /**
     * Next set the name of the entry.
     */
    TextView name_text = (TextView) v.findViewById(R.id.name_entry);
    if (name_text != null) {
        name_text.setText(name);
    }
    int favCol = c.getColumnIndex(DataBaseNamesHelper.COLUMN_FAVOURITED);
    int fav = c.getInt(favCol);

    Button button = (Button) v.findViewById(R.id.Button01);
    Log.e("fav",String.valueOf(fav));
    if(fav==1){
     button.setVisibility(View.INVISIBLE);
    }
}

 @Override
 public int getPositionForSection(int section) {
  return alphaIndexer.getPositionForSection(section);
 }

 @Override
 public int getSectionForPosition(int position) {
    return alphaIndexer.getSectionForPosition(position);
 }

 @Override
 public Object[] getSections() {
    return alphaIndexer.getSections();
 }
 @Override
 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
  Log.e("item Click", arg1.toString()+ " position> " +arg2);
 }

i have already set the button to be clickable(true) and focusable(false).

with this code i can achieve what i want but by clicking the listView row (logs only item clicks on the LinearLayout that is holding the button. how do i make the button receive click exactly the same as LinearLayout does?

here is the row layout:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:orientation="horizontal" android:focusable="false">
 <TextView
  android:id="@+id/name_entry"
  android:layout_height="wrap_content"
  android:textSize="28dip" android:layout_width="wrap_content" android:layout_weight="1" android:layout_gravity="center_vertical"/>
       <Button android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Fav" android:layout_gravity="center_vertical" android:layout_marginRight="10dp" android:focusable="false" android:clickable="true"></Button><TextView
  android:id="@+id/number_entry"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:textSize="28dip" />


</LinearLayout>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T09:04:36+00:00Added an answer on May 18, 2026 at 9:04 am

    you need a new aproach as is this described in the button documentation.

    However, instead of applying an OnClickListener to the button in your activity, you can assign a method to your button in the XML layout, using the android:onClick attribute. For example:

    <Button
         android:layout_height="wrap_content"
         android:layout_width="wrap_content"
         android:text="@string/self_destruct"
         android:onClick="selfDestruct" />
    

    Now, when a user clicks the button, the Android system calls the activity’s selfDestruct(View) method. In order for this to work, the method must be public and accept a View as its only parameter. For example:

     public void selfDestruct(View view) {
         // Kabloey
     }
    

    The View passed into the method is a reference to the widget that was clicked. You can setTag() on the View in the adapter to recognize which button was clicked.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What I was trying to do is that when I click the Add button
I'm trying to catch a JSON object with a mouse click event. I use
I'm trying to catch a double-click event in a TreeView's empty area to create
I'm using Cognos and I'm trying to catch the enter key so that the
I'm just trying to run a new thread each time a button click even
I am trying to raise a click event on a submit button in a
I am trying to press a button that takes the text from textbox4 and
So i'm trying to make a program that can find how many pixels of
I'm trying to setup a button on a map that allows a user to
I've inherited a website developed using Struts 1.2 that I need to make some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.