所以我试图将两种不同类型的ArrayLists
( GregorianCalendar
& String
) 组合成第三种ArrayList
。然后将其显示ArrayList
到ListView
. 我不知道为什么它没有显示,因为我没有收到任何错误,而且它似乎应该可以工作。
该模型的代码如下。
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.GregorianCalendar;
import android.util.Log;
public class Model implements Serializable {
public static final int END_MORNING = 11; // 11:00AM, inclusive
public static final int END_AFTERNOON = 16; // 4:00PM, inclusive
private GregorianCalendar startDate;
private ArrayList<GregorianCalendar> datesSmoked = new ArrayList<GregorianCalendar>();
private ArrayList<String> locationsSmoked = new ArrayList<String>();
private ArrayList<String> locations = new ArrayList<String>();
private ArrayList<String> allIncidents = new ArrayList<String>();
public String [] defaultLocations = {"Home", "Work", "Commuting", "School", "Bar", "Restaurant", "Social Gathering", "Other"};
public String [] newLocation = {"",""};
public Model(GregorianCalendar date){
startDate = date;
for (String s : this.defaultLocations) {
locations.add(s);
}
}
public Model(){
this(new GregorianCalendar()); // now
}
public void setLocationSmoked() {
for (String s : this.newLocation) {
locationsSmoked.add(s);
}
}
public void setAllIncidentsArray() {
allIncidents.add(datesSmoked.toString());
allIncidents.add(locationsSmoked.toString());
}
public ArrayList<String> getAllIncidentsArray() {
return allIncidents;
}
public ArrayList<String> getlocationsArray() {
return locations;
}
public ArrayList<String> getLocationsSmokedArray() {
return locationsSmoked;
}
public ArrayList<GregorianCalendar> getDatesSmokedArray() {
return datesSmoked;
}
public void incrementCount(String location) {
this.datesSmoked.add(new GregorianCalendar()); // now
this.locationsSmoked.add(location);
}
public int getTodayCount() {
GregorianCalendar now = new GregorianCalendar();
int todayDayOfYear = now.get(Calendar.DAY_OF_YEAR);
int count = 0;
for (GregorianCalendar day : this.datesSmoked)
if (day.get(Calendar.DAY_OF_YEAR) == todayDayOfYear)
count++;
return count;
}
public int getTotalCount() {
return this.datesSmoked.size();
}
public double getAverage() {
if (getDays() > 0)
return (double) getTotalCount() / getDays();
else
return 0.0;
}
public int getDays() {
return (new GregorianCalendar()).get(Calendar.DAY_OF_YEAR) - startDate.get(Calendar.DAY_OF_YEAR) + 1;
}
public int getMorning() {
int count = 0;
for (GregorianCalendar date : this.datesSmoked)
if (date.get(Calendar.HOUR_OF_DAY) <= END_MORNING)
count++;
return count;
}
public int getAfternoon() {
int count = 0;
for (GregorianCalendar date : this.datesSmoked)
if (date.get(Calendar.HOUR_OF_DAY) > END_MORNING && date.get(Calendar.HOUR_OF_DAY) <= END_AFTERNOON)
count++;
return count;
}
public int getEvening() {
int count = 0;
for (GregorianCalendar date : this.datesSmoked)
if (date.get(Calendar.HOUR_OF_DAY) > END_AFTERNOON)
count++;
return count;
}
}
此代码适用于尝试在列表视图中显示数组的 Activity
import java.io.ObjectInputStream;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class AllIncidentsActivity extends Activity {
public static final String SMOKIN_DATA_FILE = "smokin.dat";
public static Model model = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_all_incidents);
restoreModel();
ListView listView = (ListView) findViewById(R.id.all_incidents_listview_Id);
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, model.getAllIncidentsArray());
listView.setAdapter(listAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_all_incidents, menu);
return true;
}
public void restoreModel() {
// Restore from disk, or start with an empty model
try {
ObjectInputStream ois = new ObjectInputStream(
openFileInput(SMOKIN_DATA_FILE));
model = (Model) ois.readObject();
ois.close();
} catch (Exception e) {
Log.v("*** DEBUG ***", "Error writing to file: " + e);
model = new Model();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_home_Id:
Intent homeIntent = new Intent(this, AllIncidentsActivity.class);
startActivity(homeIntent); ;
return true;
case R.id.menu_locations_Id:
Intent locationsIntent = new Intent(this, LocationActivity.class);
startActivity(locationsIntent); ;
return true;
case R.id.menu_edit_locations_Id:
Intent editLocationsIntent = new Intent(this, EditLocationsActivity.class);
startActivity(editLocationsIntent); ;
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
预先感谢您的任何帮助。