2017-12-15 17 views
0

특별한 이유없이 내동댕이 앱은 나는 여기의 코드를 둘러싸 안드로이드 응용 프로그램은 내가 보여하려고 할 때 로직없이 내가 비용 그래프를 만든

을 내동댕이 그리고 오류가 발생하지 않습니다 그래프와 데이터베이스. 더 많은 코드가 필요합니까?

이 내가 MPandroidChart 라이브러리

EDIT에서 사용하는 그래프이다 : 당신이 나를 도와 내가 잘못 어디에 있는지 말해 주시겠습니까? 나는 지난 달의 모든 지출로 그래프를 만들려고 노력하고있다. 그러나 어떤 이유로 나는 나에게 아무 것도 프린트하지 않는다는 문제를 발견했다. 나는

다음
public class DatabaseHandler extends SQLiteOpenHelper { 

// All Static variables 
// Database Version 
private static final int DATABASE_VERSION = 11; 

// Database Name 
public static final String DATABASE_NAME = "Records_Item Purcashes"; 

// Contacts table name 
public static final String TABLE_RECORDS = "Records"; 

// Contacts Table Columns names 
public static final String KEY_ID = "_id"; 
public static final String KEY_PRICE = "Price"; 
public static final String KEY_ITEM = "Item"; 
public static final String KEY_DETAILS = "Details"; 
public static final String KEY_DATE = "DateAndTime"; 

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

// Creating Tables 
@Override 
public void onCreate(SQLiteDatabase db) { 
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + 
      TABLE_RECORDS + 
      "(" + 
      KEY_ID + " INTEGER PRIMARY KEY," + 
      KEY_PRICE + " INTEGER," + 
      KEY_ITEM + " TEXT," + 
      KEY_DETAILS + " TEXT, " + 
      KEY_DATE + " TEXT" + 
      ")"; 
    db.execSQL(CREATE_CONTACTS_TABLE); 
} 

// Upgrading database 
@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    // Drop older table if existed 
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_RECORDS); 

    // Create tables again 
    onCreate(db); 
} 

public void insertRecord(int price, String item, String details, String date) { 
    ContentValues cv = new ContentValues(); 
    cv.put(KEY_PRICE, price); 
    cv.put(KEY_ITEM, item); 
    cv.put(KEY_DETAILS, details); 
    cv.put(KEY_DATE, date); 
    SQLiteDatabase db = this.getWritableDatabase(); 
    db.insert(TABLE_RECORDS, null, cv); 
} 

public Cursor getAllRecords() { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     return db.query(TABLE_RECORDS, null, null, null, null, null, null); 
    } 
public Cursor get1Record(String[] key) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    return db.query(TABLE_RECORDS, key, null, null, null, null, null); 
} 
// Adding new contact 
public void addRecord(Record record) { 
    SQLiteDatabase db = this.getWritableDatabase(); 

    ContentValues values = new ContentValues(); 

    values.put(KEY_ID, record.getId()); // Contact Name 
    values.put(KEY_PRICE, record.getPrice()); // Contact Name 
    values.put(KEY_ITEM, record.getItem()); // Contact Name 
    values.put(KEY_DETAILS, record.getDetails()); // Contact Name 
    values.put(KEY_DATE, record.getDate()); // Contact Phone Number 

    // Inserting Row 
    db.insert(TABLE_RECORDS, null, values); 
    db.close(); // Closing database connection 
} 

// Getting single contact 
public Record getRecord(int id) { 
    SQLiteDatabase db = this.getReadableDatabase(); 

    Cursor cursor = db.query(TABLE_RECORDS, new String[]{KEY_ID, KEY_PRICE, 
        KEY_ITEM, KEY_DETAILS, KEY_DATE}, KEY_ID + "=?", 
      new String[]{String.valueOf(id)}, null, null, null, null); 
    if (cursor != null) 
     cursor.moveToFirst(); 

    Record record = new Record(Integer.parseInt(cursor.getString(0)), 
      Integer.parseInt(cursor.getString(1)), cursor.getString(2), cursor.getString(3), cursor.getString(4)); 
    // return contact 
    return record; 
} 

// Getting All Contacts 
public List<Record> getAllContacts() { 
    List<Record> contactList = new ArrayList<Record>(); 
    // Select All Query 
    String selectQuery = "SELECT * FROM " + TABLE_RECORDS; 

    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor cursor = db.rawQuery(selectQuery, null); 

    // looping through all rows and adding to list 
    if (cursor.moveToFirst()) { 
     do { 
      Record record = new Record(); 
      record.setId(Integer.parseInt(cursor.getString(0))); 
      record.setPrice(Integer.parseInt(cursor.getString(1))); 
      record.setItem(cursor.getString(2)); 
      record.setDetails(cursor.getString(3)); 
      record.setDate(cursor.getString(4)); 

      // Adding contact to list 
      contactList.add(record); 
     } while (cursor.moveToNext()); 
    } 
    return contactList; 
} 

// Getting contacts Count 
public int getRecordsCount() { 
    String countQuery = "SELECT * FROM " + TABLE_RECORDS; 
    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor cursor = db.rawQuery(countQuery, null); 
    // cursor.close(); 

    // return count 
    return cursor.getCount(); 
} 

// Updating single contact 
public int updateContact(Record record) { 
    SQLiteDatabase db = this.getWritableDatabase(); 

    ContentValues values = new ContentValues(); 
    values.put(KEY_ID, record.getId()); 
    values.put(KEY_PRICE, record.getPrice()); 
    values.put(KEY_ITEM, record.getItem()); 
    values.put(KEY_DETAILS, record.getDetails()); 
    values.put(KEY_DATE, record.getDate()); 

    // updating row 
    return db.update(TABLE_RECORDS, values, KEY_ID + " = ?", 
      new String[]{String.valueOf(record.getId())}); 
} 

// Deleting single contact 
public void deleteContact(Record record) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    db.delete(TABLE_RECORDS, KEY_ID + " = ?", new String[] 
      {String.valueOf(record.getId())}); 
    db.close(); 
} 
     // Deleting some Records 
public boolean deleteRecord(long id) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    return (db.delete(TABLE_RECORDS,KEY_ID + "=?",new String[] 
    {Long.toString(id)})> 0); 
} 

    public String[] getAllCountries(String KEY_PRICE) { 
     SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor cursor = db.query(TABLE_RECORDS, null, null, null, null, null, 
    null); 

    if (cursor.getCount() > 0) { 
     String[] str = new String[cursor.getCount()]; 
     int i = 0; 

     while (cursor.moveToNext()) { 
      str[i] = cursor.getString(cursor.getColumnIndex(KEY_PRICE)); 
      i++; 
     } 
     return str; 
    } else { 
     return new String[]{}; 
    } 
} 

public Cursor getCursor(int id) { 

    SQLiteDatabase db = this.getReadableDatabase(); 
    String from[] = {KEY_PRICE};//this is the edit1 
    String where = KEY_ID+"=?";//this is the edit2 
    String[] whereArgs = new String[]{String.valueOf(id)+""}; //this is the edit3 
    Cursor cursor = db.query(true, TABLE_RECORDS, from, where, whereArgs, null, null, null, null); 
    return cursor; 
} 

public Cursor getCursorByItem(String id) { 

    SQLiteDatabase db = this.getReadableDatabase(); 
    String Get = "select Price from Records where item = '"+id+"'"; 
    Cursor cursor = db.rawQuery(Get ,null); 
    return cursor; 
} 

public Cursor getCursorByDate(String id) { 

    SQLiteDatabase db = this.getReadableDatabase(); 
    String Get = "SELECT price FROM Records WHERE " +KEY_DATE +" LIKE '%"+id+"%'"; 
    Cursor cursor = db.rawQuery(Get ,null); 
    return cursor; 
} 

public int getCountByItem(String id) { 

    SQLiteDatabase db = this.getReadableDatabase(); 
    String Get = "select count(*) from Records where item = '"+id+"'"; 
    Cursor cursor = db.rawQuery(Get ,null); 
    int price = cursor.getCount(); 
    return price; 
} 

내가 Video Explain을 설명하는 동영상을 사용하는 그래프 라이브러리 link

에 대한 링크입니다 수정 된 코드를 사용하여 메시지를 일부 설명

// get all the dates(days in this month with month) like DD.MM 
public double[] getDate() { 

    int month=Calendar.getInstance().get(Calendar.MONTH); 
    int day=Calendar.getInstance().get(Calendar.DAY_OF_MONTH); 

    double[] f; 
    f= new double[day+1]; 

    while(day>=0) 
    { 
     String date = String.valueOf(month) +"."+ String.valueOf(day); 
     f[day]=Double.parseDouble(date); 
     day--; 
    } 
    return f; 
} 

// get Price by specific day with sum 
public int getPriceDate(String id) { 

    DatabaseHandler db = new DatabaseHandler(this); 
    Cursor c; 
    c = db.getCursorByDate(id); 


     int count = getCountItem(id); 
     int x = 0; 
     int price11 = 0; 
    int price1[] = new int[count]; 

      while (x < count) { 


      price1[x] = -1; 
      x++; 
     } 

     x = 0; 

    // get sum of all the day 
     while (x < count) { 
      if (c != null) { 
       while (c.moveToNext()) { 
        //assuming price is an integer 
        price1[x] = c.getInt(0);//edit 4 

        price11 = price11 + price1[x]; 

       } 

      } 
      x++; 
     } 

    return price11; 

} 

// chart/ graph Daily bar chart 
public void DailyChart() { 


    mChart = (BarChart) findViewById(R.id.chart1); 
    mChart.getDescription().setEnabled(false); 
    setData(getDate().length); 
    mChart.setFitBars(true); 
} 

// insert data to Daily Chart 
private void setData(int count) { 
    ArrayList<BarEntry> yVals = new ArrayList<>(); 


    for (int i = 0; i < count; i++) { 

     yVals.add(new BarEntry(i, getPriceDate(String.valueOf(getDate()[i])))); 

     yVals.add(new BarEntry(i, i)); 

    } 

    BarDataSet set = new BarDataSet(yVals, "Data set"); 
    set.setColors(ColorTemplate.MATERIAL_COLORS); 
    set.setDrawValues(true); 

    BarData data = new BarData(set); 

    mChart.setData(data); 
    mChart.invalidate(); 
    mChart.animateY(500); 

} 


// Get count of Specific items 
public int getCountItem(String id) { 
    DatabaseHandler db = new DatabaseHandler(this); 
    int price = db.getCountByItem(id); 
    return price; 
} 

이 내 데이터베이스를 편집

+0

문제를보고 다른 사람이 될 수있는 바와 같이,이 문제를 해결 코드를 사용하여 질문을 수정하지 마십시오 혼란스러워서 문제가없는 것처럼 보입니다. 질문과 관련하여 더 많은 정보가 있으면 질문을 편집하고 편집에 대한 설명을 포함시켜야합니다. 그러나 문제가 해결되고 후속 문제에 직면하면 새로운 질문을해야합니다. – MikeT

답변

1

getPriceDate 방법에서 x를 증가시키는 것으로 보입니다. e는 증가하므로 x는 증가하지 않습니다.

현재 가지고있다 -에

while (x < count) { 
     if (c != null) { 
      while (c.moveToNext()) { 
       //assuming price is an integer 
       price1[x] = c.getInt(0);//edit 4 

       price11 = price11 + price1[x]; 
       // use these strings as you want 
      } 
     } 
    } 
    x++; //<<<<<< not in the while loop so won't be actioned. 

변경을 : -

while (x < count) { 
     if (c != null) { 
      while (c.moveToNext()) { 
       //assuming price is an integer 
       price1[x] = c.getInt(0);//edit 4 

       price11 = price11 + price1[x]; 
       // use these strings as you want 
      } 
     } 
     x++; //<<<<<< Moved to inside the loop 
    } 
+0

고맙습니다 !!! 도와 주시겠습니까? 그리고 내가 잘못한 곳을 말해 주시겠습니까? 나는 지난 달의 모든 지출로 그래프를 만들려고 노력하고 있습니다 ...하지만 어떤 이유로 나는 그것이 나에게 아무 것도 프린트하지 않는 문제를 보았습니다! 수정 된 코드와 몇 가지 설명으로 메시지를 편집했습니다. –

+0

추출 된 커서가 비어있는 경우 아무 것도 인쇄되지 않으면 아무 것도 인쇄되지 않을 수 있습니다. 그렇다면 루프 테스트는 'while (0 <0)'과 같습니다. 즉, x는 처음에는 0이고 커서가 비어 있으면 카운트는 0이됩니다. Log.d를 추가하는 것이 좋습니다 ("C COUNT", "추출 된 열의 수는"c = db.getCursorByDate (id); "뒤에 Integer.toString (c.getCount()); 커서가 어떤 데이터도 추출하지 못하기 때문에 이유를 찾아야합니다 .0보다 크면 문제는 다른 곳에서 발생합니다. 디버깅 방법의 예입니다. – MikeT