我一直在关注一本书的教程,我有一个列表,该列表将通过游标填充数据库数据。尽管列表没有显示任何内容,但是我已经做了所有这本书的全部讲完的事情,并且查看了无数相同主题的问题。
TimeTracker
public class TimeTracker extends AppCompatActivity {
TimeTrackerAdapter timeTrackerAdapter;
TimeTrackerDatabaseHelper databaseHelper;
ListView listView;
public static final int TIME_ENTRY_REQUEST_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.time_tracker_activity);
//Instantiating your custom open helper will cause the database to be created.
databaseHelper = new TimeTrackerDatabaseHelper(this);
listView = findViewById(R.id.times_list);
timeTrackerAdapter = new TimeTrackerAdapter(this, databaseHelper.getAllTimeRecordsList());
listView.setAdapter(timeTrackerAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.time_list_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem menu) {
if (menu.getItemId() == R.id.add_time_menu_item) {
Intent intent = new Intent(this, AddTimeActivity.class);
//startActivity(intent);
startActivityForResult(intent, TIME_ENTRY_REQUEST_CODE);
return true;
}
return super.onOptionsItemSelected(menu);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == TIME_ENTRY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
String notes = data.getStringExtra("notes");
String time = data.getStringExtra("time");
databaseHelper.saveTimeRecord(time, notes);
Cursor cursor = databaseHelper.getAllTimeRecordsList();
int count = 0;
if (cursor.moveToFirst()) {
do {
String time01 = cursor.getString(1);
String notes01 = cursor.getString(2);
Log.d("RESULT","1: Database Info: POS: " + count + ", Time: " + time01 + ", Note: " + notes01);
count++;
} while (cursor.moveToNext());
}
if (!cursor.isClosed()) {
cursor.close();
}
timeTrackerAdapter.changeCursor(cursor);
//listView = (ListView) findViewById(R.id.times_list);
listView.setAdapter( timeTrackerAdapter);
// timeTrackerAdapter.addTimeRecord( time, notes);
timeTrackerAdapter.notifyDataSetChanged();
Log.d("RESULT","1: End of method: onActivityResult, Time: " + time + ", Note: " + notes);
}
}
}
/** @Override
protected void onRestart() {
super.onRestart();
databaseHelper = new TimeTrackerDatabaseHelper(this);
Cursor cursor = databaseHelper.getAllTimeRecordsList();
listView = findViewById(R.id.times_list);
timeTrackerAdapter = (TimeTrackerAdapter) listView.getAdapter();
timeTrackerAdapter.changeCursor(cursor);
timeTrackerAdapter.notifyDataSetChanged();
}*/
}
TimeTrackerAdapter
public class TimeTrackerAdapter extends CursorAdapter {
private ArrayList<TimeRecord> times = new ArrayList<TimeRecord>();
TimeTrackerDatabaseHelper databaseHelper;
public TimeTrackerAdapter(Context context, Cursor cursor) {
super(context, cursor);
//Cursor cursor = databaseHelper.getAllTimeRecordsList();
/**if (cursor.moveToFirst()) {
do {
String time = cursor.getString(1);
String notes = cursor.getString(2);
addTimeRecord(time, notes);
} while (cursor.moveToNext());
}
if (!cursor.isClosed()) {
cursor.close();
}**/
Log.d("RESULT","5: End of method: TimeTrackerAdapter: TimeTrackerAdapter");
}
public void bindView(View view, Context context, Cursor cursor) {
TextView nameTextView = (TextView) view.findViewById(R.id.time_view);
nameTextView.setText(cursor.getString(cursor.getColumnIndex("time")));
TextView valueTextView = (TextView) view.findViewById(R.id.notes_view);
valueTextView.setText(cursor.getString(cursor.getColumnIndex("notes")));
Log.d("RESULT","4: End of method: TimeTrackerAdapter: bindView");
}
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.time_list_item, parent, false);
Log.d("RESULT","3: End of method: TimeTrackerAdapter: newView");
return view;
}
@Override
public int getCount() {
return times.size();
}
@Override
public Object getItem(int position) {
return getItem(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
convertView = inflater.inflate(R.layout.time_list_item, parent, false);
}
TimeRecord time = times.get(position);
TextView timeTextView = (TextView) convertView.findViewById(R.id.time_view);
timeTextView.setText(time.getTime());
TextView notesTextView = (TextView) convertView.findViewById(R.id.notes_view);
notesTextView.setText(time.getNotes());
Log.d("RESULT","2: End of method: TimeTrackerAdapter: getView");
return convertView;
}
public void addTimeRecord(String time, String notes) {
times.add(new TimeRecord(time, notes));
}
}