我在 TabActivity 中有一个按钮,单击该按钮时,会打开一个
AlertDialog带有 2
EditTextViews 的名称和编号。
当我单击 Ok Button
关闭 Dialog
时,我想将名称传递回 上的
。我可以获得要传递回 ListView
选项卡 Activity TabActivity
上的 EditText
框 mAlertDialog
的名称。但是 ListView
中显示的不是名称。
它看起来像是对对象“widget”的引用(Widget
是类Device
的一个对象,它有getName
,setName
方法),com.mypackage.Device@419226e0。
我会尝试在下面发布相关代码(是的,我知道我没有使用 Fragments
。我发现很难用 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
时,它说
Unable to retrieve the correct enclosing instance of this
即使在应用程序中显示了正确的名称。当我让程序在调试后完成时,此引用显示在 ListView
“com.mypackage.Device@419226e0”中,而不是我在 AlertDialog
框中键入的名称。
这与范围或匿名内部类有关吗?请帮忙。我对 Java 不是很熟悉,所以我在这里迷失了细节。
最佳答案
好的,我让它工作了。这非常有帮助:http://www.ezzylearning.com/tutorial.aspx?tid=6816874 ,特别是这句话
You may be wondering how the ListView will display the Product object and which property of the Product object will be displayed. The answer is very simple, by default Android ListView control renders a simple TextView inside every ListView item and TextView control can only display simple text. Notice how the toString() function is overridden in the Product class we defined above. Whatever String you will return from the object toString() function will be displayed in the TextView rendered in the ListView items.
基本上,在我的 Device.java 类中,我必须覆盖 toString 方法来指定要传递的对象成员,所以我包含了这段代码
@Override
public String toString(){
return this.name;
}
关于android 将对象成员从 alertdialog 传递回 listview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12009073/