Код: Выделить всё
public class OldworksActivity extends Activity {
DBHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_old_works);
// достаем из таблицы все поля группируем по дате выводим группами
Log.d("Oncreate","dbHelper = new DBHelper(this)");
dbHelper = new DBHelper(this);
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor c = db.query("workList", null, null, null, null, null, null);
Log.d("Oncreate","db.query");
int rowscount = c.getCount();
String[] Dates = new String [rowscount];
Long [] datelong = new Long[rowscount];
String[] PrintDate = new String[rowscount];
long[] rawdatelong = new long[rowscount];
// ставим позицию курсора на первую строку выборки
// если в выборке нет строк, вернется false
if (c.moveToFirst()) {
// определяем номера столбцов по имени в выборке
int idColIndex = c.getColumnIndex("id");
int DateColIndex = c.getColumnIndex("date");
int i = 0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd");
long date = 0;
//
do {
Log.d("Oncreate","do");
date = c.getLong(DateColIndex);
rawdatelong[i]=date;
Log.d("Oncreate",String.valueOf(rawdatelong[i]));
Date initialDate = null;
try {
initialDate = sdf.parse(String.valueOf(date));
} catch (Exception e) {
//do anything you want to handle the exception
// println("Unable to parse date stamp");
Log.d("exception", e.getMessage());
}
Date dat =initialDate;
Log.d("Oncreate","do1");
Dates[i] = sdf.format(dat);
Log.d("Oncreate","do2");
datelong[i] =Long.valueOf(Dates[i]);
Log.d("Oncreate",String.valueOf(datelong[i]));
Log.d("Oncreate","do3");
PrintDate[i]= sdf2.format(dat);
Log.d("Oncreate","do4");
i++;
Log.d("Oncreate","do5");
//Log.d("Oncreate",Dates[i]);
// переход на следующую строку
// а если следующей нет (текущая - последняя), то false - выходим из цикла
} while (c.moveToNext());
} else c.close();
// сортируем даты от большего
Log.d("Oncreate","prefor");
for(int i=0;i<rowscount;i++)
{
Log.d("Oncreate","for");
for (int j=0;j<(rowscount-1);j++)
{
if (datelong[j]<datelong[j+1]){
long tmp = datelong[j];
datelong[j]=datelong[j+1];
datelong[j+1] = tmp;
String temp2 = PrintDate[j];
PrintDate[j] = PrintDate[j+1];
PrintDate[j+1] = temp2;
Log.d("Oncreate","for");
long temp3 = rawdatelong[j];
rawdatelong[j] = rawdatelong[j+1];
rawdatelong[j+1] = temp3;
};
}
};
LinearLayout linLayoutworks = (LinearLayout) findViewById(R.id.linLayoutworks);
LayoutInflater ltInflater = getLayoutInflater();
for(int i=0;i<rowscount;i++)
{
//if (i==(rowscount-1)) if (datelong[i]==datelong[i+1]) continue;
View item = ltInflater.inflate(R.layout.blackdaterow, linLayoutworks, false);
TextView datetext = (TextView) item.findViewById(R.id.datetext);
datetext.setText(PrintDate[i]);
linLayoutworks.addView(item);
Cursor c2 = db.query("workList", null, "date=" +rawdatelong[i], null, null, null, null);
Log.d("second for","second querry");
// ставим позицию курсора на первую строку выборки
// если в выборке нет строк, вернется false
if (c2.moveToFirst()) {
// определяем номера столбцов по имени в выборке
int idColIndex = c2.getColumnIndex("id");
int DateColIndex = c2.getColumnIndex("date");
int workColIndex = c2.getColumnIndex("work");
int ConsumptionColIndex = c2.getColumnIndex("consumption");
int masteridColIndex = c2.getColumnIndex("masterid");
int masterNameColIndex = c2.getColumnIndex("mastername");
int HouseNameColIndex = c2.getColumnIndex("housename");
//
LayoutInflater ltInflater2 = getLayoutInflater();
do {
//long date = c2.getLong(DateColIndex);
long work = c2.getLong(workColIndex);
long consumption = c2.getLong(ConsumptionColIndex);
String mastername = c2.getString(masterNameColIndex);
String housename = c2.getString(HouseNameColIndex);
Log.d("second for","get");
View item2 = ltInflater2.inflate(R.layout.work, linLayoutworks, false);
TextView worktext = (TextView) item.findViewById(R.id.worktext);
worktext.setText("work: "+String.valueOf(work));
Log.d("second for","get2");
TextView consumptiontext = (TextView) item.findViewById(R.id.consumptiontext);
consumptiontext.setText("Consumption: "+String.valueOf(consumption));
TextView mastertext = (TextView) item.findViewById(R.id.mastertext);
mastertext.setText("master: "+mastername);
TextView housetext = (TextView) item.findViewById(R.id.housetext);
housetext.setText("House: "+housename);
linLayoutworks.addView(item2);
// переход на следующую строку
// а если следующей нет (текущая - последняя), то false - выходим из цикла
} while (c2.moveToNext());
} else c2.close();
}
}
Nullpointerexception... где ошибка?