我正在研究学习Android应用程序(PersistAlarm,我称之为),我可以设置闹钟。应用程序的第一个活动是一个带有ListView的按钮,用户可以在其中向应用程序添加警报。警报在第二个活动中设置,它在后端填充数据库。
设置闹钟后,它将返回第一个活动(PersistAlarm.java),其中填充了警报列表。
问题是到目前为止,我能够得到警报列表。突然,现在只能看到报警表中的第一个条目。其他条目未显示。奇怪!
谁能告诉我哪里出错了?
这里是PersistAlarm活动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;
}
在此感谢一些帮助。谢谢
答案 0 :(得分:1)
尝试更改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
view
getView()
null
view
为view
,则null
视图会被回收,这意味着view
不会被回收,我们必须对其进行初始化;如果条件我们做的。如果if...else
不是{{1}},则意味着它已被回收,我们只需要更改{{1}}的数据。
所以视图是否被回收,我们必须设置数据。所以数据设置代码应该在{{1}}
之外