从Firebase获取最后(5或10)值

时间:2017-09-27 05:28:52

标签: java android firebase firebase-realtime-database

我是10的新用户,我希望从messages获取最后FirebaseDatabaseChatInteractor.databaseReference.orderByKey().startAt(10).endAt(20).addListenerForSingleValueEvent((ValueEventListener) ActivityChatView.this); NullPointerException。我已尝试使用此功能。< / p>

Logcat
  • 但结果却给了我java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.Query com.google.firebase.database.DatabaseReference.orderByKey()' on a null object reference
  • DatabaseStructure
updating
  • 我的 DatabaseReference databaseReference= FirebaseDatabase.getInstance().getReferenceFromUrl("https://afdbjkaffirebaseio.com/"); Query query=databaseReference.child(Constants.ARG_CHAT_ROOMS).orderByChild("tKyhXXAf6TQnQgKyAf23RJcwhsn1_S5ClzMeWUEXR9evLJO9CBd9ABCH3").limitToLast(2); query.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { for(DataSnapshot snapshot:dataSnapshot.getChildren()){ Chat chat=snapshot.getValue(Chat.class); Log.e("163","ACV"+chat.fromEmail); } } @Override public void onCancelled(DatabaseError databaseError) { } }); 就是这个

enter image description here

  • Log
  • 之后
 E/163: ACVnull
  • XYTextAnnotation
  • 的结果
import java.awt.Color;
import java.awt.BasicStroke;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.*;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.Layer;
import org.jfree.ui.RefineryUtilities;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.ui.TextAnchor;

public class XYLineChart extends ApplicationFrame {

   public XYLineChart(String applicationTitle, String chartTitle) {
      super(applicationTitle);
      JFreeChart xylineChart = ChartFactory.createXYLineChart(
         chartTitle ,
         "Category" ,
         "Score" ,
         createDataset() ,
         PlotOrientation.VERTICAL ,
         false , true , false);

      ChartPanel chartPanel = new ChartPanel( xylineChart );
      chartPanel.setMouseWheelEnabled(true);
      chartPanel.setBackground(Color.WHITE);
      chartPanel.setPreferredSize( new java.awt.Dimension( 560 , 367 ) );
      final XYPlot plot = xylineChart.getXYPlot( );
      plot.setDomainPannable(true);
      plot.setRangePannable(true);
      plot.setBackgroundPaint(Color.WHITE);
      plot.setDomainGridlinePaint(Color.BLACK);
      plot.setRangeGridlinePaint(Color.BLACK);

//annotations
      XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer( );
      renderer.setSeriesPaint( 0 , Color.BLUE );
      renderer.setSeriesStroke( 0 , new BasicStroke( 1.0f ) );

      XYTextAnnotation a1 = new XYTextAnnotation("data",(double)9.1,9);
      a1.setTextAnchor(TextAnchor.CENTER_LEFT);
      a1.setPaint(Color.BLUE);
      renderer.addAnnotation(a1, Layer.BACKGROUND);

      XYTextAnnotation a2 = new XYTextAnnotation("data",(double)28.2,60);
      a2.setTextAnchor(TextAnchor.CENTER_LEFT);
      a2.setPaint(Color.BLUE);
      renderer.addAnnotation(a2, Layer.BACKGROUND);

      plot.setRenderer(renderer);
      setContentPane( chartPanel ); 
   }

   private XYDataset createDataset( ) {
      final XYSeriesCollection dataset = new XYSeriesCollection( );
      final XYSeries data1 = new XYSeries( "Nothing" );
      data1.add(8,9);
      data1.add(9,9);
      data1.add(7,9);
      dataset.addSeries(data1);
      final XYSeries data2 = new XYSeries( "Nothing1" );
      data2.add(20,60);
      data2.add(28,60);
      dataset.addSeries(data2);
      return dataset;
   }

   public static void main( String[ ] args ) {
      XYLineChart chart = new XYLineChart("",
         "");
      chart.pack();
      RefineryUtilities.centerFrameOnScreen( chart );          
      chart.setVisible( true ); 
   }
}

3 个答案:

答案 0 :(得分:4)

DatabaseReference messagesReference = FirebaseDatabase.getInstance().getReference().child("chat_rooms").child("tKyhXXAf6TQnQgKyAf23RJcwhsn1_S5ClzMeWUEXR9evLJO9CBd9ABCH3");  
 Query chatQuery = messagesReference.orderByChild("your key").limitToLast(5);
     chatQuery.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    if (dataSnapshot.getValue() != null) {
    //do what ever required 
    }

希望这会有所帮助。请试试。

答案 1 :(得分:4)

更新您的代码,如下所示:

 DatabaseReference databaseReference= FirebaseDatabase.getInstance().getReference();
    Query query=databaseReference.child(Constants.ARG_CHAT_ROOMS).child("tKyhXXAf6TQnQgKyAf23RJcwhsn1_S5ClzMeWUEXR9evLJO9CBd9ABCH3").orderByChild().limitToLast("Number of last messages you want to show");
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            for(DataSnapshot snapshot:dataSnapshot.getChildren()){
                Chat chat=snapshot.getValue(Chat.class);
                Log.e("163","ACV"+chat.fromEmail);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

答案 2 :(得分:0)

我认为你应该使用Query,这可能是

    @Override
public Query getQuery(DatabaseReference databaseReference) {
    // [START recent_posts_query]
    // Last 1000 posts, these are automatically the 1000 most recent
    // due to sorting by push() keys
    Query recentPostsQuery = databaseReference.child("posts")
            .limitToLast(number of post you want);
    // [END recent_posts_query]

    return recentPostsQuery;
}

这会给你的帖子限制你给出的号码。 您可以从 Firebase官方文档或其 github repo 获取更多信息 Firebase link of github of fragment repo

希望这会有所帮助.......