Articles → ANDROID → arrayadapter in android

arrayadapter in android






What is adapterview and adapter?






Picture showing how data is retrieved from array adapter

Click to Enlarge


What is an arrayadapter?










<ListView    
      android:layout_width="match_parent"    
      android:layout_height="match_parent" 
      android:id="@+id/listview" />




@Override
protected void onCreate(Bundle savedInstanceState) 
{    
    super.onCreate(savedInstanceState);    
    setContentView(R.layout.activity_main);    
    
    String fruitlist[] = {"apple","banana","orange","cherry"};    
    ArrayAdapter<String> itemsAdapter =  new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, fruitlist);    
    ListView listView = (ListView) findViewById(R.id.listview);    
    listView.setAdapter(itemsAdapter);
}




Picture showing the output of array adapter

Click to Enlarge


Creating array from resource file






<resources>    
        <string-array name="sample_array">        
                <item>Banana</item>        
                <item>Apple</item>        
                <item>Orange</item>    
        </string-array>
</resources>




String fruitlist[] = getResources().getStringArray(R.array.sample_array);



Posted By  -  Karan Gupta
 
Posted On  -  Thursday, October 31, 2019

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250