android 将对象成员从 alertdialog 传递回 listview

标签 android listview tabs android-alertdialog

我在 TabActivity 中有一个按钮,单击该按钮时,会打开一个AlertDialog带有 2EditTextViews 的名称和编号。

当我单击 Ok Button 关闭 Dialog 时,我想将名称传递回 上的 ListView选项卡 Activity 。我可以获得要传递回 TabActivity 上的 EditTextmAlertDialog 的名称。但是 ListView 中显示的不是名称。

它看起来像是对对象“widget”的引用(Widget 是类Device 的一个对象,它有getNamesetName 方法),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/

相关文章:

android - 传输 5 个 Activity 页面的数据 26 个编辑文本字段并在最后一个 android Activity 中计算

SwiftUI 列表行未调整大小以适应内容

listview - Xamarin 表单 ListView 组标题模板不起作用

c# - 没有打开IE窗口时,如何在C#中打开多个IE选项卡?

android - getPixel 在 Android 中返回负值

java - 扩展文件下载功能不工作

Android Media Player 停止后无法播放

android - 如何使用 Android onItemClick 获取 ListView 项?

德尔福:毛玻璃片

javascript - 使用 Jquery 递增选项卡