that when clicked, opens an
我在 TabActivity AlertDialog with 2
EditText Views中有一个按钮,用于输入名称和编号。
当我单击确定Button
关闭 时,Dialog
我想将名称传递回ListView
. 我可以将名称传TabActivity
回. 但是除了名称之外的其他内容显示在.EditText
mAlertDialog
TabActivity
ListView
它看起来像是对对象“widget”的引用(Widget
是类的对象Device
,它有getName
,setName
方法),com.mypackage.Device@419226e0。
我将尝试在下面发布相关代码(是的,我知道我没有使用Fragments
。我发现很难使用 with 实现水平滚动标签ListView
)Fragments
:
@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
“com.mypackage.Device@419226e0”中,而不是我在AlertDialog
框中键入的名称。
这是否与范围或匿名内部类有关?请帮忙。我对Java不是很熟悉,所以我在这里迷失了细节。