如何显示从db获取数据?

时间:2013-12-31 02:52:45

标签: android

我是Android应用程序的初学者。我正在尝试开发TODO列表。 我想从数据库中获取一些数据(日期,标题和ID)并在列表视图中显示。通过单击列表视图,我想显示所单击项目的所有详细信息。我的问题是我已经使用arraylist填充了listview,但我不想在列表视图中显示id,所以我尝试使用哈希表,但之后我得到了这个错误。

有没有更常见的解决方案?

这是我的代码,我评论一些与第一路相关的代码==>数组列表 包com.Sali.todolist_final;

公共类MainActivity扩展了Activity {

DBAdapter db = new DBAdapter(this);

ListView lv;

ArrayList<String> taskArrayList;
ArrayAdapter<String> taskArrayAdapter;


SimpleAdapter mSchedule;

@SuppressWarnings("null")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);




    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

    mSchedule = new SimpleAdapter(this, mylist, R.layout.activity_main,
            new String[] {"title", "duedate"}, new int[] {R.id.TRAIN_CELL, R.id.FROM_CELL});        

     lv = (ListView) findViewById(R.id.SCHEDULE);


    DisplayUpComingTODO();      
    lv.setAdapter(mSchedule);



}// On Create

// //-------------------------------Functions---------------------/////////////

// -------------- UP Coming TODO--------------
public void DisplayUpComingTODO() {

    db.open();
    //Toast.makeText(this, "DisplayUpComingTODO", Toast.LENGTH_SHORT).show();
    Log.w("getUpComingTODO.MAIN", "DB>OPEN");
    Cursor c = db.getUpComingTODO();
    // c.moveToFirst();
    if (c.moveToFirst()) {
        do {
            AddtoList(c);
            Toast.makeText(this, "AddtoList", Toast.LENGTH_SHORT).show();
        } while (c.moveToNext());
    }

    db.close();

    // return taskArrayList;
}

public void AddtoList(Cursor c) {

/*  String v_title = c.getString(c.getColumnIndex("title"));
    String v_duedate = c.getString(c.getColumnIndex("duedate"));

    String v_text = v_title + "   " + v_duedate;
//  Hashtable<title, V>
    taskArrayList.add(v_text);*/


//  ListView list = (ListView) findViewById(R.id.SCHEDULE);
    Toast.makeText(this, "AddtoList", Toast.LENGTH_LONG).show();

    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
    HashMap<String, String> map = new HashMap<String, String>();

    map.put("title", c.getString(c.getColumnIndex("title")));
    map.put("duedate",  c.getString(c.getColumnIndex("duedate")));
    map.put("id", c.getString(c.getColumnIndex("id")));

    mylist.add(map);

    mSchedule = new SimpleAdapter(this, mylist, R.layout.activity_main,
                new String[] {"title", "duedate"}, new int[] {R.id.TRAIN_CELL, R.id.FROM_CELL});


 TextView t=(TextView) findViewById(R.id.TRAIN_CELL);
 t.setText("dSD");
}


private Cursor GetIdofClickedIteminListView(String selectedTitle) {

    return db.getRecordByTitle(selectedTitle);  
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

这里是getUpComingTODO:

public Cursor getUpComingTODO() throws SQLException 
{
    cal = Calendar.getInstance();

    int y = cal.get(Calendar.YEAR);
    int m = cal.get( Calendar.MONTH+1);
    int d = cal.get(Calendar.DAY_OF_MONTH) + 1;// +1 : to retrive the task
                                                // are gonna be done in next
                                                // day.


    Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
            KEY_ROWID,KEY_TITLE, KEY_DUEDATE}, KEY_DUEYEAR + "=" + y + " AND "+ KEY_DUEMONTH + "=" + m + " AND " + KEY_DUEDAY + "=" + d, // Where Cluse  
            null, null, null, null, null, null);

    if (mCursor != null) {
        mCursor.moveToFirst();
    }else {
        Log.w("getUpComingTODO", "NUUUUUL");
    }

    return mCursor;
}

0 个答案:

没有答案