-4

所以我试图将两种不同类型的ArrayLists( GregorianCalendar& String) 组合成第三种ArrayList。然后将其显示ArrayListListView. 我不知道为什么它没有显示,因为我没有收到任何错误,而且它似乎应该可以工作。

该模型的代码如下。

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

}

预先感谢您的任何帮助。

4

2 回答 2

1

我想到了。我正在发布我的解决方案,以便其他新Android开发人员可以从我的错误中吸取教训。

首先制作一个for-loop以格式化原始日期ArrayList

/**
This method loops through datesSmoked ArrayList<GregorianCalendar> and formats each
element with a simpledateformatter named sdf. Then adds the formatted elements to a new  
ArrayList<String> times. Finally it returns the value of the times ArrayList
*/
public ArrayList<String> getDates() {
    for (int i = 0; i < datesSmoked.size(); i++) {
        String s = (sdf.format(i)); // formats each line here
        times.add(s);               // adds the elements to the new ArrayList here
    }
    return times;                   // returns the values from the new ArrayList
}

二是把这两条平行String ArrayLists加起来合二为一ArrayList。需要注意的是,这些需要在我的ListView. 其他方法可能更有效,但由于这些方法实际上是并行ArrayLists的并且总是相同的大小,因此下面的代码可以完美运行。

/**
This method adds the two ArrayLists together into a new ArrayList, then returns the  
values concatenated into one line. 
*/
public ArrayList<String> getAllIncidentsArray() {

    for (int i = 0; i < datesSmoked.size(); i++) {
        allIncidents.add(getDates().get(i) + ", " + locationsSmoked.get(i));
    }

return allIncidents;
}

第三次ArrayList用你的在指定活动中显示新的 allIncidentsArrayAdapter

public class LocationActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_location);
        // find your listview
        ListView listView = (ListView) findViewById(R.id.location_listview_Id);
        // declare your ArrayAdapter and attach your ArrayList or in this case Method
        ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, model.getPlacesSmoked());      
        // attach the ArrayAdapter to your ListView
        listView.setAdapter(listAdapter);
        // Notify your ArrayAdapter of any changes when these ArrayLists grow
        listAdapter.notifyDataSetChanged();
}
于 2013-03-11T15:00:08.877 回答
0

不要拿对象。将其转换为 String arraylist 即可。为什么需要使用 Object ArrayList ?在列表中,您将看到字符串不是对象。

于 2013-03-06T06:15:41.707 回答