I am trying to make an app, and I am trying to sort a list of bus routes in one list, but when you click on the name of the bus route, a message pops up with the option to click North/South or East/West.
I just wondering what is the best way to be to have the bus routes in a list, but to also code in the correct North/South or East/West, depending on which one you click.
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
//import android.widget.Toast;开发者_JAVA百科
public class BusRoutes extends ListActivity {
static final String[] BUSROUTES = new String[] {
"01 Kipps Lane / Thompson Road", "02 Dundas", "03 Hailton Rd",
"04 Oxford East", "05 Springbank", "06 Richmond", "07 Wavell",
"08 Riverside", "09 Whitehills", "10 Wonderland", "11 Southcrest",
"11 SouthCrest", "12 Wharncliffe South", "13 Wellington Road",
"13 Highbury", "15 Westmount", "16 Adelaide", "17 Oxford West",
"19 Oakridge", "20 CherryHill", "21 Huron Heights", "22 Trafalgar",
"23 Berkshire", "24 Baseline", "25 Killally", "26 Jalna Blvd West",
"27 Fanshawe College", "28 Lambeth", "30 Newbold",
"31 Orchard Park", "32 Windermere", "33 Proudfoot", "34 Medway",
"35 Argyle", "36 Airport/Industrial", "37 Sovereign Road",
"38 Stoney Creek", "39 Fanshawe West" };
static final String[] BUSROUTES2 = new String[] {
"18 Kipps Lane / Thompson Road", };
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, BUSROUTES));
getListView().setTextFilterEnabled(true);
}
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
new AlertDialog.Builder(this)
.setTitle("Choose Direction")
// Change to display bus times
//.setMessage("from " + getListView().getItemAtPosition(position))
.setPositiveButton("North",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
}
})
.setNeutralButton("South",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
}
}).show();
Creating seperate list for each N/S and W/E is reasonable.
On the implementation part you asked:
Create two List
s from these two arrays.
List l = Arrays.asList(BUSROUTES);
Collections.sort(l);
Or you can create custom method like
public List myConverterMethod(String[] MyBusArray)
{
List list = new ArrayList();
for(String s: MyBusArray)
{
list.add(s);
}
Collections.sort(list);
return list;
}
To append both lists into one. Use Firstlist.addAll(secondList);
therefore you have elements from both of arrays/lists into one list.
I realized that each item has its own ID with its respective position in the array, so i just made a If - else for NorthSouth and EastWest.
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
if(
id == 0 || id == 3 || id == 5 || id == 9 || id == 12 ||
id == 13 || id == 14 || id == 15 || id == 19 || id == 23 ||
id == 24 || id == 26 || id == 32 || id == 34 || id == 35
){
new AlertDialog.Builder(this)
.setTitle("Choose Direction")
// Change to display bus times
//.setMessage("from " + getListView().getItemAtPosition(position))
.setPositiveButton("North",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
}
})
.setNeutralButton("South",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
}
}).show();
else{
new AlertDialog.Builder(this)
.setTitle("Choose Direction")
// Change to display bus times
.setPositiveButton("East",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
}
})
.setNeutralButton("West",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
}
}).show();
}
I'm not sure if I'm understanding you correctly, but I would make some type of BusRoute class, with 3 fields - name, route1, and route2. When you implement specific routes, you can set route1 / route2 depending on the type of routes you need.
public class BusRoute {
private String routeName;
private String route1;
private String route2;
public void setRoute1(String route1){
this.route1 = route1;
}
public String getRoute1(String route2){
return route2;
}
....
}
So simply calling getRoute1()
and getRoute2()
will result in fetching the proper display message for your buttons.
This is a crude example I made, and should be expanded upon, but hopefully it relates the idea I'm trying to get across.
精彩评论