Textview仅显示ListView中的最后一项

时间:2019-03-19 01:37:33

标签: java android listview textview

我正在尝试使用textview显示从列表视图中选择的联系人。列表视图包含用户从android电话簿中选择的联系人,这些联系人已加载到列表视图中。即使用户选择了不是列表视图中的最后一个联系人的文本联系人,textview也将仅显示列表视图中的最后一个条目。

即使在日志打印中,我也可以看到我选择了哪个联系人,但是当我尝试将其打印到另一个窗口的textview中时,它仍然默认为最后一个联系人。

Contacts List Text Display

addModem.java

 display_contacts1.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {

        @Override
        public void onItemClick(AdapterView<?> adapter, View view, int position, long id)
        {
            // TODO Auto-generated method stub
            Object o = adapter.getItemAtPosition(position);
            String str_text = o.toString();

            String name = AddModem.name;
            String phoneNumber = AddModem.phoneNo;

            Log.e("Contacts selected", str_text);

            // Display string here showing which contact was selected
            Toast.makeText(getApplicationContext(), str_text + " Selected for Communication", Toast.LENGTH_LONG).show();

            // Update Modem View Contact
            modemView.setText(str_text);

            //modemView.setText(name);
            arrayAdapter.notifyDataSetChanged();

            // Go back to the main screen after the contact has been selected
            Intent main_menu = new Intent(AddModem.this, MainActivity.class);
            main_menu.putExtra("StrValue", str_text);
            startActivity(main_menu);

        }

    });

MainActivity.java

// This checks to see if the current contact is selected
        if ( display_contacts.isEmpty())
        {
            number = null;

            // No contacts selected from the Add Modem listview
            modemView.setText("No contact selected");
        }
        else
        {
            runOnUiThread(new Runnable()
            {
                // This is not updating to the one selected
                @Override
                public void run()
                {
                    modemView.setText(name);
                    Log.e("DisplayName", name);
                }
            });

        }

1 个答案:

答案 0 :(得分:0)

在主要活动代码中,我们需要

name = getIntent().getStringExtra("StrValue");

在将其打印到textView之前

modemVide.setText(name);