Having a custom ListView with different options to choose from
I'm currently building a ListView VERY similiar to the standard alarm clock app that comes with any android phone today. In the app they have a ListView that has different views for each row.
For example:
http://www.androidpeople.com/wp-content/uploads/2010/05/listviewwithmultiplechoice.png
and
http://www.talkandroid.com/android-forums/attachments/android-development-answers-tutorials-code-snippets/360d1234754310-add-edittext-backgroundtxt-child-expandable-list-screencap1.png
I would like for both of the options to use checkboxes and the expandable View on the same ListView. I've tried looking for tutorials but none have really helped.
In my code, I'm looking to have the view be a combination of the expandable list View and multiple choice view. Whats the best way to do something like this? Here is my code currently.
public class AlarmList extends ListActivity {
AlarmListItem[] items = {
new AlarmListItem("Turn On Alarm", Alerts.class),
new AlarmListItem("Time", Person.class),
new AlarmListItem("Time", MTSU_Android.class),
new AlarmListItem("Sound", Person.class),
new AlarmListItem("Vibrate", Alerts.class),
new AlarmListItem("Repeat", Alerts.class),
new AlarmListItem("Volume", MTSU_Android.class),
new AlarmListItem("BackupAlarm", Person.class)};
@Override
public void onCreate (B开发者_StackOverflow中文版undle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main3);
//ListView list1 = ((ListView)findViewById(R.id.ListView01));
setListAdapter(new ArrayAdapter<AlarmListItem>(
this, android.R.layout.simple_list_item_1, items));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id )
{
super.onListItemClick(l, v, position, id);
final Intent intent = new Intent(this, items[position].getActivity());
startActivityForResult(intent, position);
}
You need to extend ListAdapter and override getView. Use getItem using the passed position to obtain the object and then return a view suitable for the object passed.
If the view depends on the class of object, then test using instanceof.
精彩评论