ListView ……
5/1/20 AAiT 13
• In activity xml file we need to define
ListView, similarly to all components,
• Define a string array to contain all
the lists to be displayed in the
MainActivity.java,
• Define the ListView in the
MainActivity to associate with
specific actions when the list is
clicked,
• We need also to define a list adaptor
to hold the list to be created,
• Set also item click listener to react to
the selection
……….
public class MainActivity extends AppCompatActivity {
ListView listFruits;
@Override
protected void onCreate(Bundle savedInstanceState) {
……….
listFruits = (ListView) findViewById(R.id.list_fruits);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, fruits);
listFruits.setListAdapter(adapter);
listFruits.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView,
View view, int i, long l) {
Toast.makeText(getApplicationContext(), "Selected ” +
fruits[i], Toast.LENGTH_SHORT).show();
}
});
}
………..