使用集合字符串的ArrayList [英] String ArrayList using Collections

查看:101
本文介绍了使用集合字符串的ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主要活动有多个的ArrayList 阵列 。当您单击熏香烟按钮,在 ArrayList中的应用程序日志某些信息。其中的的ArrayList的 locationsSmoked拥有所有你已经抽的位置。我的任务就是让一个活动,在的ListView 你吸过,而且位置显示有多少次你抽那里。
如果显示locationsSmoked 阵列目前,它看起来像这样

首页结果
首页结果
首页结果
工作结果
工作结果
学校结果
学校结果

和等等,等等。我需要遍历这个阵列来显示时间每个字符串被称为量,给整数值。然后,我需要将整数与匹配字符串结合起来。在平行阵列或多维阵列来显示另一个活动信息活动命名位置的列表视图
所以它看起来像这样

主页:3结果
工作:2结果
学校:2结果

应该指出的是,即使你永远不应该硬code元素,在这种情况下我真的很不准,因为位置的的ArrayList 将不断生长。还有另外一个活动我已经完成了允许用户在新位置添加到现有的的ArrayList
我认为,code我即将从我model.java和我locations.java下面列出的大部分准确。但是没有显示在列表视图当我去到活动

因此​​,如果这是任何困惑,请让我知道,我会问更具体。

感谢你能提供任何帮助。

Model.java

 的Bean;
进口的java.util.ArrayList;
进口的java.util.Calendar;
进口java.util.Collections中;
进口java.util.GregorianCalendar中;公共类模型实现Serializable {
公共静态最终诠释END_MORNING = 11; // 11:00 AM,包容
公共静态最终诠释END_AFTERNOON = 16; // 4:00 PM,包容
私人诠释locNumber;
私人字符串LOCNAME;私人的GregorianCalendar的startDate;私人的ArrayList<&GregorianCalendar的GT; datesSmoked =新的ArrayList<&GregorianCalendar的GT;();
私人的ArrayList<串GT; locationsSmoked =新的ArrayList<串GT;();
私人的ArrayList<串GT;位置=新的ArrayList<串GT;();
私人的ArrayList<串GT; allIncidents =新的ArrayList<串GT;();
私人的ArrayList<串GT;测试=新的ArrayList<串GT;();公众的String [] = defaultLocations {家,工作,上下班,学校,酒吧,餐厅,社交聚会,其他};
公众的String [] eachSmoked;
公共模型(GregorianCalendar的日期){
    的startDate =日期;    对于(一个String:this.defaultLocations){
        locations.add(多个);
    }
}公共模型(){
    这(新的GregorianCalendar()); //现在
}
公共无效setAllIncidentsArray(){
    this.allIncidents.add(datesSmoked.toString());
    this.allIncidents.add(locationsSmoked.toString());
}公共无效setEachSmoked(){
    的for(int i = 0; I< locationsSmoked.size();我++){
        locNumber = Collections.frequency(locationsSmoked,locationsSmoked.get(I));
        LOCNAME = locations.get(ⅰ);
        test.add(I,locNumber + LOCNAME);
    }
}公众的ArrayList<串GT; getEachSmoked(){
    返回this.test;
}公众的ArrayList<串GT; getAllIncidentsArray(){
    返回this.allIncidents;
}公众的ArrayList<串GT; getlocationsArray(){
    返回this.locations;
}公众的ArrayList<串GT; getLocationsSmokedArray(){
    返回this.locationsSmoked;
}公众的ArrayList<&GregorianCalendar的GT; getDatesSmokedArray(){
    返回this.datesSmoked;
}公共无效incrementCount(字符串位置){
    this.datesSmoked.add(新的GregorianCalendar()); //现在
    this.locationsSmoked.add(位置);
}公众诠释getTodayCount(){
    现在的GregorianCalendar =新的GregorianCalendar();
    INT todayDayOfYear = now.get(Calendar.DAY_OF_YEAR);    诠释计数= 0;
    对于(GregorianCalendar的一天:this.datesSmoked)
        如果(day.get(Calendar.DAY_OF_YEAR)== todayDayOfYear)
            算上++;    返回计数;
}公众诠释getTotalCount(){
    返回this.datesSmoked.size();
}公共双getAverage(){
    如果(getDays()大于0)
        回报(双)getTotalCount()/ getDays();
    其他
        返回0.0;
}公众诠释getDays(){
    返回(新的GregorianCalendar())获得(Calendar.DAY_OF_YEAR) - startDate.get(Calendar.DAY_OF_YEAR)+ 1。
}公众诠释getMorning(){
    诠释计数= 0;
    对于(GregorianCalendar的日期:this.datesSmoked)
        如果(date.get(Calendar.HOUR_OF_DAY)LT = END_MORNING)
            算上++;    返回计数;
}公众诠释getAfternoon(){
    诠释计数= 0;
    对于(GregorianCalendar的日期:this.datesSmoked)
        如果(date.get(Calendar.HOUR_OF_DAY)GT; END_MORNING和放大器;&安培; date.get(Calendar.HOUR_OF_DAY)LT = END_AFTERNOON)
            算上++;    返回计数;
}公众诠释getEvening(){
    诠释计数= 0;
    对于(GregorianCalendar的日期:this.datesSmoked)
        如果(date.get(Calendar.HOUR_OF_DAY)GT; END_AFTERNOON)
            算上++;    返回计数;
}
}

Locations.java(地点活动)

 进口java.io.ObjectInputStream中;
进口java.io.ObjectOutputStream中;进口android.app.Activity;
进口android.content.Context;
进口android.content.Intent;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.widget.ArrayAdapter;
进口android.widget.ListView;公共类LocationActivity延伸活动{公共静态最后弦乐SMOKIN_DATA_FILE =smokin.dat;公共静态模型模型= NULL;@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);    的setContentView(R.layout.activity_location);    restoreModel();    ListView控件的ListView =(ListView控件)findViewById(R.id.location_listview_Id);
    ArrayAdapter<串GT; listAdapter =新ArrayAdapter<串GT;(这一点,
            android.R.layout.simple_list_item_1,model.getEachSmoked());
    listView.setAdapter(listAdapter);
}@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    。getMenuInflater()膨胀(R.menu.activity_location,菜单);
    返回true;
}公共无效restoreModel(){
    //从磁盘恢复,或使用空模型开始
    尝试{
        ObjectInputStream的OIS =新的ObjectInputStream(
                openFileInput(SMOKIN_DATA_FILE));        模型=(模型)ois.readObject();
        ois.close();
    }赶上(例外五){
        Log.v(*** DEBUG ***,写入文件时出错:+ E);
        模型=新模式();
    }
}@覆盖
公共布尔onOptionsItemSelected(菜单项项){
    //处理项目选择
    开关(item.getItemId()){    案例R.id.menu_home_Id:
        意图homeIntent =新意图(这一点,MainActivity.class);
        startActivity(homeIntent); ;
        返回true;    案例R.id.menu_all_incidents_Id:
        意图allIncidentsIntent =新意图(这一点,AllIncidentsActivity.class);
        startActivity(allIncidentsIntent); ;
        返回true;    案例R.id.menu_edit_locations_Id:
        意图editLocationsIntent =新意图(这一点,EditLocationsActivity.class);
        startActivity(editLocationsIntent); ;
        返回true;
    默认:
        返回super.onOptionsItemSelected(项目);
    }}
}


解决方案

这看起来像一个完美的oportunity使用的Hashtable<字符串,整数> ,哈希表将包含位置作为键和计数的值。

现在,当您生成为ListView的数据,只需遍历熏地方ArrayList的元素,如果该元素是不是在哈希表添加和设定值0。如果它已经在它只是一个加​​一。

在它结束时,你有你需要的所有数据的哈希。

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

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

Home: 3
Work: 2
School: 2

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.

Model.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 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);
    }

}
}

解决方案

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.

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.

At the end of it, you have a HashTable with all the data you need.

这篇关于使用集合字符串的ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆