How to use ArrayAdapter

后端 未结 5 1835
夕颜
夕颜 2020-11-22 14:23
ArrayList myList = new ArrayList();

ListView listView = (ListView) findViewById(R.id.list);

ArrayAdapter adapter = new         


        
5条回答
  •  伪装坚强ぢ
    2020-11-22 14:45

    Here's a quick and dirty example of how to use an ArrayAdapter if you don't want to bother yourself with extending the mother class:

    class MyClass extends Activity {
        private ArrayAdapter mAdapter = null;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            mAdapter = new ArrayAdapter(getApplicationContext(),
                android.R.layout.simple_dropdown_item_1line, android.R.id.text1);
    
            final ListView list = (ListView) findViewById(R.id.list);
            list.setAdapter(mAdapter);
    
            //Add Some Items in your list:
            for (int i = 1; i <= 10; i++) {
                mAdapter.add("Item " + i);
            }
    
            // And if you want selection feedback:
            list.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView parent, View view, int position, long id) {
                    //Do whatever you want with the selected item
                    Log.d(TAG, mAdapter.getItem(position) + " has been selected!");
                }
            });
        }
    }
    

提交回复
热议问题