2011-08-24 2 views
0

앱을 만들려고하고 있는데 한 목록에서 버스 노선 목록을 정렬하려하지만 버스 경로의 이름을 클릭하면 북쪽/북쪽을 클릭하는 옵션이있는 메시지가 표시됩니다. 남쪽 또는 동쪽/서쪽.두 문자열 배열을 하나의 목록으로 올바른 순서로 정렬하는 방법. - Android

나는 버스 경로를 목록에 넣는 것이 가장 좋은 방법 일뿐만 아니라 클릭 한 항목에 따라 정확한 북/남 또는 동/서에 코드를 작성하는 것이 궁금합니다.

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

글쎄, 그들은 버스 번호 01 KipsLane..etc와 함께 주문됩니다. 나는 kipslane을 클릭하면 북쪽 또는 남쪽을 말하는 메시지가 나타납니다. 북쪽/남쪽으로가는 모든 버스와 동쪽/서쪽으로가는 하나의 그룹에 대해 두 개의 다른 배열을 하나의 그룹으로 만들어야합니다. 나중에 코딩 작업을 수행하거나 더 좋은 방법이 있습니다. –

답변

0

내가 제대로 이해하고있어 있는지 확실하지 않습니다,하지만 난 3 개 필드, BusRoute 클래스의 몇 가지 유형을 만들 것 - 이름, 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()를 호출.

이것은 내가 작성한 포괄적 인 예이며, 확장해야하지만 잘하면 필자가 얻으려는 아이디어와 관련이 있습니다.

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);을 사용하면 두 배열/목록의 요소가 하나의 목록에 포함됩니다.

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(); 

}

관련 문제