I recently started working on an Android project and given my lack of understanding of UI programming, I find myself in a fix, when I try to dynamically add a entry to the UI.
I am working on a chat-application in which when a new message is received, it should be added at the bottom. For that I need to notify that the data-set has changed. When I was directly calling data-set has changed, I was getting an error. So I used a local broadcast reciever on the adapter.
Unfortunately, I don't know how to pass the ArrayList which contains a HashMap. I will post the code of the adapter and where I am trying to notify where data has changed. I hope someone can help me out. THanks a lot.. :-)
Code :
    public static void recieveUpdatedMessage(String channelName, Map<String, Object> input){
//The input here contains message from our PUSH system 
  Intent intent = new Intent();
                    HashMap<String, String> insertMap = new HashMap<>();
                    insertMap.put(chatText, String.valueOf(input.get("text")));
                    insertMap.put(firstName,String.valueOf("firstname"));
                    insertMap.put(groupChannel, "/service/chat" + String.valueOf(groupAccountId));
                    ArrayList<HashMap<String, String>> chatMessagesHashMapList = new ArrayList<HashMap<String, String>>();
                    chatMessagesHashMapList.add(insertMap);
// Below is where I am trying to send data.
                 //  intent.putExtra(chatMessagesHashMapList);//send any data to your adapter
                    intent.setAction("myaction");
                    LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
The adapter code, it's in same java file :
public class ChatMessagesAdapter extends BaseAdapter {
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent)
    {
        if(intent.getAction().equals("MYREFRESH"))
        {
            notifyDataSetChanged();
        }
    }
};
    private Activity activity = null;
    private ArrayList<HashMap<String, String>> data;
    private LayoutInflater inflater = null;
    public ChatMessagesAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("MYREFRESH");
        LocalBroadcastManager.getInstance(context).registerReceiver(broadcastReceiver, intentFilter);
        activity = a;
        data = d;
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
        return data.size();
    }
    @Override
    public Object getItem(int position) {
        return position;
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if (convertView == null)
            vi = inflater.inflate(R.layout.chat_messages_row, parent, false);
        TextView chatText = (TextView) vi.findViewById(R.id.chatText);
        ImageView userImage = (ImageView) vi.findViewById(R.id.chatImage);
        TextView firstName = (TextView) vi.findViewById(R.id.personName);
        HashMap<String, String> chatList = new HashMap<>();
        chatList = data.get(position);
        chatText.setText(chatList.get(ChatMessagesActivity.chatText));
        userImage.setImageBitmap(convertByteArrayToBitmap(chatList.get(ChatMessagesActivity.chatImage)));
        firstName.setText(chatList.get(ChatMessagesActivity.firstName));
        return vi;
    }
}
I hope I was clear. If there is anything missing, kindly let me know. How can I tell the activity that there is a new message and put it at the bottom.
 
     
    