gpt4 book ai didi

java - 获取同一个对象到Android ListView

转载 作者:行者123 更新时间:2023-12-01 23:12:08 25 4
gpt4 key购买 nike

我尝试在 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;
}

这是我需要从数据库发布对象的 Activity 代码:

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);



Cursor cursor = database.query(TABLE_REMINDERS, null, null,null,null,null,null);
getRemindersCount();
count = Integer.toString(getRemindersCount());
countRem.setText(count);
if(cursor.moveToFirst())
{
int idIndex = cursor.getColumnIndex(DBHelper.KEY_ID);
int nameIndex = cursor.getColumnIndex(DBHelper.KEY_NAME);
int hourIndex = cursor.getColumnIndex(DBHelper.KEY_HOUR);
int dateIndex = cursor.getColumnIndex(DBHelper.KEY_DATE);
String name = Integer.toString(nameIndex);
String hour = Integer.toString(hourIndex);
String date = Integer.toString(dateIndex);
do{
ReminderList.add(new Reminder(idIndex, name, hour, date));
ReminderListAdapter adapter = new ReminderListAdapter(this, R.layout.reminder_view, ReminderList);
listView.setAdapter(adapter);

} while(cursor.moveToNext());
} else
Log.d("mLog", "0 rows in db");

}

public int getRemindersCount(){
long NrOfReminders = DatabaseUtils.queryNumEntries(database, TABLE_REMINDERS);
int count = (int)NrOfReminders;
return count;
}

结果,我在 ListView 中得到相同的输出。对象1:ID:0,名称:1,小时:2,日期:3。该数据与数据库中的数据不匹配。而且,这个输出适用于每个对象。有什么错误吗?如果这是一个愚蠢的错误,我很抱歉,这是第一个应用程序。

最佳答案

cursor.getColumnIndex(...) 仅返回行的列索引。要获取该列中的值,您有几个选项:

  • 如果事先知道数据类型,则可以直接调用光标上相应的方法。例如,如果DBHelper.KEY_NAME列包含一个String,我们可以直接使用cursor.getString(cursor.getColumnIndex(DBHelper. KEY_NAME))

  • 如果没有,您可以尝试调用 cursor.getType(int columnIndex)获取基础类型,然后根据返回值调用游标上相应的方法。

简而言之,代码的问题在于您获取的是列索引 (getColumnIndex),而不是列 (getFloatgetIntgetString等):

    ...
int idIndex = cursor.getColumnIndex(DBHelper.KEY_ID);
int nameIndex = cursor.getColumnIndex(DBHelper.KEY_NAME);
int hourIndex = cursor.getColumnIndex(DBHelper.KEY_HOUR);
int dateIndex = cursor.getColumnIndex(DBHelper.KEY_DATE);
String name = cursor.getString(nameIndex);
String hour = cursor.getString(hourIndex);
String date = cursor.getString(dateIndex);
...

关于java - 获取同一个对象到Android ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58368826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com