Android应用程序卡在没有逻辑的情况下,没有明显的原因

问题描述 投票:0回答:1

当我尝试显示它并且应用程序卡住时,我制作了一个费用图表

并且没有得到错误,我在这里附上图表和数据库的代码。你需要更多代码吗?

这是我在MPandroidChart库中使用的图表

编辑:能帮帮我,告诉我哪里错了吗?我正在尝试使用上个月的所有费用创建一个图表...但由于某种原因,我遇到了一个问题,它不会向我打印任何内容!我用修改后的代码和一些解释编辑了这条消息

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

android database sqlite graph
1个回答
1
投票

getPriceDate方法中,您似乎在循环外增加x,因此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
    }
© www.soinside.com 2019 - 2024. All rights reserved.