开发者

how select item from a ListView?

开发者 https://www.devze.com 2023-03-09 23:58 出处:网络
my class extends Activity, not ListActivity. i have this code on create method but i select an item from the list, the background of it dont stay orange. I have to move the arrows in the emulator for

my class extends Activity, not ListActivity. i have this code on create method but i select an item from the list, the background of it dont stay orange. I have to move the arrows in the emulator for down to navigate on the listview.

When i click on the button center on the emulator, the log dont show the message.

I tired of try many thinks and i still without get the action when i select one item from the list. Any sugestions?? thanks a lot

      final List<Profile> profilesList = getProfilesList();
  ProfileArrayAdapter3 adapter = new ProfileArrayAdapter3(
        getApplica开发者_如何学CtionContext(), R.layout.profiles_item, profilesList);
  listViewProfiles = (ListView) this.findViewById(R.id.profilesList);

  listViewProfiles.setAdapter(adapter);
  listViewProfiles.setOnItemClickListener(new OnItemClickListener() {

     @Override
     public void onItemClick(AdapterView<?> a, View v, int position, long id) {
        Toast.makeText(getApplicationContext(), profilesList.get(position).getCardNumber(), Toast.LENGTH_SHORT).show();
        Log.d("cardNumber",profilesList.get(position).getCardNumber());
     }});


Personnaly, I prefer to use click listeners on my view than using a itemclicklistener on the list itself. Click listeners on views can be shared, and you will get the source of the event using the parameter of onClick.

Here is an example :

private SharedClickListener sharedListener = new SharedClikListener();

private class MovieArrayAdapter extends ArrayAdapter<Movie>
{
    public MovieArrayAdapter()
    {
        super(getApplicationContext(), R.layout.profiles_item, profilesList);
    }//cons

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
                    //you should build your own views here and fill them with
                    //a profile object, here I use super to compile the example
                    View view = super.getView(position, convertView, parent );
                    view.setOnClickListener( sharedListener );
        return view;
    }//met
}//inner class

private class ItemClickListener implements View.OnClickListener {

    @Override
    public void onClick(View v) {
        Toast.makeText(getApplicationContext(), ((your view class)v).getProfile().getCardNumber(), Toast.LENGTH_SHORT).show();
    Log.d("cardNumber",((your view class) v).getProfile().getCardNumber()); 
        }// met
}//inner class

Regards, Stéphane

0

精彩评论

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