我正在尝试创建一个应用程序,我正在尝试在一个列表中对一个总线路由列表进行排序,但是当您单击总线路径的名称时,会弹出一条消息,其中包含单击北/南的选项或东/西。
我只是想知道在列表中使用总线路由的最佳方法是什么,但也要在正确的北/南或东/西进行编码,具体取决于您点击的那个。
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;
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();
答案 0 :(得分:0)
我不确定我是否正确理解你,但我会做一些类型的BusRoute类,有3个字段 - name,route1和route2。实现特定路由时,可以根据所需路由的类型设置route1 / route2。
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;
}
....
}
因此,只需拨打getRoute1()
和getRoute2()
即可获取适合您按钮的显示消息。
这是我做的一个粗略的例子,应该扩展,但希望它与我想要碰到的想法有关。
答案 1 :(得分:0)
为每个N / S和W / E创建单独的列表是合理的。
在执行部分,你问:
从这两个数组中创建两个List
。
List l = Arrays.asList(BUSROUTES);
Collections.sort(l);
或者您可以创建自定义方法,如
public List myConverterMethod(String[] MyBusArray)
{
List list = new ArrayList();
for(String s: MyBusArray)
{
list.add(s);
}
Collections.sort(list);
return list;
}
将两个列表附加到一个列表中。使用Firstlist.addAll(secondList);
因此您将两个数组/列表中的元素放入一个列表中。
答案 2 :(得分:0)
我意识到每个项目都有自己的ID及其在数组中的相应位置,因此我只为NorthSouth和EastWest创建了一个If - else。
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();
}