【问题标题】:Why is there an error when getting an item from a ListView in an ArrayAdapter?为什么从 ArrayAdapter 中的 ListView 获取项目时会出错?
【发布时间】:2021-02-18 14:28:36
【问题描述】:

我为 ListView 创建了一个 ArrayAdapter,并使用 ArrayList 的值“msg”定义了元素的 TextView 文本,到目前为止一切正常,但是当我创建另一个元素时,第一个元素将文本更改为新的“msg”值,有人知道错误吗? (文字从葡萄牙语到英语,如果翻译有误,请见谅)

类 ListViewAdapter

public class ListViewAdapter extends ArrayAdapter
    {
        ArrayList<HashMap<Object,Object>> items;

        public ListViewAdapter(Context context,ArrayList<HashMap<Object,Object>> items)
            {
                super(context, R.xml.chat, items);

                this.items = items;
            }

        @Override
        public View getView(int position, View view, ViewGroup parent)
            {
                if (view == null)
                    {
                        view = LayoutInflater.from(getContext()).inflate(R.xml.chat, parent, false);
                    }

                TextView text_name = view.findViewById(R.id.text_name);
                TextView text_message = view.findViewById(R.id.text_message);

                text_message.setText(items.get(position).get("msg").toString());

                return view;
                
            }
            
    }

MainAcrivity:

public class MainActivity extends Activity
    {
        public static ListView list_chat;
        public static EditText edit_message;
        public static Button button_send;

        public static ArrayList<HashMap<Object,Object>> list_map = new ArrayList<>();
        public static HashMap<Object,Object> user_map;
        public static HashMap<Object,Object> chat_map;
        public static HashMap<Object,Object> send_map;

        public static LayoutInflater inflaterChat;
        public static String BUILD;

        public static Texture texture;
        public static ListViewAdapter chatAdapter;

        @Override
        protected void onCreate(Bundle savedInstanceState)
            {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);


                list_chat = findViewById(R.id.list_chat);
                edit_message = findViewById(R.id.edit_message);
                button_send = findViewById(R.id.button_send);

                BUILD = Build.ID.replace(".", "");

                chatAdapter = new ListViewAdapter(this, list_map);

                list_chat.setAdapter(chatAdapter);

                button_send.setOnClickListener(new View.OnClickListener()
                        {
                            public void onClick(View view)
                                {
                                    chat_map = new HashMap<>();

                                    chat_map.put("msg", edit_message.getText());

                                    list_map.add(chat_map);

                                    chatAdapter.notifyDataSetChanged();
                                }
                        });
            }
    }

【问题讨论】:

    标签: java hashmap android-arrayadapter


    【解决方案1】:

    我发现了错误,我将“getText ()”放在了 HashMap 值中,当更新 ListView 时,它没有获取已经定义的值,而是再次执行 getText () 命令 例如:将字符串设置为 " 。 getText ()" 而不是 "HashMap" 我的解决方案是获取字符串中的文本,然后使用该字符串设置 HashMap 的值

      HashMap<Object,Object> chat_map = new HashMap<>();
      String message = String.valueOf(edit_message.getText());
    
      chat_map.put("msg", message);
    
      list_chat_map.add(chat_map);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-22
      • 2023-04-07
      • 1970-01-01
      • 2018-01-31
      • 2011-03-29
      • 2014-08-20
      • 2011-06-09
      相关资源
      最近更新 更多