2017-12-15 10 views
0

明らかな理由もなく立ち往生し、アプリが は、私はここのコードを囲む Androidアプリは、私はそれを表示しようとすると、ロジックなしで、私は費用のグラフを作っ

を立ち往生し、エラーを取得していませんグラフとデータベース。もっとコードが必要ですか?

これは私がMPandroidChartライブラリに

EDITを使用したグラフである:あなたは私を助け、私が間違っていたところを教えていただけますか? 先月のすべての経費でグラフを作成しようとしています...何らかの理由で私は何も印刷されないという問題に直面しました!私は改訂コードといくつかの説明

// 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; 
} 

と、この私のデータベース

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; 
} 

ここでのメッセージは、グラフライブラリへのリンクが link

で編集した私はVideo Explainを説明するために、このビデオを使用しました

+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

thanx !!!私を助けて、どこが間違っているか教えてください。先月のすべての経費でグラフを作成しようとしています...何らかの理由で私は何も印刷されないという問題に直面しました!改訂されたコードといくつかの説明でメッセージを編集しました –

+0

印刷するものがない場合、つまり抽出されたカーソルが空の場合は、何も印刷されないことがあります。もしそうなら、あなたのループテストは 'while(0 <0)'と等しくなります(つまりxは最初は0です。そしてカーソルが空であればカウントは0になります)。 Log.dを追加することをお勧めします( "C COUNT"、 "抽出された行の数は" c = db.getCursorByDate(id); 'の後に" + Integer.toString(c.getCount());カーソルがデータを抽出していないので、理由を調べる必要があります.0より大きい場合、問題は他の場所にあります。これはデバッグ方法の例です。 – MikeT