Archive

Posts Tagged ‘sending’

ListView in one activity doesn’t refresh data sending another activity

July 15, 2011 1 comment

There’s a problem in my previous post on Auto Receiving SMS that the ListView in MainActivity doesn’t refresh the underlying data which is sent from BroadcastReceiver.

The reason is:

– When application is load, the onCreate() of MainActivity is called, at this time there’s no point of new arriving message, and that’s done! Data will never update more to the ListView.

The solution:
– While SmsReceiver continuously waits for new message and sends it directly to MainActivity, the new message will be stored on the Intent of the automatically-triggered onNewIntent() method.

– What we need to do is to handle new message inside this method.

– This is the update version of MainActivity

package pete.android.study;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;

public class MainActivity extends Activity {
    ListView mListData;
    ArrayList<SmsInfo> mListSms = new ArrayList<SmsInfo>();
    SmsInfoAdapter mAdapter;

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

        mListData = (ListView)findViewById(R.id.lvData);
    }

    @Override
    protected void onNewIntent(Intent data) {
    	ArrayList<SmsInfo> tmp = data.getParcelableArrayListExtra("ListSMS");
    	mListSms.add(tmp.get(tmp.size() - 1));

    	if(mAdapter == null) {
    		mAdapter = new SmsInfoAdapter(this, mListSms);
    		mListData.setAdapter(mAdapter);
    		return;
    	}
    	mAdapter.notifyDataSetChanged();
    }
}

– Yes, at first time when new message arrives, the adapter is null and it should be created.

– The next time it’s not null but we need to notify the ListView about data changed.

Question here: why do I receive new message storing in a temp ArrayList instead of directly using mListSms itself?

The answer is: I don’t want to change the reference of data in which I already set into the adapter at the first time. Assigning mListSms to the new message ArrayList means to create new ArrayList<SmsInfo> not using the old one. If I do, the, calling of mAdapter.notifyDataSetChanged() won’t respond or do nothing because it doesn’t have reference to the old list. Also, the new message arriving always stays at last position in the sending list from SmsReceiver, so I just need to add the last item from temp list is enough.

 

Hope you learn something from this!

 

Cheers,

Pete Houston

Sending messages among devices

July 7, 2011 1 comment

It’s very simple. Just launch all devices you need to test and send your message to device you want by the number on the top of each device, such as: 5554, 5556, 5558…..

Message among devices

Message among devices

Message among devices 2

Message among devices 2

 

Hope you like it !

 

Cheers,

Pete Houston