Detect new data when call is given to webservice

江枫思渺然 提交于 2019-12-24 14:43:10

问题


I have app, in which i am refreshing that app in particular amount of time.

Each time the call is given to webservice and all the messages in database are loaded to listview.

Its done as follows:

public class Messages extends Activity {


    protected Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_messages);

        Intent intent = getIntent();

        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
        String id = intent.getStringExtra(MainActivity.EXTRA_ID);
        String[] lst = null;
        ListView lm=(ListView)findViewById(R.id.listView1);
        TextView tv = (TextView)findViewById(R.id.textView1);

        tv.setText("Welcome " + message);

        handler.postDelayed(new UpdateTask(),750);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.messages, menu);
        return true;
    }

    class UpdateTask implements Runnable {

        @Override
        public void run() {
            // TODO Auto-generated method stub

            setContentView(R.layout.activity_messages);

            Intent intent = getIntent();

            String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
            String id = intent.getStringExtra(MainActivity.EXTRA_ID);
            String[] lst = null;
            ListView lm=(ListView)findViewById(R.id.listView1);
            TextView tv = (TextView)findViewById(R.id.textView1);

            tv.setText("Welcome " + message);

            CallSoap cs=new CallSoap();

            lst=cs.GetMessage(id);

            ArrayAdapter<String> adpt = new ArrayAdapter<String>(Messages.this, android.R.layout.simple_list_item_1,lst);

            lm.setAdapter(adpt);

            handler.postDelayed(this, 500);
        }
    }
}

Now, i am just unable to detect (highlight) new messages in my app, because each time when call is given to webservice it retrieves all messages.

How should i detect that this is new message(or can say as data in listview ) for this particular interval of time.

Please help me.


回答1:


Add one field(i.e. Status) in database. And when you call your service and its return all message from database then status need to change with 1 (0 means still webservice not fetch, 1 means its fetch at android side). So after new records inserted your service only fetch the records which status have 0.

I hope this will help you.



来源:https://stackoverflow.com/questions/18438735/detect-new-data-when-call-is-given-to-webservice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!