Android 使用后台服务在listview中添加数据

Android 使用后台服务在listview中添加数据,android,listview,Android,Listview,创建一个应用程序,其中我在listview中显示提要,并将提要记录存储在sqlitedatabase中。现在我希望,如果有人添加新的提要,应该使用后台服务自动更新listview,并且每1分钟运行一次服务,并且应该将新值插入数据库并生成新添加提要的通知 我不知道如何添加服务以及如何在服务类中为listview设置适配器。我知道我必须为此添加意向服务。 我搜索了一下,但我不明白 我想要的:假设我在提要列表视图中有两个图像,有人又添加了一个图像,那么这个新添加的图像应该自动显示在我的提要中,而不刷新

创建一个应用程序,其中我在listview中显示提要,并将提要记录存储在sqlitedatabase中。现在我希望,如果有人添加新的提要,应该使用后台服务自动更新listview,并且每1分钟运行一次服务,并且应该将新值插入数据库并生成新添加提要的通知

我不知道如何添加服务以及如何在服务类中为listview设置适配器。我知道我必须为此添加意向服务。 我搜索了一下,但我不明白

我想要的:假设我在提要列表视图中有两个图像,有人又添加了一个图像,那么这个新添加的图像应该自动显示在我的提要中,而不刷新提要列表视图。为此,我必须运行后台服务,将在每1分钟运行,新添加的图像将显示


但我不知道如何添加服务,为此,如何在服务类中调用服务器,在服务类的listview中添加数据,在sqlitedatabase中添加记录,以及为新提要生成通知。这一切都将在服务类。扩展listview的baseadapter。

正如我所说,您需要一个ListAdapter来控制更改和异步内容

例如,类似这样的事情:

        // Get ListView object from xml
        listView = (ListView) findViewById(R.id.list);

        // Defined Array values to show in ListView
        String[] values = new String[] { "Android List View", 
                                         "Adapter implementation",
                                         "Simple List View In Android",
                                         "Create List View Android", 
                                         "Android Example", 
                                         "List View Source Code", 
                                         "List View Array Adapter", 
                                         "Android Example List View" 
                                        };

        // Define a new Adapter
        // First parameter - Context
        // Second parameter - Layout for the row
        // Third parameter - ID of the TextView to which the data is written
        // Forth - the Array of data

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
          android.R.layout.simple_list_item_1, android.R.id.text1, values);


        // Assign adapter to ListView
        listView.setAdapter(adapter);
    Cursor cursor = dbHelper.fetchAllCountries();

  // The desired columns to be bound
  String[] columns = new String[] {
    CountriesDbAdapter.KEY_CODE,
    CountriesDbAdapter.KEY_NAME,
    CountriesDbAdapter.KEY_CONTINENT,
    CountriesDbAdapter.KEY_REGION
  };

  // create the adapter using the cursor pointing to the desired data 
  //as well as the layout information
  dataAdapter = new SimpleCursorAdapter(
    this, R.layout.country_info, 
    cursor, 
    columns, 
    to,
    0);

  ListView listView = (ListView) findViewById(R.id.listView1);
  // Assign adapter to ListView
  listView.setAdapter(dataAdapter);

您可以使用BusEvent解决方案。当您的服务检测到更改时,您将发布带有更改的新事件。包含ListView的活动将侦听此事件并更新适配器

您可以使用类似的库,也可以使用带有RxAndroid和RxJava的新aproach

我认为最好的选择是otto,因为集成RxAndroid对于简单的通信来说太难了

奥托的解决方案可以是:

dependencies {
    compile 'com.squareup:otto:1.3.8'
}
BusProvider.java

import com.squareup.otto.Bus;
import com.squareup.otto.ThreadEnforcer;

public final class BusProvider {

    private static final Bus BUS = new Bus(ThreadEnforcer.ANY);

    private BusProvider() {
        // No instances.
    }

    public static Bus getInstance() {
        return BUS;
    }
}
ListDataEvent.java

public class ListDataEvent {
   List<YourObject> data; ////set your data
}
最后,服务:

public class YourService extends IntentService {
    @Override
    protected void onHandleIntent(Intent workIntent) {
        //Gets data and post the event
       BusProvider.getInstance().post(new ListDataEvent(data));
    }
}

您需要一个ListAdapter用于此目的是的,我已经这样做了,但我不知道如何在服务的listview中添加服务和数据您可以更具体一点吗?还可以创建一个内部类来处理您的服务。你也没有回答,为什么?我还为sqlite添加了解决方案。。。你需要一个游标适配器。我自己都明白,但问题是关于使用服务类刷新数据。
public class YourService extends IntentService {
    @Override
    protected void onHandleIntent(Intent workIntent) {
        //Gets data and post the event
       BusProvider.getInstance().post(new ListDataEvent(data));
    }
}