为什么从 ArrayAdapter 中的 ListView 中获取项目时会出错?
Why is there an error when getting an item from a ListView in an ArrayAdapter?
我为 ListView 创建了一个 ArrayAdapter,并用 ArrayList 的值“msg”定义了元素的 TextView 文本,到目前为止一切正常,但是当我创建另一个元素,第一个元素将文本更改为新的“msg”值,有人知道错误吗? (文由葡译英,如有翻译错误请见谅)
Class 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;
}
}
主要活动:
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();
}
});
}
}
我发现了错误,我把“getText()”放在一个HashMap值中,当ListView被更新时,它没有得到一个已经定义的值,而是再次执行了getText()命令Ex: Set String to “.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);
我为 ListView 创建了一个 ArrayAdapter,并用 ArrayList
Class 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;
}
}
主要活动:
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();
}
});
}
}
我发现了错误,我把“getText()”放在一个HashMap值中,当ListView被更新时,它没有得到一个已经定义的值,而是再次执行了getText()命令Ex: Set String to “.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);