Reputation: 399
I am working on a learning Android application (PersistAlarm, I call it) where I can set an alarm. The application's first activity is a button with ListView where the user can add alarms to the application. The alarm is set in the second activity and it populates the DB in the backend.
Once the alarm is set, it returns to the first activity (PersistAlarm.java) where the list of alarms is populated.
The problem is that till now, I was able to get the list of alarms. Suddenly, only the first entry in the alarms table is now visible. Other entries arent displayed. Strange!
Can anyone tell me where I am going wrong?
Here's the 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>
Here's the PersistAlarm.java code
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);
}
Here's my PAlarm object that acts as the foundation object for the Application:
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;
}
This is my custom Adapter to render the 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>
Also, my Custom Adapter's java code:
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;
}
Would appreciate some help here. Thanks
Upvotes: 0
Views: 241
Reputation: 1380
try changing your 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;
}
Explaination:
In ListView
views are recycled if view
coming in getView()
is null
it means view
is not recycled and we have to initialize it; which we do in if condition. if view
is not null
that means it is recycled and we just have to change the data for that view
.
so view is recycled or not we have to set data. so data setting code should be outside if...else
Upvotes: 1