Vector 自定义类的赋值与初始化

自定义类与数据库字段一致,读取数据库,添加到vector

public class Classes {
	public class strSortname{
		public String sortname;
		public int rowid;
		public int ncount;
		public strSortname(){
			sortname="";
			rowid=0;
			ncount=0;
		}
	}
	}
    	public Vector<strSortname> getData(){
    	    	Vector<strSortname> vector=new Vector<strSortname>();
    	       	SQLiteDatabase database;
    	       	Cursor cur;
    	       	String sql;
    	       	database=""; //database 与sql 赋值
    	       	sql="select * from table";
    	       	cur=database.rawQuery(sql, null);
		cur.moveToFirst(); //一般需加这句,否则容易出来空指针
		if (cur.isBeforeFirst()){
		    cur.close;
		    database.close;
			return vector;
		}
		for (int i=0;i<cur.getCount();i++)
        {
        	Classes classes =new Classes();
        	strSortname temp = classes.new strSortname;       	//初始化赋值必须在循环体内
            temp.sortname= cur.getString(cur.getColumnIndex("sortname"));
            temp.rowid=cur.getInt(cur.getColumnIndex("rowid"));
            temp.ncount=cur.getInt(cur.getColumnIndex("ncount"));
            vector.add(temp);
            cur.moveToNext();
            if (cur.isAfterLast()) break;
        }
		cur.close();
		database.close();
		return vector;
}

你可能感兴趣的:(android)