2012-08-17 34 views
2

我在TabActivity中有一個按鈕that when clicked, opens an AlertDialog with 2 EditText Views`爲一個名字和數字。android從alertdialog傳遞對象成員回到列表視圖

當我點擊Ok Button關閉Dialog時,我想將名稱傳回上的ListView。我可以通過TabActivity得到名稱回EditTextmAlertDialog。但名稱以外的內容正在ListView中顯示。

它看起來像到對象「窗口小部件」的引用(Widget是類Device,其具有getNamesetName方法的目的),[email protected]

我會嘗試後下相關的代碼(是的,我知道我沒有使用Fragments我發現很難實現水平滾動的標籤與Fragments一個ListView):

@SuppressWarnings("deprecation") 

public class MainActivity extends TabActivity implements OnClickListener { 

    public static ArrayList<Device> deviceList = new ArrayList<Device>(); 
    public static ArrayAdapter<Device> deviceAdapter=null; 
    private static ListView deviceListView; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

    mTabHost= getTabHost(); 
     TabHost.TabSpec spec; 
     Intent intent; 

    rButton = (Button)findViewById(R.id.button_register); 
     mAlertDialog = (EditText)findViewById(R.id.edittextresult_Name); 


    rButton.setOnClickListener(onRegister); 

    intent = new Intent (this, devices.class); 
     spec = mTabHost.newTabSpec("devices") 
      .setIndicator("Devices") 
      .setContent(R.id.devices); 
     mTabHost.addTab(spec); 

    deviceListView=(ListView)findViewById(R.id.rdevices); 
     //Attach array adapter to data array "deviceList" 
     deviceAdapter = new ArrayAdapter<Device>(this,android.R.layout.simple_list_item_1, deviceList); 
     //connect adapter "deviceAdapter" to listview widget so the activity listview is populated with data from the array 
     deviceListView.setAdapter(deviceAdapter); 
    } 

private View.OnClickListener onRegister = new View.OnClickListener() { 

    public void onClick(View v) { 
     String title = "Register"; 
      String buttonOk = "OK"; 
      String buttonCancel = "Cancel"; 
      String madd = "address"; 
      String name = "widget name"; 


      //get rdevice.xml view 
      LayoutInflater li = LayoutInflater.from(context); 
      View rView = li.inflate(R.layout.rdevice, null); 

      AlertDialog.Builder adRegister = new AlertDialog.Builder(context); 
      //set rdevice.xml to adRegister builder 
      adRegister.setView(rView); 

      //set title 
      adRegister.setTitle(title); 


      //Set EditText views to get user input 

      final EditText mField = (EditText)rView.findViewById(R.id.editText_Address); 
      final EditText nField = (EditText)rView.findViewById(R.id.editText_WidgetName); 

      //set dialog message 
     adRegister.setMessage("Message") 
      .setCancelable(false) 
      .setPositiveButton(buttonOk, new DialogInterface.OnClickListener() { 

       public void onClick(DialogInterface dialog, int BUTTON_POSITIVE) { 
        // TODO Auto-generated method stub 
        Device widget = new Device(); 


        String madd = mField.getText().toString(); 
        String name = nField.getText().toString(); 

       widget.setName(name); 
       widget.setAddress(madd); 

       Log.d(TAG, "Address: " + madd); 
        Log.d(TAG, "Widget name: " + name); 

        //get user input and set it to result on main activity 
        mAlertDialog.setText(nField.getText()); 

        deviceAdapter.add(widget); 
        deviceAdapter.notifyDataSetChanged(); 


       } 
      }) 
      .setNegativeButton(buttonCancel, new DialogInterface.OnClickListener() { 

       public void onClick(DialogInterface dialog, int BUTTON_NEGATIVE) { 

       } 
      }); 



     //Create alert dialog 
     AlertDialog alertDialog = adRegister.create(); 

     //show it 
     adRegister.show(); 

     } 

    }; 

在調試時,logcat中沒有錯誤。

然而,當我看的表達mAlertDialog它說

無法檢索即使正確的名稱將顯示在應用程序的該

正確的外圍實例。當我在調試後讓程序結束時,此參考將顯示在ListView[email protected]」中,而不是在AlertDialog框中鍵入的名稱。

這是否與範圍或匿名內部類有關?請幫忙。我對Java並不熟悉,因此我迷失在這裏的細枝末節。

+0

'mAlertDialog.setText(nField.getText( ));' - 由於'nField'是一個'EditText',所以你需要使用'nField.getText()。toString()'。 – Squonk 2012-08-17 16:26:54

+0

mAlertDialog已經正確顯示我輸入nField的名稱。問題在於deviceListView中顯示的內容。 – jawin 2012-08-17 17:22:26

回答

0

好吧我得到它的工作。這是非常有益的:http://www.ezzylearning.com/tutorial.aspx?tid=6816874,特別是這句話

你可能想知道ListView控件將如何展示產品對象和產品對象的屬性將被顯示出來。答案很簡單,默認情況下,Android ListView控件在每個ListView項目內呈現一個簡單的TextView,而TextView控件只能顯示簡單的文本。注意toString()函數在我們上面定義的Product類中是如何被覆蓋的。無論將從對象返回的字符串toString()函數都將顯示在ListView項目中呈現的TextView中。

基本上,在我的Device.java類,我不得不重寫toString方法來指定哪個對象構件穿過,所以包括在此代碼

@Override 
public String toString(){ 
    return this.name; 
} 
相關問題