在android中面对Adapter的问题,有什么不对?

时间:2015-08-30 04:14:26

标签: java android android-arrayadapter android-adapter

今天,我突然遇到了前所未见的问题。帮帮我:)

  public class ShowAnimalsListActivity extends Activity
  {
        ArrayList<String> animalsNameList;
        public void onCreate(Bundle saveInstanceState)
        {
                super.onCreate(saveInstanceState);
                setContentView(R.layout.animal_list);

               // Get the reference of ListViewAnimals
                ListView animalList=(ListView)findViewById(R.id.listViewAnimals);


                 animalsNameList = new ArrayList<String>();
                 getAnimalNames();

                 ArrayAdapter<String> arrayAdapter =      
                 new ArrayAdapter<String>(this,android.R.layout.simple_list_item1, animalsNameList);
                 // Set The Adapter
                 animalList.setAdapter(
                 animalList.setOnItemClickListener(new OnItemClickListener()
                    {
                             // argument position gives the index of item which is clicked
                            public void onItemClick(AdapterView<?> arg0, View v,int position, long arg3)
                            {

                                    String selectedAnimal=animalsNameList.get(position);
                                    Toast.makeText(getApplicationContext(), "Animal Selected : "+selectedAnimal,   Toast.LENGTH_LONG).show();
                                 }
                    });
        }

1 个答案:

答案 0 :(得分:0)

您应首先设置适配器,然后编写onClickListener。

animalList.setAdapter(arrayAdapter);

也改变了

String selectedAnimal=animalsNameList.get(position);

String selectedAnimal=String.ValueOf(animalsNameList.get(position));