Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何从服务类更新SimpleCursorAdapter列表视图?_Android_Sqlite_Service_Background - Fatal编程技术网

Android 如何从服务类更新SimpleCursorAdapter列表视图?

Android 如何从服务类更新SimpleCursorAdapter列表视图?,android,sqlite,service,background,Android,Sqlite,Service,Background,我使用了SimpleCursorAdapter在我的活动类中将数据显示到列表中。我从Sqlite DB获取数据并应用到listview。我从服务类插入数据。在我的服务类中,我从WEB服务获取数据并将最新数据插入Sqlite移动数据库此服务类将每隔5秒运行一次,以检查最新的at Web服务,然后将其带到Sqlite db中插入。当新记录插入Sqlite db时,listview不会使用最新插入的记录进行更新。我已按如下方式实现了我的应用程序: MyActivity.java private MyS

我使用了
SimpleCursorAdapter
在我的活动类中将数据显示到列表中。我从Sqlite DB获取
数据并应用到listview
。我从服务类插入数据。在我的服务类中,我从WEB服务获取数据并将最新数据插入Sqlite移动数据库此服务类将每隔5秒运行一次,以检查最新的at Web服务,然后将其带到Sqlite db中插入。当新记录插入Sqlite db时,listview不会使用最新插入的记录进行更新。我已按如下方式实现了我的应用程序:

MyActivity.java

private MySqliteHelper msh;
ListView lst;
Cursor cursor;

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

        msh = new MySqliteHelper(MyActivity.this);
        msh.openToWrite();
        lst = ((ListView)findViewById(R.id.listView1));
        cursor = msh.queueAll();

        String getFromDB[] = new String[]{MySqliteHelper.KEY_CONTENT1,MySqliteHelper.KEY_CONTENT2};
        int toView[] = new int[]{R.id.msgId,R.id.usrmsg};

        lst.setAdapter(new SimpleCursorAdapter(this, R.layout.list, cursor, getFromDB, toView));

       //calling service class
        bindService(new Intent(ShoutGetMessagesActivity.this, ShoutRepeatService.class), mConnection, Context.BIND_AUTO_CREATE);

        updateList();
    }

      private void updateList(){
              cursor.requery();
               }


        RepeatService bg;
        private ServiceConnection mConnection = new ServiceConnection() {

            public void onServiceConnected(ComponentName className, IBinder binder) {

                bg = ((RepeatService.MyBinder) binder).getService();

                Toast.makeText(MyActivity.this, "Connected",
                        Toast.LENGTH_SHORT).show();

            }

            public void onServiceDisconnected(ComponentName className) {
                bg = null;
            }
        };
    }
    ArrayList<NewMessages> result;
    private MySqliteHelper msh;

     private Timer timer = new Timer();
     private static final long UPDATE_INTERVAL = 500;



    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
          msh = new MySqliteHelper(this);
            msh.openToWrite();

       pollForUpdates();

     super.onCreate();
    }

        private void pollForUpdates() {
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                Log.v("Last message ID", "====>"+MySqliteHelper.LastMsgID);

                if(MySqliteHelper.LastMsgID==null){

                    MySqliteHelper.LastMsgID="0";

                }

                result = new ParseXml().convertUsersDetails(new Generic().getLatestFromWebService(MySqliteHelper.LastMsgID));


                 for(int i=0;i<result.size();i++){
                    String msgs =result.get(i).getMessages();
                    String msgID = result.get(i).getMsgId();
                        Log.v("messages", "------------>>>>"+msgs);
                        Log.v("messageIDs", "--------->>>>>>"+msgID);
                        msh.insert(msgID, msgs);
                    }   

            }
        }, 0, UPDATE_INTERVAL);

        Log.v(getClass().getSimpleName(), "Timer started.");

    }



         private void updateList(){
      cursor.requery();
       }

    public class MyBinder extends Binder {
        RepeatService getService() 
        {
            return RepeatService.this;
        }
    }
}
RepeatService.java

private MySqliteHelper msh;
ListView lst;
Cursor cursor;

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

        msh = new MySqliteHelper(MyActivity.this);
        msh.openToWrite();
        lst = ((ListView)findViewById(R.id.listView1));
        cursor = msh.queueAll();

        String getFromDB[] = new String[]{MySqliteHelper.KEY_CONTENT1,MySqliteHelper.KEY_CONTENT2};
        int toView[] = new int[]{R.id.msgId,R.id.usrmsg};

        lst.setAdapter(new SimpleCursorAdapter(this, R.layout.list, cursor, getFromDB, toView));

       //calling service class
        bindService(new Intent(ShoutGetMessagesActivity.this, ShoutRepeatService.class), mConnection, Context.BIND_AUTO_CREATE);

        updateList();
    }

      private void updateList(){
              cursor.requery();
               }


        RepeatService bg;
        private ServiceConnection mConnection = new ServiceConnection() {

            public void onServiceConnected(ComponentName className, IBinder binder) {

                bg = ((RepeatService.MyBinder) binder).getService();

                Toast.makeText(MyActivity.this, "Connected",
                        Toast.LENGTH_SHORT).show();

            }

            public void onServiceDisconnected(ComponentName className) {
                bg = null;
            }
        };
    }
    ArrayList<NewMessages> result;
    private MySqliteHelper msh;

     private Timer timer = new Timer();
     private static final long UPDATE_INTERVAL = 500;



    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
          msh = new MySqliteHelper(this);
            msh.openToWrite();

       pollForUpdates();

     super.onCreate();
    }

        private void pollForUpdates() {
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                Log.v("Last message ID", "====>"+MySqliteHelper.LastMsgID);

                if(MySqliteHelper.LastMsgID==null){

                    MySqliteHelper.LastMsgID="0";

                }

                result = new ParseXml().convertUsersDetails(new Generic().getLatestFromWebService(MySqliteHelper.LastMsgID));


                 for(int i=0;i<result.size();i++){
                    String msgs =result.get(i).getMessages();
                    String msgID = result.get(i).getMsgId();
                        Log.v("messages", "------------>>>>"+msgs);
                        Log.v("messageIDs", "--------->>>>>>"+msgID);
                        msh.insert(msgID, msgs);
                    }   

            }
        }, 0, UPDATE_INTERVAL);

        Log.v(getClass().getSimpleName(), "Timer started.");

    }



         private void updateList(){
      cursor.requery();
       }

    public class MyBinder extends Binder {
        RepeatService getService() 
        {
            return RepeatService.this;
        }
    }
}
ArrayList结果;
私人MySqliteHelper msh;
专用计时器=新计时器();
私有静态最终长更新间隔=500;
@凌驾
公共IBinder onBind(意图arg0){
//TODO自动生成的方法存根
返回null;
}
@凌驾
public void onCreate(){
msh=新的MySqliteHelper(此);
msh.openToWrite();
pollForUpdates();
super.onCreate();
}
私有void pollForUpdates(){
timer.scheduleAtFixedRate(新TimerTask(){
@凌驾
公开募捐{
Log.v(“最后一个消息ID”,“=>”+MySqliteHelper.LastMsgID);
if(MySqliteHelper.LastMsgID==null){
MySqliteHelper.LastMsgID=“0”;
}
结果=new ParseXml().convertUsersDetails(new Generic().getLatestFromWebService(MySqliteHelper.LastMsgID));

对于(int i=0;i您可以在活动中使用广播接收器,并让服务在数据更新时发出广播。您必须定义自定义广播才能使其工作

首先。 像这样定义您的自定义操作

public static final String DATA_INSERTED = "WHATEVER_YOU_WANT"
当服务中的数据发生更改时,请执行此操作

Intent intent = new Intent (DATA_INSERTED);
sendBroadcast(intent)

初始化自定义广播接收器以侦听此特定的数据插入操作。重写onReceive方法。在onReceive方法中。您必须在适配器上调用notifydatasetchanged,这也意味着您需要对simplecursoradapter的引用。我将把广播接收器实现留给您来解决

您可以在活动中使用广播接收器,并让服务在数据更新时发出广播。您必须定义自定义广播才能使其工作

首先。 像这样定义您的自定义操作

public static final String DATA_INSERTED = "WHATEVER_YOU_WANT"
当服务中的数据发生更改时,请执行此操作

Intent intent = new Intent (DATA_INSERTED);
sendBroadcast(intent)

初始化自定义广播接收器以侦听此特定的数据插入操作。重写onReceive方法。在onReceive方法中。您必须在适配器上调用notifydatasetchanged,这也意味着您需要对simplecursoradapter的引用。我将把广播接收器实现留给您来解决

您可以使用intent创建广播,以便从服务的run()方法调用活动中的函数

private void pollForUpdates() {
timer.scheduleAtFixedRate(new TimerTask() {
    @Override
    public void run() {
        Log.v("Last message ID", "====>"+MySqliteHelper.LastMsgID);

        if(MySqliteHelper.LastMsgID==null){

            MySqliteHelper.LastMsgID="0";

        }

        result = new ParseXml().convertUsersDetails(new Generic().getLatestFromWebService(MySqliteHelper.LastMsgID));


         for(int i=0;i<result.size();i++){
            String msgs =result.get(i).getMessages();
            String msgID = result.get(i).getMsgId();
                Log.v("messages", "------------>>>>"+msgs);
                Log.v("messageIDs", "--------->>>>>>"+msgID);
                msh.insert(msgID, msgs);
            }   

    }
*************************************
Intent intent = new Intent();
intent.setAction("com.example.updatelist");
sendBroadcast(intent); // finally broadcast
*************************************

}, 0, UPDATE_INTERVAL);

Log.v(getClass().getSimpleName(), "Timer started.");

}

您可以使用intent创建广播,以便从服务的run()方法调用活动中的函数

private void pollForUpdates() {
timer.scheduleAtFixedRate(new TimerTask() {
    @Override
    public void run() {
        Log.v("Last message ID", "====>"+MySqliteHelper.LastMsgID);

        if(MySqliteHelper.LastMsgID==null){

            MySqliteHelper.LastMsgID="0";

        }

        result = new ParseXml().convertUsersDetails(new Generic().getLatestFromWebService(MySqliteHelper.LastMsgID));


         for(int i=0;i<result.size();i++){
            String msgs =result.get(i).getMessages();
            String msgID = result.get(i).getMsgId();
                Log.v("messages", "------------>>>>"+msgs);
                Log.v("messageIDs", "--------->>>>>>"+msgID);
                msh.insert(msgID, msgs);
            }   

    }
*************************************
Intent intent = new Intent();
intent.setAction("com.example.updatelist");
sendBroadcast(intent); // finally broadcast
*************************************

}, 0, UPDATE_INTERVAL);

Log.v(getClass().getSimpleName(), "Timer started.");

}

谢谢你的回答,但迟到记录在最后一个位置添加。有没有机会在第一个位置添加迟到记录?谢谢你的回答,但迟到记录在最后一个位置添加。有没有机会在第一个位置添加迟到记录?