Basically, I want to discuss, how to bind ListView in android. Before we get started, if you want to know about some essential Tips and Tricks for android development, please go through the following article: Tips and Tricks Android App Developers.
Suppose I have some data in ArrayList<String> format, then put it into ArrayAdapter<String> which will load data to views in ListView using Android Studio.
Code for the drawable layout of ListView
1 2 3 4 5 6 | <ListView android:id="@+id/SimpleListView" android:layout_width="match_parent" android:layout_height="match_parent"></ListView> |
Code for ArrayList data and Adapter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | public class ListviewActivity extends AppCompatActivity { ListView lvSimple; ArrayList<String> arrayList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_listview); lvSimple = (ListView) findViewById(R.id.SimpleListView); arrayList = GenerateData(); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList); lvSimple.setAdapter(adapter); } private ArrayList<String> GenerateData() { ArrayList<String> listData = new ArrayList<String>(); listData.add("Saturday"); listData.add("Sunday"); listData.add("Monday"); listData.add("Tuesday"); return listData; } } |
You may like Tips and Tricks for Android App Developers.
How to bind Listview in Android
The article was published on October 22, 2016 @ 4:08 PM
Leave a Comment