2016-08-17 61 views
1

我想通過使用從firebase返回的數據來實現listView小部件。Android小部件列表視圖顯示每個項目的相同內容

但是對於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() { 
} 

}

請幫幫忙!

在此先感謝。

+0

的可能的複製[Android的列表視圖添加相同的項目](http://stackoverflow.com/questions/16564986/android-listview-adds-the-same-item) – 0X0nosugar

回答

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); 
     } 
    } 
相關問題