Step 1: Add ListView on activity_main.xml
MainActivity.java:
public class MainActivity extends AppCompatActivity {ListView listView;
String[] cities = { "dhaka", "chattogram", "Rongpur", "mongla", "sylhet"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1 , cities);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "Clicked " + cities[position] , Toast.LENGTH_SHORT).show();
}
});
}
}
একটি মন্তব্য পোস্ট করুন
0 মন্তব্যসমূহ