我的主要activity
有多个ArrayLists
and Arrays
。当您单击 Smoked Cigarette 按钮时,应用程序会将某些信息记录在ArrayList
. 其中一个ArrayLists
位置Smoked 包含您吸烟的所有位置。我的任务是制作一个activity
显示在ListView
您吸烟的位置,以及您在那里吸烟的次数。如果您显示array
当前的位置Smoked,它将如下所示
家
家
家
工作
工作
学校
学校
等等等等。我需要循环array
显示每个string
被调用的次数并给出integer
值。然后我需要将integer
与匹配的 结合起来string
。在并行array
或多维array
中显示该列表视图中另一个activity
名为 Locations的信息activity
所以它看起来像这样
家庭:3
工作:2
学校:2
应该注意的是,即使您永远不应该对元素进行硬编码,但在这种情况下,我实际上是不允许这样做的,因为ArrayList
位置将不断增长。activity
我已经完成了另一个允许用户将新位置添加到现有ArrayList
位置的方法。但是,listview
当我转到activity
.
因此,如果这令人困惑,请告诉我,我会更具体地问。
谢谢你尽你所能的帮助。
模型.java
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.GregorianCalendar;
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 int locNumber;
private String locName;
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>();
private ArrayList<String> test = new ArrayList<String>();
public String [] defaultLocations = {"Home", "Work", "Commuting", "School", "Bar", "Restaurant", "Social Gathering", "Other"};
public String [] eachSmoked;
public Model(GregorianCalendar date){
startDate = date;
for (String s : this.defaultLocations) {
locations.add(s);
}
}
public Model(){
this(new GregorianCalendar()); // now
}
public void setAllIncidentsArray() {
this.allIncidents.add(datesSmoked.toString());
this.allIncidents.add(locationsSmoked.toString());
}
public void setEachSmoked() {
for (int i = 0; i < locationsSmoked.size(); i++) {
locNumber = Collections.frequency(locationsSmoked, locationsSmoked.get(i));
locName = locations.get(i);
test.add(i, locNumber + locName);
}
}
public ArrayList<String> getEachSmoked() {
return this.test;
}
public ArrayList<String> getAllIncidentsArray() {
return this.allIncidents;
}
public ArrayList<String> getlocationsArray() {
return this.locations;
}
public ArrayList<String> getLocationsSmokedArray() {
return this.locationsSmoked;
}
public ArrayList<GregorianCalendar> getDatesSmokedArray() {
return this.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;
}
}
Locations.java(位置活动)
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class LocationActivity 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_location);
restoreModel();
ListView listView = (ListView) findViewById(R.id.location_listview_Id);
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, model.getEachSmoked());
listView.setAdapter(listAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_location, 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, MainActivity.class);
startActivity(homeIntent); ;
return true;
case R.id.menu_all_incidents_Id:
Intent allIncidentsIntent = new Intent(this, AllIncidentsActivity.class);
startActivity(allIncidentsIntent); ;
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);
}
}
}