【发布时间】:2021-02-18 14:28:36
【问题描述】:
我为 ListView 创建了一个 ArrayAdapter,并使用 ArrayList
类 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