Android - 在textview中显示ListView项目

时间:2011-01-03 17:22:54

标签: android listview textview alertdialog

我设法设置了一个页面,以便用户点击列表视图中的项目,然后显示一个警告对话框,并为他们提供预订或取消的选项。如果他们点击了书,就会将他们带到另一个屏幕。我需要的是显示在下一个屏幕上选择的项目。我几乎拥有它,但我无法解决它出错的地方。我希望的是,如果有人可以帮助我找到最佳方法吗?

我目前的代码是 -

        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(ListTaxi.this); 
             adb.setTitle("Taxi Booking");
             adb.setMessage("You have chosen = "+lv.getItemAtPosition(position)); 
             adb.setPositiveButton("Book", new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int id) {
                     Intent intent = new Intent(getApplicationContext(), Booking.class);
                     intent.putExtra("booking",  taxi[selectedPosition]);
                     startActivity(intent);
                 }
             });
             adb.setNegativeButton("Cancel", null); 
             adb.show(); 
         }
     });

在booking.java上是:

public void onCreate(Bundle savedInstanceState){             super.onCreate(savedInstanceState);             的setContentView(R.layout.booking);             TextView tv_taxi =(TextView)findViewById(R.id.tvtaxi);             //tv_taxi.setText(taxi);

        Intent intent = getIntent();
        String booking = "";
        if (intent != null) {
            Bundle extras = intent.getExtras();
            if (extras != null) {
                booking = extras.getString("booking");   

            }


            }

        }

原始文章位于 - Android - Change View when alertdialog button is clicked

谢谢大家

1 个答案:

答案 0 :(得分:1)

您只需将TextView上的文字设置为您从Bundle检索到的值:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.booking);
    TextView tv_taxi = (TextView) findViewById(R.id.tvtaxi);
    Intent intent = getIntent();
    String booking = "";
    if (intent != null) {
        Bundle extras = intent.getExtras();
        if (extras != null) {
            booking = extras.getString("booking");
            tv_taxi.setText(booking);
        }
    }
}