2011-01-04 5 views
0

경고 대화 상자에 몇 가지 문제가 있습니다. 일부 문자열이 포함 된 listview가 있고 상자를 클릭하면 예약하거나 취소 할 수있는 경고 대화 상자 (택시 응용 프로그램)가 표시됩니다. 경고 대화 상자에 선택된 항목의 이름이 표시되도록하려고합니다. 하지만 매번 시도 할 때마다 임의의 문자와 숫자가 표시됩니다.선택한 항목의 목록보기의 경고 대화 상자

public class TaxiMain extends ListActivity { 
/** Called when the activity is first created. 
* @return */ 

class Taxi { 
    private String taxiName; 
    private String taxiAddress; 

    public String getName() { 
     return taxiName; 
    } 

    public void setName(String name) { 
     taxiName = name; 
    } 

    public String getAddress() { 
     return taxiAddress; 
    } 

    public void setAddress(String address) { 
     taxiAddress = address; 
    } 

    public Taxi(String name, String address) { 
     taxiName = name; 
     taxiAddress = address; 
    } 
} 

public class TaxiAdapter extends ArrayAdapter<Taxi> { 
    private ArrayList<Taxi> items; 
    private TaxiViewHolder taxiHolder; 

    private class TaxiViewHolder { 
     TextView name; 
     TextView address; 
    } 

    public TaxiAdapter(Context context, int tvResId, ArrayList<Taxi> items) { 
     super(context, tvResId, items); 
     this.items = items; 
    } 

    @Override 
    public View getView(int pos, View convertView, ViewGroup parent) { 
     View v = convertView; 
     if (v == null) { 
      LayoutInflater vi = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE); 
      v = vi.inflate(R.layout.taxi_list_item, null); 
      taxiHolder = new TaxiViewHolder(); 
      taxiHolder.name = (TextView)v.findViewById(R.id.taxi_name); 
      taxiHolder.address = (TextView)v.findViewById(R.id.taxi_address); 
      v.setTag(taxiHolder); 
     } else taxiHolder = (TaxiViewHolder)v.getTag(); 

     Taxi taxi = items.get(pos); 

     if (taxi != null) { 
      taxiHolder.name.setText(taxi.getName()); 
      taxiHolder.address.setText(taxi.getAddress()); 
     } 

     return v; 
    } 
} 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    final String[] taxiNames = getResources().getStringArray(R.array.taxi_name_array); 
    final String[] taxiAddresses = getResources().getStringArray(R.array.taxi_address_array); 

    ArrayList<Taxi> taxiList = new ArrayList<Taxi>(); 

    for (int i = 0; i < taxiNames.length; i++) { 
     taxiList.add(new Taxi(taxiNames[i], taxiAddresses[i])); 
    } 

    setListAdapter(new TaxiAdapter(this, R.layout.taxi_list_item, taxiList)); 

    final ListView lv = getListView(); 
    lv.setTextFilterEnabled(true); 

     lv.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> a, View v, final int position, long id) 
     { 

      final int selectedPosition = position; 
      AlertDialog.Builder adb=new AlertDialog.Builder(TaxiMain.this); 
      adb.setTitle("Taxi Booking"); 
      adb.setMessage("You Have Selected: "+taxiNames); 
      adb.setPositiveButton("Book", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        Intent intent = new Intent(TaxiMain.this, Booking.class); 
        intent.putExtra("booking", taxiNames[selectedPosition]); 
        intent.putExtra("address", taxiAddresses[selectedPosition]); 
        startActivity(intent); 
       } 
      }); 
      adb.setNegativeButton("Cancel", null); 
      adb.show(); 
     } 
    }); 

당신은 코드의 맨 아래에 나타납니다, 늘 제대로 작동 라인을 프로그래머 - 아래

코드 :이 쉽게 이해할 수 있도록 수도로 병이 내 코드를 게시 할 수 있습니다. -

adb.setMessage("You Have Selected: "+taxiNames); 

누구든지이 쇼가 왜 도움이되지 않는지 알 수 있다면 도움이 될 것입니다.

감사합니다.

답변

1

당신이 taxiNames.toString()을 수행하고 +taxiNames을 입력합니다. taxiNames은 많은 항목을 포함하는 배열입니다. 이 파일을 + taxiNames[position]으로 변경해야합니다. 또는 다른 Taxi 개체와 일치하도록 유지하려면 + taxiList.get(position).getName()을 사용할 수도 있습니다.

편집 : 호기심에서 벗어나 selectedPosition에 다른 final int을 (를) 설정하는 이유는 무엇입니까? 메서드 호출에 이미 final int position이 전달되었습니다.

+1

'int position은 원래'final'이 아니었고 메서드의 다른 곳에서 사용되고있었습니다. 이제 '위치'를 직접 사용할 수 있다는 것이 맞습니다. –

+0

건배 동생. 그냥 경고 대화 상자였습니다. 마침내 뭔가 올바른 하하 완료! 너는 내가 얼마나 스트레스를 많이 받는지 모르겠다! 스택 오버플로와 사용자를 칭찬하십시오. 또한 왜 ismail 카트맨이 내 질문을 편집했다고 말합니까? 당신의 대답 바로 위에 – Oli

+0

@dave : 아 좋아요, 제 생각 엔 말이죠. @Oli : 수정 한 내용을 보려면 "편집자"행을 클릭 할 수 있습니다. 대개 담당자가 수정하면 형식 오류 및 그와 같은 문제가 수정됩니다. – kcoppock

2

택시 이름은 문자열이 아니라 배열입니다. 이 작업을 시도해야합니다 :

adb.setMessage("You Have Selected: "+taxiNames[selectedPosition]); 
+0

건배! 나는 그것이 단지 작동하지 않는 이유가 무엇인지를 알기 바로 전에 위치를 잡았습니다. 전설! – Oli

관련 문제