-1

it seams that I can not find the solution on how to obtain the data from each column and insert it to specific ArrayList of Integers.

I have established the database happinessDb which stores integers which I would than use for graphical projection in different activity (Chart). Activity Chart makes the problems since I dont know how to obtain the integers and store it to arrayList for further manipulation.

I would really appreciate your help regarding this issue.

thank you in advance

Database:

public class happinessDb {

public static final String KEY_ROWID = "_id";
public static final String KEY_DATE = "date";
public static final String KEY_TIME = "time";
public static final String KEY_HAPPY = "happy";
public static final String KEY_NORMAL = "normal";
public static final String KEY_SAD = "sad";

public static final String DATABASE_NAME = "happinesMeter";
public static final String DATABASE_TABLE = "practiceReport";
public static final int DATABASE_VERSION = 2;

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;



private static class DbHelper extends SQLiteOpenHelper{

    public DbHelper (Context context){
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                        KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                        KEY_DATE + " TEXT NOT NULL, " +
                        KEY_TIME + " TEXT NOT NULL, " +
                        KEY_HAPPY + " TEXT NOT NULL, " +
                        KEY_NORMAL + " TEXT NOT NULL, " +
                        KEY_SAD + " TEXT NOT NULL);"
        );

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        onCreate(db);
    }
}

    public happinessDb(Context c){
        ourContext = c;
}
public happinessDb open() throws SQLException{
    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}
public void close(){
    ourHelper.close();
}

public long createEntry(String date, String time, String happy, String normal, String sad) {
    ContentValues cv = new ContentValues();
    cv.put(KEY_DATE, date);
    cv.put (KEY_TIME, time);
    cv.put(KEY_HAPPY, happy);
    cv.put(KEY_NORMAL, normal);
    cv.put(KEY_SAD, sad);
    return ourDatabase.insert(DATABASE_TABLE, null, cv); //Insert all puts to table

}
}

public class Chart extends Activity {

ArrayList<Integer> stateList = new ArrayList<Integer>();
ArrayList<Integer> normalList = new ArrayList<Integer>();
ArrayList<Integer> sadList = new ArrayList<Integer>();

//Chart creation
private GraphicalView mChart;
private XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();
private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
private XYSeries mCurrentSeries;
private XYSeriesRenderer mHappyRenderer;

private XYSeries mNormalSeries;
private XYSeriesRenderer mNormalRenderer;
private XYSeries mSadSeries;
private XYSeriesRenderer mSadRenderer;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chart);
    // readHappyLog();






}



//TODO - Obtain the info from the database for graph plotting
private void readHappinessDb() {

    /**
     * Create the functions which obtains the data from the database
     * and populate the stateList, normalList and sadList
     *
     */

}


    //Create a method for the aChart engine
private void initChart() {

    /**
     * Add all series to mDataset
     */

    //Happy kids series
    mCurrentSeries = new XYSeries("Amount of happy kids"); //Creating Happy kids series
    mDataset.addSeries(mCurrentSeries); //Adding data to Happy kids series

    //Renderer for happy kids and line characteristics
    mHappyRenderer = new XYSeriesRenderer();
    mRenderer.addSeriesRenderer(mHappyRenderer);
    mHappyRenderer.setColor(Color.GREEN);
    mHappyRenderer.setLineWidth(10f);

    //Normal kids series
    mNormalSeries = new XYSeries("Amount of normal kids");
    mDataset.addSeries(mNormalSeries);

    //Renderer for normal Kids and line characteristics
    mNormalRenderer = new XYSeriesRenderer();
    mRenderer.addSeriesRenderer(mNormalRenderer);
    mNormalRenderer.setColor(Color.BLUE);
    mNormalRenderer.setLineWidth(10f);

    //Sad kids series
    mSadSeries = new XYSeries("Amount of sad kids");
    mDataset.addSeries(mSadSeries);

    //Renderer for sad kids and line characteristics
    mSadRenderer = new XYSeriesRenderer();
    mRenderer.addSeriesRenderer(mSadRenderer);
    mSadRenderer.setColor(Color.RED);
    mSadRenderer.setLineWidth(10f);


    //Applying renderer style
    mRenderer.setAxisTitleTextSize(20);
    mRenderer.setYLabelsPadding(20);
    mRenderer.setXLabelsPadding(20);
    mRenderer.setXTitle(" Date ");
    mRenderer.setYTitle(" Number of kids ");
    mRenderer.setApplyBackgroundColor(true);
    mRenderer.setBackgroundColor(Color.WHITE);
    mRenderer.setMarginsColor(Color.BLACK);
    mRenderer.setShowGrid(true);
    mRenderer.setAxesColor(Color.MAGENTA);
    mRenderer.setGridColor(Color.MAGENTA);
    mRenderer.setShowCustomTextGrid(true);
}

//Add x and y data to series from the array list

private void addHappyData() {

    Integer x = 20;
    for (Integer happy : stateList) {
        mCurrentSeries.add(x += 20, happy);
    }
}

private void addNormalData() {
    Integer x = 20;
    for (Integer normal : normalList) {
        mNormalSeries.add(x += 20, normal);
    }
}

private void addSadData() {
    Integer x = 20;
    for (Integer sad : sadList) {
        mSadSeries.add(x += 20, sad);
    }
}

// Draw the graph
@Override
protected void onResume() {
    super.onResume();
    LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
    if (mChart == null) {
        initChart();
        addHappyData();
        addNormalData();
        addSadData();
        mChart = ChartFactory.getCubeLineChartView(this, mDataset, mRenderer, 0.3f);
        layout.addView(mChart);
    } else {
        mChart.repaint();
    }
}

}

Found solution for obtaining the ArrayList of Integers and using it in different class with mChartEngine:

 public ArrayList<Integer> getDataForGraph() {
    ArrayList<Integer> happyValues = new ArrayList<Integer>();
    String[] columns = new String[]{KEY_ROWID, KEY_DATE, KEY_TIME,KEY_HAPPY, KEY_NORMAL, KEY_SAD };
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null); //Read the data from columns with cursor
    int result = 0 ;
    int iHappy = c.getColumnIndex(KEY_HAPPY);

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
        result =Integer.parseInt(c.getString(iHappy));
        happyValues.add(result);
    }

    return happyValues;
}

Chart class:

  public void happyDataFromDb() {
    happinessDb happyInfo = new happinessDb(this);
    try {
        happyInfo.open();
    } catch (SQLException e) {
        e.printStackTrace();
    }

    ArrayList<Integer> data = happyInfo.getDataForGraph();
    //myNum = Integer.parseInt(data);
    happyInfo.close();
    happyList = data;
}
Lovro Bajc
  • 33
  • 1
  • 2
  • 10

1 Answers1

0
 ArrayList<Integer> ints= new ArrayList<Integer>();  
//your query will returns cursor
if (cursor.moveToFirst()) {
  do {
    ints.add(get you integer from cursor);
  } while (cursor.moveToNext());
}
praveen
  • 420
  • 4
  • 18