开发者

Add onclick at more then one object

开发者 https://www.devze.com 2023-03-09 02:56 出处:网络
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

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.开发者_JS百科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


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.


to define onClick to more than one object , your class should implement the interface OnClickListener , and override the method onClick(View v) , like this :

public class MyActivity extends Activity implements OnClickListener{
@Override
public void onCreate(Bundle savedInstance){
....
//add listeners to your buttons 
btn1.setOnClikListener(this);
btn2.setOnClickListener(this);
....
}

@Override
public void onClick(View v ) {

if(v==btn1)
   // treatement 1 
if(v== btn2)
   //treatment 2 
...etc 
}
0

精彩评论

暂无评论...
验证码 换一张
取 消