Question

I have an activity that stores assignments with their timestamp in ms. I then want an activity that lists all the timestamps and names of the activity if that activity is actually today or tomorrow. I started by just trying to show the timestamps for all of the activities that are today in one listview. This gives me and error saying that "_id" is does not exist as a column. Here's my code:

public class UpcomingActivity extends Activity implements LoaderManager.LoaderCallbacks<Cursor>{

@SuppressLint("NewApi")
private DatabaseHelper databaseHelper;
private SimpleCursorAdapter mAdapter;
private ListView listView;
private String[] today= new String[5]; //max 5 values
private String[] tom= new String[5]; //max 5 values

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_upcoming);

    databaseHelper = new DatabaseHelper(getApplicationContext());

    SQLiteCursorLoaderActivities cLoader = new SQLiteCursorLoaderActivities(getApplicationContext(),
                                                                                databaseHelper,
                                                                                null,
                                                                                null);

    Cursor c = cLoader.buildCursor();

    c.moveToFirst();
    int todayTracker = 0; 
    int tomTracker = 0; 
    int alltimes = 1;
    long day = 86400000; // length of a day in ms
    String newText;
    TextView testField = (TextView)findViewById(R.id.test);

    listView = (ListView) findViewById(android.R.id.list);
    listView.setEmptyView(findViewById(android.R.id.empty));

    //loop through all records and check if the date is today!
  for(int i=0; i<alltimes;i++){
    long time = c.getInt(i);
    if(DateUtils.isToday(time)){

    newText = String.valueOf(time);

    today[todayTracker] = newText; 
    todayTracker++;
    }
    else{
        time = time + day; //maybe it is tomorrow?
       if(DateUtils.isToday(time)){

            newText = String.valueOf(time);

            tom[tomTracker] = newText; 
            tomTracker++;
            }
    }

  }

    int[] toViews = {android.R.id.text2};

    mAdapter = new SimpleCursorAdapter( getApplicationContext(), 
             R.layout.my_list_element, 
             c, 
             today, 
             toViews, 
             0);

    listView.setAdapter(mAdapter);


    getLoaderManager().initLoader(0, null, this);

}

Here is my log cat:

05-06 09:16:43.243: E/AndroidRuntime(2025): Caused by: java.lang.IllegalArgumentException: column '_id' does not exist

Why is there an error, and for the future how could I populate two different listviews? Thanks!

Was it helpful?

Solution

for fetching and filling data to ListViews every query from databases have to contains a column named _id . you have to include _id in your cursor

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top