0

I have a custom arrayadapter class that has 3 textviews: Request Title, Date and Latest Message. I call that class in my main activity and that reads from a textfile sitting in the phones memory. When I receive a message from my server, how can I change the Latest Message to the one from the server instead of the text file?

My Main Activity

listView = (ListView)findViewById(R.id.lv_requests);

    ra = new RequestAdapter(getApplicationContext(),R.layout.request_adapter);
    listView.setAdapter(ra);

    Date date = new Date();


    if(fileExists("requests")==true){
        try{

            BufferedReader inputReader = new BufferedReader(new InputStreamReader(openFileInput("requests")));
            String in = null;
            while((in=inputReader.readLine())!=null){
                //localbroadcast manager update here

                String[] split = in.split(Pattern.quote("|"));
                String requestTitle = split[0];
                String requestDate = split[2];
                String latestMessage = split[3];
                String registrationID = split[1];
                String audioPath = split[4];
                String fileTitle = split[5];
                String textPath = "";
                Date convDate = convert(requestDate);
                Request newRequest = new Request(requestTitle,registrationID,convDate,latestMessage, audioPath,textPath,fileTitle);
                userReqs.add(newRequest);
                ra.add(newRequest);

            }

        }catch (IOException e){
            e.toString();
        }

The broadcast receiver on the same activity

private BroadcastReceiver onNotice = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
            String str = intent.getStringExtra("msg");
            String requestTitle = intent.getStringExtra("requestTitle");
            String latestMessage = intent.getStringExtra("latestMsg");

    }
};

Do I rewrite the line in the file and refresh the list?

Cœur
  • 37,241
  • 25
  • 195
  • 267
JianYA
  • 2,750
  • 8
  • 60
  • 136
  • Is your `userReqs` a global `List`? If so, have you tried doing something similar as above? `Request r = new Request(params here)` then adding it to the `userReqs` variable? – AL. May 04 '16 at 23:12
  • Yeap its a global variable declared above. However, how can i replace the line in the text file or like update the listview with the request. – JianYA May 04 '16 at 23:49
  • If you want to simply update the `ListView`, I think so long as you update the list then try calling `notifyDataSetChanged`? – AL. May 05 '16 at 00:39
  • Heya this is a possible duplicate of this [thread](http://stackoverflow.com/questions/2250770/how-to-refresh-android-listview). Maybe it can help ; ) – ReyAnthonyRenacia May 05 '16 at 10:53

0 Answers0