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 5978415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:30:45+00:00 2026-05-22T21:30:45+00:00

I create a button for each name in my database, then i should add

  • 0

I create a button for each name in my database, then i should add at this button a onclick function, but i don’t know what id use. i use ??? in the code for indicate the position

public class Game extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);

    ListView playersList=(ListView)findViewById(R.id.playersList);

    MyDb db=new MyDb(getApplicationContext());
    db.open();

    Cursor c=db.fetchAllUsers();
    startManagingCursor(c);

    SimpleCursorAdapter adapter=new SimpleCursorAdapter( 
            this,
            R.layout.user,
            c,
            new String[]{MyDb.UsersMetaData.USERS_NAME},
            new int[]{R.id.namePlayer}
    );

    playersList.setAdapter(adapter);
    db.close();

    Button newUser;
    newUser = (Button)findViewById(R.id.buttonNew);
    newUser.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            MyDb db=new MyDb(getApplicationContext());
            db.open();
            EditText inserted = (EditText)findViewById(R.id.editName);
            String nome = inserted.getText().toString();
            db.insertUser(nome);
            db.close();
        }
    });

    Button chosen;
    chosen = (Button)findViewById(R.id.???);
    chosen.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent myIntent = new Intent(view.getContext(), Game2.class);
            startActivityForResult(myIntent, 0);
            finish();
        }
    });
    }

}

user interface

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/sfondo"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:background="@drawable/sfondo">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">  

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"> 

            <EditText
                android:id="@+id/editName"
                android:text="@string/editName"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:lines="1"
                android:width="180px" />
        <Button
                android:text="@string/newPlayer"
                android:id="@+id/buttonNew"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:minWidth="70px" />
        </LinearLayout>
        <ListView
            android:id="@+id/playersList"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </LinearLayout>
</FrameLayout>

xml for the db elements

<?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="fill_parent"
    android:orientation="vertical"
    android:layout_gravity="center_vertical"
    >

    <Button
        android:id="@+id/namePlayer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:minWidth="250px" />
</LinearLayout>

thanks

  • 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-22T21:30:46+00:00Added an answer on May 22, 2026 at 9:30 pm

    So the button is in R.layout.user? If so I think you may need to extend SimpleCursorAdapter and in the getView() method you can find the view and add the listener. Something like this:

    public class MyCursorAdapter extends SimpleCursorAdapter {
        public MyCursorAdapter(...) {
            super(...);
        }
    
        public void getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);
    
            Button button = (Button) view.findViewById(R.id.???);
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    ...
                }
            });
        }
    }
    

    That way your ‘find’ is scoped to one row in the list. You can’t add the listener where you have the code currently because there will be multiple Views with the same ID.

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

Sidebar

Related Questions

i want to create a button/menu in title bar on each windows explore(or in
To create Button and its click event in run time I use: Button b
This should be simple but is boggling me for some reason. I'm sure it's
I create a button programmatically.......... button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchDown]; [button
How do I create a button control (with CreateWindow of a BUTTON window class)
I need to create a button that has the same style as ButtonSpec with
I want to create a button that has a jQuery click on the fly.
I want to create a Button otherthan creating it from xml layout.i want to
I want to create PayPal button with dynamic price (depending from the item) and
I'm trying to create a button with a rectangle since we're gonna have different

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.