gpt4 book ai didi

java - Android ListView 不会填充 SQLite DB 中的所有条目

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

我正在开发一个学习 Android 应用程序(我称之为 PersistAlarm),我可以在其中设置闹钟。应用程序的第一个 Activity 是一个带有 ListView 的按钮,用户可以在其中向应用程序添加警报。警报在第二个 Activity 中设置,并填充后端的数据库。

设置警报后,它将返回到填充警报列表的第一个 Activity (PersistAlarm.java)。

问题是,到目前为止,我能够获取警报列表。突然间,现在只有警报表中的第一个条目可见。其他条目不显示。奇怪!

谁能告诉我哪里出错了?

这是 PersistAlarm Activity XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".PersistAlarm" >

<Button
android:id="@+id/buttonAddAlarm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Entry" />

<ListView
android:id="@+id/alarmListV"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>

这是 PersistAlarm.java 代码

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_persist_alarm);

final AlarmAdapter alarmAdapt;
final ListView mainAlarmView;

final Context context = this;



Button alarmAdd = (Button) findViewById(R.id.buttonAddAlarm);

mainAlarmView = (ListView) findViewById(R.id.alarmListV);

alarmAdd.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, AddEditAlarm.class);
startActivity(intent);
}
});

List<PAlarm> alarmList = db.getAllAlarms();
Log.d("LIST COUNT" , String.valueOf(alarmList.size()));
alarmAdapt = new AlarmAdapter(PersistAlarm.this, R.id.alarmListV, alarmList);
mainAlarmView.setAdapter(alarmAdapt);

}

这是我的 PAlarm 对象,它充当应用程序的基础对象:

public class PAlarm {

int _id;
String _eventName;
String _eventDate;
String _eventTime;
String _type;

public PAlarm() {

}

public PAlarm(int id, String eventName, String eventDate, String eventTime, String type) {
this._id = id;
this._eventName = eventName;
this._eventDate = eventDate;
this._eventTime = eventTime;
this._type = type;
}

public PAlarm(String eventName, String eventDate, String eventTime, String type) {
this._eventName = eventName;
this._eventDate = eventDate;
this._eventTime = eventTime;
this._type = type;
}

public int get_id() {
return _id;
}

public void set_id(int _id) {
this._id = _id;
}

public String get_eventName() {
return _eventName;
}

public void set_eventName(String _eventName) {
this._eventName = _eventName;
}

public String get_eventDate() {
return _eventDate;
}

public void set_eventDate(String _eventDate) {
this._eventDate = _eventDate;
}

public String get_eventTime() {
return _eventTime;
}

public void set_eventTime(String _eventTime) {
this._eventTime = _eventTime;
}

public String get_type() {
return _type;
}

public void set_type(String _type) {
this._type = _type;
}

@Override
public String toString() {
return this._id + ". " + this._eventName + " - " + this._eventDate + " - " + this._eventTime;
}

这是我用来渲染 ListView 的自定义适配器

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
android:id="@+id/alarmNameTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24dip" />
<TextView
android:id="@+id/alarmDateTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="9dip" />

</LinearLayout>

另外,我的自定义适配器的 java 代码:

public class AlarmAdapter extends ArrayAdapter<PAlarm> {
private List<PAlarm> alarms;
private Activity activity;

public AlarmAdapter(Activity a, int textViewId, List<PAlarm> alms) {
super(a, textViewId, alms);
this.alarms = alms;
this.activity = a;

}

public static class ViewHolder {
public TextView alarmView;
public TextView alarmDateTimeView;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

View v = convertView;
ViewHolder holder;

if (v == null) {
LayoutInflater vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.alarm_item, null);
holder = new ViewHolder();
holder.alarmView = (TextView) v.findViewById(R.id.alarmNameTV);
holder.alarmDateTimeView = (TextView) v.findViewById(R.id.alarmDateTime);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
final PAlarm alarm = alarms.get(position);
if (alarm != null) {
holder.alarmView.setText(alarm.get_eventName());
holder.alarmDateTimeView.setText(alarm.get_eventDate() + " (" + alarm.get_eventTime() + ")" );
}
}

return v;

}

希望得到一些帮助。谢谢

最佳答案

尝试更改您的getView():

public View getView(int position, View convertView, ViewGroup parent) {

View v = convertView;
ViewHolder holder;

if (v == null) {
LayoutInflater vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.alarm_item, null);
holder = new ViewHolder();
holder.alarmView = (TextView) v.findViewById(R.id.alarmNameTV);
holder.alarmDateTimeView = (TextView) v.findViewById(R.id.alarmDateTime);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();


}
final PAlarm alarm = alarms.get(position);
if (alarm != null) {
holder.alarmView.setText(alarm.get_eventName());
holder.alarmDateTimeView.setText(alarm.get_eventDate() + " (" + alarm.get_eventTime() + ")" );
}

return v;

}

说明:

ListView 中,如果 getView() 中的 viewnull,则 View 将被回收,这意味着 view 没有被回收,我们必须初始化它;我们在 if 条件下执行的操作。如果view不为null,则意味着它被回收,我们只需更改该view的数据即可。

所以 View 是否被回收我们必须设置数据。所以数据设置代码应该在 if...else

之外

关于java - Android ListView 不会填充 SQLite DB 中的所有条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18655662/

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