Reputation: 31
I want to open an Activity called Bihar.class when I will click on Bihar in listview. and also open other activities as well when clicking on other items in the list. so How to implement this please help me.
I have made custom list and custom adapter for thisenter image description here
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<State> stateNames = new ArrayList<>();
stateNames.add(new State("Andhra Pradesh","Amaravati"));
stateNames.add(new State("Arunachal Pradesh","Itanagar"));
stateNames.add(new State("Assam","Dispur"));
stateNames.add(new State("Bihar","Patna"));
stateNames.add(new State("Chhattisgarh","Raipur"));
stateNames.add(new State("Goa","Panaji"));
stateNames.add(new State("Gujarat","Gandhinagar"));
stateNames.add(new State("Haryana","Chandigarh"));
stateNames.add(new State("Himachal Pradesh","Shimla"));
stateNames.add(new State("Jharkhand","Ranchi"));
stateNames.add(new State("Karnataka","Bangalore"));
stateNames.add(new State("Kerala","Thiruvananthapuram"));
stateNames.add(new State("Madhya Pradesh","Bhopal"));
stateNames.add(new State("Maharashtra","Mumbai"));
stateNames.add(new State("Manipur","Imphal"));
stateNames.add(new State("Meghalaya","Shillong"));
stateNames.add(new State("Mizoram","Aizawl"));
stateNames.add(new State("Nagaland","Kohima"));
stateNames.add(new State("Odisha","Bhubaneshwar"));
stateNames.add(new State("Punjab","Chandigarh"));
stateNames.add(new State("Rajasthan","Jaipur"));
stateNames.add(new State("Sikkim","Gangtok"));
stateNames.add(new State("Tamil Nadu","Chennai"));
stateNames.add(new State("Telangana","Hyderabad"));
stateNames.add(new State("Tripura","Agartala"));
stateNames.add(new State("Uttarakhand","Dehradun"));
stateNames.add(new State("Uttar Pradesh","Lucknow"));
stateNames.add(new State("West Bengal","Kolkata"));
stateNames.add(new State("Andaman and Nicobar Islands","Port Blair"));
stateNames.add(new State("Chandigarh","Chandigarh"));
stateNames.add(new State("Dadra and Nagar Haveli and Daman & Diu", "Daman"));
stateNames.add(new State("Delhi","Delhi"));
stateNames.add(new State("Jammu and Kashmir","Srinagar(Summer" +
") Jammu(Winter)"));
stateNames.add(new State("Lakshadweep","Kavaratti"));
stateNames.add(new State("Ladakh","Leh"));
stateNames.add(new State("Puducherry","Puducherry"));
StateCapitalAdapter adapter = new StateCapitalAdapter(this,stateNames);
ListView listView = (ListView)findViewById(R.id.list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
}
}
Upvotes: 0
Views: 301
Reputation: 1071
Modify your State class to have another member Class activityClass;
which is a reference to the Activity class name for that State and add those States like following:
stateNames.add(new State("Assam", "Dispur", Assam.class));
stateNames.add(new State("Bihar", "Patna", Bihar.class));
...
Now, change your listener like following
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, stateNames.get(position).activityClass);
startActivity(intent);
}
});
This will launch your preferred Activity for that State.
Upvotes: 1