Object ArrayList未显示到ListView

时间:2013-03-06 06:08:13

标签: java android arrays listview

所以我尝试将两种不同类型的ArrayListsGregorianCalendar& 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;
}
}

此代码用于试图在列表视图中显示数组的活动

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

}

提前感谢您的帮助。

2 个答案:

答案 0 :(得分: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中一起显示在同一行上。其他方法可能更有效,但由于这些方法实际上是parellel 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

显示指定活动中的新ArrayAdapter所有事件
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();
}

答案 1 :(得分:0)

不要拿对象。将它转换为String arraylist并且它将工作。为什么你需要使用Object ArrayList?在List中,您将看到Strings not Object。