我尝试在 Android 中开发我的第一个应用程序,但遇到了一个错误。我需要从 SQLite 数据库获取对象并在 ListView 中显示它们。这是我的适配器的代码:
public class ReminderListAdapter extends ArrayAdapter<Reminder> {
private Context mContext;
private int mResource;
public ReminderListAdapter(@NonNull Context context, int resource, @NonNull ArrayList<Reminder> objects) {
super(context, resource, objects);
mContext = context;
mResource = resource;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
int id = getItem(position).getId();
String name = getItem(position).getName();
String hour = getItem(position).getHour();
String date = getItem(position).getDate();
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(mResource, parent, false);
TextView resId = convertView.findViewById(R.id.textId);
TextView resName = convertView.findViewById(R.id.textName);
TextView resHour = convertView.findViewById(R.id.textHour);
TextView resDate = convertView.findViewById(R.id.textDate);
resId.setText(String.valueOf(id));
resName.setText(name);
resHour.setText(hour);
resDate.setText(date);
return convertView;
}
这是我需要从数据库发布对象的活动代码:
public class EditActivity extends AppCompatActivity {
DBHelper dbHelper;
SQLiteDatabase database;
private ListView listView;
private TextView countRem;
private String count;
ArrayList<Reminder> ReminderList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
dbHelper = new DBHelper(this);
database = dbHelper.getWritableDatabase();
listView =(ListView) findViewById(R.id.ListOfReminders);
countRem =(TextView) findViewById(R.id.textView);
结果,我在列表视图中得到相同的输出。对象1:ID:0,名称:1,小时:2,日期:3。该数据与数据库中的数据不匹配。而且,这个输出适用于每个对象。有什么错误吗?如果这是一个愚蠢的错误,我很抱歉,这是第一个应用程序。
呼如林
相关分类