我尝试使用SQLite数据库中的数据填充ListView
。我已经检查了无数的教程,去探索Stack Overflow,虽然很多人都有类似我的问题,但是我越是研究代码为什么不起作用,我就越多。找个问题。我必须遗漏一些重要的事情,因为我已经解决了这个问题,所以发布这个问题确实是我的最后一招。任何帮助将不胜感激。
在我继续之前,我并没有要求任何人为我编码,我已经被困在这个问题上好几个小时了,而且我没有到达任何地方。
到目前为止,我已经创建了一个Fragment
类:
public class RecordsListFragment extends Fragment implements OnItemClickListener, OnItemLongClickListener {
public static final String ARGUMENT_ITEM_ID = "records_list";
Activity activity;
ListView recordsListView;
ArrayList<Records> records;
RecordsListAdapter recordsListAdapter;
RecordsDAO recordsDAO;
private GetRecordsTask task;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity = getActivity();
recordsDAO = new RecordsDAO(activity);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_records_list, container, false);
findViewsById(view);
task = new GetRecordsTask(activity);
task.execute((Void) null);
recordsListView.setOnItemClickListener(this);
recordsListView.setOnItemLongClickListener(this);
return view;
}
private void findViewsById(View view) {
recordsListView = (ListView) view.findViewById(R.id.list_records);
recordsListView.setOnItemClickListener(this);
recordsListView.setOnItemLongClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Records record = (Records) parent.getItemAtPosition(position);
if (records != null) {
Bundle arguments = new Bundle();
arguments.putParcelable("selectedRecord,", record);
CustomRecordsDialogFragment customRecordsDialogFragment = new CustomRecordsDialogFragment();
customRecordsDialogFragment.setArguments(arguments);
customRecordsDialogFragment.show(getFragmentManager(), CustomRecordsDialogFragment.ARGUMENT_ITEM_ID);
}
}
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Records records = (Records) parent.getItemAtPosition(position);
recordsDAO.deleteRecord(records);
recordsListAdapter.remove(records);
return true;
}
public class GetRecordsTask extends AsyncTask<Void, Void, ArrayList<Records>> {
private final WeakReference<Activity> activityWeakRef;
ArrayList<Records> recordsArrayList = new ArrayList<>();
public GetRecordsTask(Activity context) {
this.activityWeakRef = new WeakReference<Activity>(context);
}
@Override
protected void onPostExecute(ArrayList<Records> empList) {
if (activityWeakRef.get() != null && !activityWeakRef.get().isFinishing()) {
records = empList;
if (empList != null) {
if (empList.size() != 0) {
recordsListAdapter = new RecordsListAdapter(activity, empList);
recordsListView.setAdapter(recordsListAdapter);
} else {
Toast.makeText(activity, "No records in the database", Toast.LENGTH_LONG).show();
}
}
}
}
@Override
protected ArrayList<Records> doInBackground(Void... arg0) {
recordsArrayList = recordsDAO.getRecords();
return recordsArrayList;
}
}
public void updateView() {
task = new GetRecordsTask(activity);
task.execute((Void) null);
}
public void onResume() {
getActivity().setTitle(R.string.app_name);
getActivity().getActionBar().setTitle(R.string.app_name);
super.onResume();
}
}
和Adapter
类
public class RecordsListAdapter extends ArrayAdapter<Records>{
private Context context;
List<Records> records;
private static final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
public RecordsListAdapter(Context context, List<Records> records) {
super(context, R.layout.list_item, records);
this.context = context;
this.records = records;
}
private class ViewHolder {
TextView recordsIdTxt, recordsDescriptionTxt, recordsDateTxt, recordsStartTxt, recordsFinishTxt;
}
@Override
public int getCount() {
return records.size();
}
@Override
public Records getItem(int position) {
return records.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.recordsIdTxt = (TextView) convertView.findViewById(R.id.text_record_id);
holder.recordsDescriptionTxt = (TextView) convertView.findViewById(R.id.text_record_description);
holder.recordsDateTxt = (TextView) convertView.findViewById(R.id.text_record_date);
holder.recordsStartTxt = (TextView) convertView.findViewById(R.id.text_record_start);
holder.recordsFinishTxt = (TextView) convertView.findViewById(R.id.text_record_finish);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Records records = (Records) getItem(position);
holder.recordsIdTxt.setText(String.format("%d", records.getId()));
holder.recordsDescriptionTxt.setText(records.getName());
holder.recordsDateTxt.setText(formatter.format(records.getDateofRecord()));
holder.recordsStartTxt.setText(records.getStart());
holder.recordsFinishTxt.setText(records.getFinish());
return convertView;
}
@Override
public void add(Records record) {
records.add(record);
notifyDataSetChanged();
super.add(record);
}
@Override
public void remove(Records record) {
records.remove(record);
notifyDataSetChanged();
super.remove(record);
}
}
但ListView
中没有显示任何内容。现在,我没有收到任何错误,但即使在填写表单并保存之后,我看到No records in the database
toast消息仍然出现。保存记录发生的片段返回Record saved
+数据库正在填满,我已经检查了/data/data
中的数据库文件,并且我已经使用DB Browser for SQLite来仔细检查。< / p>
那么为什么empList.size()
然后=0
,如果数据库中有数据我
同样,我所要求的是有人帮我理解发生了什么以及我错在哪里。
编辑1:这是我用来从数据库中检索所有记录的方法:
public ArrayList<Records> getRecords() {
ArrayList<Records> records = new ArrayList<Records>();
String query = "SELECT " + RECORDS_ID_WITH_PREFIX + ", "
+ RECORDS_NAME_WITH_PREFIX + " ,"
+ DatabaseHelper.KEY_CREATED_AT + ", "
+ DatabaseHelper.STARTINGHOUR + ", "
+ DatabaseHelper.FINISHINGHOUR + ", "
+ DatabaseHelper.COMMENTS + ", "
+ DatabaseHelper.LOCATIONS_ID + ", "
+ DatabaseHelper.TAGS_ID + ", " + LOCATIONS_NAME_WITH_PREFIX + ", " + TAGS_NAME_WITH_PREFIX + " FROM "
+ DatabaseHelper.TABLE_RECORDS + " rec, " + DatabaseHelper.TABLE_LOCATIONS + " loc, " + DatabaseHelper.TABLE_TAGS + " tg "
+ " WHERE rec." + DatabaseHelper.LOCATIONS_ID + " = loc." + DatabaseHelper.ID + " AND "
+ DatabaseHelper.TAGS_ID + " = tg." + DatabaseHelper.ID;
Cursor cursor = database.rawQuery(query, null);
while (cursor.moveToNext()) {
Records records1 = new Records();
records1.setId(cursor.getInt(0));
records1.setName(cursor.getString(1));
try {
records1.setDateofRecord(formatter.parse(cursor.getString(2)));
} catch (ParseException e) {
records1.setDateofRecord(null);
}
records1.setStart(cursor.getString(3));
records1.setFinish(cursor.getString(4));
records1.setComments(cursor.getString(5));
Locations locations = new Locations();
locations.setId(cursor.getInt(6));
locations.setName(cursor.getString(7));
Tags tags = new Tags();
tags.setId(cursor.getInt(8));
tags.setName(cursor.getString(9));
records1.setLocations(locations);
records1.setTags(tags);
records.add(records1);
}
return records;
}
编辑2:解决我的问题很简单。 WHERE
语句似乎过滤了数据库中的数据,这就是我无法在Log.d
中看到任何内容的原因。只需更换
String query = "SELECT " + RECORDS_ID_WITH_PREFIX + ", "
+ RECORDS_NAME_WITH_PREFIX + " ,"
+ DatabaseHelper.KEY_CREATED_AT + ", "
+ DatabaseHelper.STARTINGHOUR + ", "
+ DatabaseHelper.FINISHINGHOUR + ", "
+ DatabaseHelper.COMMENTS + ", "
+ DatabaseHelper.LOCATIONS_ID + ", "
+ DatabaseHelper.TAGS_ID + ", " + LOCATIONS_NAME_WITH_PREFIX + ", " + TAGS_NAME_WITH_PREFIX + " FROM "
+ DatabaseHelper.TABLE_RECORDS + " rec, " + DatabaseHelper.TABLE_LOCATIONS + " loc, " + DatabaseHelper.TABLE_TAGS + " tg "
+ " WHERE rec." + DatabaseHelper.LOCATIONS_ID + " = loc." + DatabaseHelper.ID + " AND "
+ DatabaseHelper.TAGS_ID + " = tg." + DatabaseHelper.ID;
与
String query = "SELECT * FROM " + DatabaseHelper.TABLE_RECORDS;