Home | History | Annotate | Download | only in demos
      1 /*
      2  * Copyright 2012 AndroidPlot.com
      3  *
      4  *    Licensed under the Apache License, Version 2.0 (the "License");
      5  *    you may not use this file except in compliance with the License.
      6  *    You may obtain a copy of the License at
      7  *
      8  *        http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  *    Unless required by applicable law or agreed to in writing, software
     11  *    distributed under the License is distributed on an "AS IS" BASIS,
     12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  *    See the License for the specific language governing permissions and
     14  *    limitations under the License.
     15  */
     16 
     17 package com.androidplot.demos;
     18 
     19 import android.app.Activity;
     20 import android.content.Context;
     21 import android.graphics.Color;
     22 import android.os.Bundle;
     23 import android.view.LayoutInflater;
     24 import android.view.View;
     25 import android.view.ViewGroup;
     26 import android.widget.ArrayAdapter;
     27 import android.widget.ListView;
     28 import com.androidplot.Plot;
     29 import com.androidplot.xy.XYSeries;
     30 import com.androidplot.xy.LineAndPointFormatter;
     31 import com.androidplot.xy.SimpleXYSeries;
     32 import com.androidplot.xy.XYPlot;
     33 
     34 import java.util.ArrayList;
     35 import java.util.List;
     36 import java.util.Random;
     37 
     38 public class ListViewActivity extends Activity {
     39     private static final int NUM_PLOTS = 10;
     40     private static final int NUM_POINTS_PER_SERIES = 10;
     41     private static final int NUM_SERIES_PER_PLOT = 5;
     42     private ListView lv;
     43 
     44     public void onCreate(Bundle savedInstanceState) {
     45         super.onCreate(savedInstanceState);
     46         setContentView(R.layout.listview_example);
     47         lv = (ListView) findViewById(R.id.listView1);
     48         lv.setAdapter(new MyViewAdapter(getApplicationContext(), R.layout.listview_example_item, null));
     49     }
     50 
     51     class MyViewAdapter extends ArrayAdapter<View> {
     52         public MyViewAdapter(Context context, int resId, List<View> views) {
     53             super(context, resId, views);
     54         }
     55 
     56         @Override
     57         public int getCount() {
     58             return 5;
     59         }
     60 
     61         @Override
     62         public View getView(int pos, View convertView, ViewGroup parent) {
     63             LayoutInflater inf = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     64 
     65             View v = convertView;
     66             if (v == null) {
     67                 v = inf.inflate(R.layout.listview_example_item, parent, false);
     68             }
     69 
     70             Plot p = (XYPlot) v.findViewById(R.id.xyplot);
     71             Random generator = new Random();
     72 
     73             for (int k = 0; k < NUM_SERIES_PER_PLOT; k++) {
     74                 ArrayList<Number> nums = new ArrayList<Number>();
     75                 for (int j = 0; j < NUM_POINTS_PER_SERIES; j++) {
     76                     nums.add(generator.nextFloat());
     77                 }
     78 
     79                 double rl = Math.random();
     80                 double gl = Math.random();
     81                 double bl = Math.random();
     82 
     83                 double rp = Math.random();
     84                 double gp = Math.random();
     85                 double bp = Math.random();
     86 
     87                 XYSeries series = new SimpleXYSeries(nums, SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "S" + k);
     88                 p.addSeries(series, new LineAndPointFormatter(
     89                         Color.rgb(new Double(rl * 255).intValue(), new Double(gl * 255).intValue(), new Double(bl * 255).intValue()),
     90                         Color.rgb(new Double(rp * 255).intValue(), new Double(gp * 255).intValue(), new Double(bp * 255).intValue()),
     91                         null, null));
     92             }
     93             return v;
     94         }
     95     }
     96 }