使用集合的字符串 ArrayList [英] String ArrayList using Collections
问题描述
我的主要 activity
有多个 ArrayLists
和 Arrays
.当您单击 Smoked Cigarette 按钮时,应用程序会在 ArrayList
中记录某些信息.ArrayLists
locationSmoked 之一保存了您吸烟的所有位置.我的任务是制作一个 activity
,在 ListView
中显示您吸烟的位置以及您在那里吸烟的次数.如果您当前显示locationsSmoked array
它将看起来像这样
My main activity
has multiple ArrayLists
and Arrays
. When you click the Smoked Cigarette button, the application logs certain information in the ArrayList
. One of the ArrayLists
locationsSmoked holds all the locations you've smoked at. My task is to make an activity
that displays in a ListView
the location you smoked at, and how many times you smoked there.
If you display the locationsSmoked array
currently it will look like this
首页
首页
首页
工作
工作
学校
学校
Home
Home
Home
Work
Work
School
School
等等等等.我需要遍历这个 array
以显示每个 string
被调用的次数并给出 integer
值.然后我需要将 integer
与匹配的 string
结合起来.在并行 array
或多维 array
中显示另一个 activity
中的信息,该信息在该 activity
的列表视图中命名为 Locations所以看起来像这样
and so on and so forth. I need to loop through this array
to display the amount of times each string
is called and give the integer
value. I then need to combine the integer
with the matching string
. In a parallel array
or multidimensional array
to display the information in another activity
named Locations in that activity
's listview
So it looks like this
首页:3
作品:2
学校:2
Home: 3
Work: 2
School: 2
应该注意的是,即使您永远不应该对元素进行硬编码,但在这种情况下,我确实不允许这样做,因为位置的 ArrayList
将不断增长.我已经完成了另一个 activity
,它允许用户向现有的 ArrayList
添加新位置我认为我将在下面从我的 model.java 和我的locations.java 中列出的代码大部分是准确的.但是,当我转到 activity
时,listview
中没有显示任何内容.
It should be noted that even though you should never hard code elements, in this situation I literally am not allowed to because the ArrayList
of locations will be constantly growing. There is another activity
I've already completed that allows the user to add new locations to the existing ArrayList
I think the code I'm about to list below from my model.java and my locations.java is for the most part accurate. However nothing displays in the listview
when i go to the activity
.
因此,如果这让我感到困惑,请告诉我,我会更具体地询问.
So if that was confusing in any please let me know and I'll ask more specifically.
感谢您提供的任何帮助.
Thanks for any help you can offer.
模型.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(位置活动)
Locations.java (locations Activity)
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);
}
}
}
推荐答案
这看起来是使用 Hashtable
的绝佳机会,Hashtable 将包含位置作为键和算作值.
This looks like a perfect oportunity to use a Hashtable<String, Integer>
, The Hashtable would contain the locations as the Keys and Count as the values.
现在,当您为 ListView 生成数据时,只需遍历 ArrayList 元素以获取熏过的地方,如果该元素不在 HashTable 中,则添加它并将 Value 设置为 0.如果它已经在其中,只需将其加一.
Now when you generate the data for the ListView, simply iterate over the ArrayList elements for places smoked, if the element is not in the HashTable add it and set Value to 0. If it is already in it simply increment it by one.
最后,您将拥有一个 HashTable,其中包含您需要的所有数据.
At the end of it, you have a HashTable with all the data you need.
这篇关于使用集合的字符串 ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!