Android小部件列表视图,显示每个项目的相同内容

时间:2016-08-17 20:32:51

标签: android firebase android-widget firebase-realtime-database

我正在尝试使用从firebase返回的数据来实现listView小部件。

但对于listview中的每个项目,我只看到来自firebase的第一个数据行,我无法找出原因。

我试过调试我在

中获得了独特的数据
  

datasnapshot

我的代码是

Widgetservice.java

public class WidgetService extends RemoteViewsService {


@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
    int appWidgetId = intent.getIntExtra(
            AppWidgetManager.EXTRA_APPWIDGET_ID,
            AppWidgetManager.INVALID_APPWIDGET_ID);

    return (new ListProvider(this.getApplicationContext(), intent));
}

}

WidgetProvider.java

public class WidgetProvider extends AppWidgetProvider {

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                     int[] appWidgetIds) {
    final int N = appWidgetIds.length;
    /*int[] appWidgetIds holds ids of multiple instance of your widget
     * meaning you are placing more than one widgets on your homescreen*/
    for (int i = 0; i < N; ++i) {
        RemoteViews remoteViews = updateWidgetListView(context,
                appWidgetIds[i]);
        appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
    }
    super.onUpdate(context, appWidgetManager, appWidgetIds);
}

private RemoteViews updateWidgetListView(Context context, int appWidgetId) {

    //which layout to show on widget
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
            R.layout.widget_layout);

    //RemoteViews Service needed to provide adapter for ListView
    Intent svcIntent = new Intent(context, WidgetService.class);
    //passing app widget id to that RemoteViews Service
    svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    //setting a unique Uri to the intent
    //don't know its purpose to me right now
    svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
    //setting adapter to listview of the widget
    remoteViews.setRemoteAdapter(appWidgetId, R.id.listViewWidget,
            svcIntent);
    //setting an empty view in case of no data
    remoteViews.setEmptyView(R.id.listViewWidget, R.id.empty_view);
    return remoteViews;
}

}

ListProvider.java

public class ListProvider implements RemoteViewsFactory {
private ArrayList<ListItem> listItemList = new ArrayList<ListItem>();
private Context context = null;
private int appWidgetId;

private DatabaseReference mFirebaseDatabaseReference;
private FirebaseAuth mFirebaseAuth;
private FirebaseUser mFirebaseUser;

public ListProvider(Context context, Intent intent) {
    this.context = context;
    appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
            AppWidgetManager.INVALID_APPWIDGET_ID);


}

private void populateListItem() {


    Firebase ref = new Firebase("https://helpplusapp-318b8.firebaseio.com/HomeContent");
    mFirebaseAuth = FirebaseAuth.getInstance();
    mFirebaseUser = mFirebaseAuth.getCurrentUser();

    DatabaseReference mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("HomeContent");
    mDatabaseReference.addValueEventListener(new ValueEventListener() {
        ListItem listItem;
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            listItem = new ListItem();
            System.out.println("There are " + dataSnapshot.getChildrenCount() + " blog posts");
            for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
                HomeSampleContent post = postSnapshot.getValue(HomeSampleContent.class);


                listItem.setContent("Heading"+post.getHomeContent());
                listItem.setHeading("this is heading");
                listItemList.add(listItem);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

@Override
public int getCount() {
    return listItemList.size();
}

@Override
public long getItemId(int position) {
    return position;
}

/*
 *Similar to getView of Adapter where instead of View
 *we return RemoteViews
 *
 */
@Override
public RemoteViews getViewAt(int position) {
    final RemoteViews remoteView = new RemoteViews(
    context.getPackageName(), R.layout.list_row);
    ListItem listItem = listItemList.get(position);
    remoteView.setTextViewText(R.id.heading, listItem.getContent());
    remoteView.setTextViewText(R.id.content, listItem.getHeading());

    return remoteView;
}


@Override
public RemoteViews getLoadingView() {
    return null;
}

@Override
public int getViewTypeCount() {
    return 1;
}

@Override
public boolean hasStableIds() {
    return true;
}

@Override
public void onCreate() {
}

@Override
public void onDataSetChanged() {
    populateListItem();
}

@Override
public void onDestroy() {
}

}

请帮忙!

提前致谢。

1 个答案:

答案 0 :(得分:0)

listItem = new ListItem();应该在for循环中:

@Override
    public void onDataChange(DataSnapshot dataSnapshot) {

        System.out.println("There are " + dataSnapshot.getChildrenCount() + " blog posts");
        for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {listItem = new ListItem();
            HomeSampleContent post = postSnapshot.getValue(HomeSampleContent.class);


            listItem.setContent("Heading"+post.getHomeContent());
            listItem.setHeading("this is heading");
            listItemList.add(listItem);
        }
    }