我正在尝试使用整数数组的自定义加载器在后台有一个服务来向数组添加值。当服务将条目添加到arraylist时。我正在尝试刷新UI列表视图的刷新。使用广播接收器作为内容观察者。
问题是调用了deliverResult,但它没有使用新值刷新UI。
Listview的主要活动实现了Loadercallbacks。非常感谢大家..
package loaders.com.myapplication;
import android.content.AsyncTaskLoader;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.Loader;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v4.content.LocalBroadcastManager;
import android.widget.Toast;
import java.util.List;
import java.util.ArrayList;
import java.util.Random;
/**
* Created by k.sekar on 1/7/2015.
*/
class testLoader extends AsyncTaskLoader<List<Integer>>{
public static List<Integer> intlist = new ArrayList<Integer>();
private testService mService;
public final String BROADCAST_ADDED = "ADDED";
private Context mContext;
private Receiver mReceiver;
public testLoader(Context context)
{
super(context);
Intent serviceMedia = new Intent(getContext(), testService.class);
getContext().startService(serviceMedia);
if(intlist == null)
{
intlist = new ArrayList<Integer>();
}
intlist.add(10);
intlist.add(20);
intlist.add(30);
intlist.add(40);
if(mReceiver == null)
{
mReceiver = new Receiver(this);
}
}
public class Receiver extends BroadcastReceiver{
final testLoader mLoader;
public Receiver(testLoader loader)
{
mLoader = loader;
}
@Override
public void onReceive(Context context, Intent intent) {
String message = intent.getAction();
if(message.compareTo("ADDED") == 0)
{
mLoader.onContentChanged();
}
}
}
@Override
public List<Integer> loadInBackground() {
// ArrayList<Integer> list = new ArrayList<Integer>();
IntentFilter filter = new IntentFilter(BROADCAST_ADDED);
LocalBroadcastManager.getInstance(getContext()).registerReceiver(mReceiver,filter);
// LocalBroadcastManager.getInstance(mLoader.getContext()).registerReceiver(this, filter);
return intlist;
}
@Override
protected void onStartLoading() {
if(intlist != null)
{
deliverResult(intlist);
}
if(mRefreshData == true)
{
mRefreshData = false;
forceLoad();
}
}
@Override
protected void onStopLoading()
{
cancelLoad();
}
@Override
protected void onReset()
{
super.onReset();
onStopLoading();
intlist = null;
if(intlist != null)
{
intlist = null;
}
if(mReceiver != null)
{
LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(mReceiver);
mReceiver = null;
}
}
@Override
public void cancelLoadInBackground(){
super.cancelLoadInBackground();
}
@Override
public void onCanceled(List<Integer> data)
{
// mContext.unregisterReceiver(mReceiver);
super.onCanceled(data);
data = null;
}
@Override
public void deliverResult(List<Integer> data)
{
if(isReset())
{
data = null;
return;
}
List<Integer> oldData = intlist;
intlist = data;
if (isStarted()) {
super.deliverResult(data);
}
if(oldData != null && oldData != intlist)
{
oldData = null;
}
}
}
活动的代码段
import android.app.LoaderManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.Loader;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends ActionBarActivity implements LoaderManager.LoaderCallbacks<List<Integer>> {
ListView mListView;
ArrayAdapter<Integer> mAdapter;
public static final String BROADCAST_REFRESH = "REFRESH";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView)this.findViewById(R.id.listnumbers);
mListView.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
mListView.setAdapter(mAdapter);
getLoaderManager().initLoader(1, null, this);
}
protected void onStop()
{
super.onStop();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public Loader<List<Integer>> onCreateLoader(int id, Bundle args) {
if(id == 1)
{
return new IntLoader(getApplicationContext());
}
return null;
}
@Override
public void onLoadFinished(Loader<List<Integer>> loader, List<Integer> data) {
if (mAdapter == null)
{
mAdapter = new ArrayAdapter<Integer>(this,R.layout.list_element, data);
mListView.setAdapter(mAdapter);
}
}
@Override
public void onLoaderReset(Loader<List<Integer>> loader) {
mListView.setAdapter(null);
}
}
答案 0 :(得分:0)
在活动课程中,试试这个。
@Override
public void onLoaderReset(Loader<List<Integer>> loader) {
mListView.notifyDataSetChanged();
}
答案 1 :(得分:0)
getLoaderManager().restartLoader(CATEGORY_LOADER, null, yourActivity.this);
这对我有用。这里的CATEGORY_LOADER
具有整数值1
,而null
是多余的参数。