zhuyangyang123
zhuyangyang123
2012-07-06 14:19

怎样在一个类中 得到 数据库的 Context ? ? ? 我这样写 会得到吗? 可是还是报空指针。。。

  • it技术
  • 互联网问答
  • IT行业问题
  • 编程语言问答
  • 计算机技术

[code="java"]

public class MovieService{
public static Context context1;

private DatabaseHelper dbHelper;
@SuppressWarnings("static-access")
public MovieService(Context context) {
    this.context1 = context;
    this.dbHelper = new DatabaseHelper(context1);
}

/**
 * 添加电影
 * @param movie
 */


public List<Movie> addOnline_All(Movie movie){
    List<Movie> list=new ArrayList<Movie>();
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    db.execSQL("insert into Online_All (television,name,type,region,year,actor,num,link)values(?,?,?,?,?,?,?,?)", 
            new Object[]{movie.getTelevision(),movie.getName(),movie.getType(),movie.getRegion(),movie.getYear(),
            movie.getActor(),movie.getNum(),movie.getLink()});
    return list;
}

}

//下面这个类是 想数据库添加信息的方法 因为 因为是 将xml 里面的内容解析出来 然后放到一个集合里面 所以 下面这个类 是 遍历集合 然后想数据库添加

public class TraverseSet extends AsyncTask>, List>, List>>{
private String television;
private String name;
private String type;
private String region;
private String year;
private String actor;
private int numint;
private String link;
Context context = MovieService.context1;

public Context getContext() {
    return context;
}


protected List<Map<String, Object>> doInBackground(
        List<Map<String, Object>>... params) {

    System.out.println("进入  遍历集合。。。。。。。。。。。");
    List<Map<String,Object>> typenum = params[0]; 
    System.out.println("有值没有========"+typenum.size());

    for (Iterator<Map<String, Object>> typeiterator = typenum.iterator(); typeiterator.hasNext();) {

        System.out.println("进入  遍历====集合。。。。。。。。。。。");
        Map<String, Object> num = typeiterator.next();

        numint = (Integer) num.get("num");
        name = (String) num.get("name");
        type = (String) num.get("type");
        region = (String) num.get("region");
        actor = (String) num.get("actor");
        year = (String) num.get("year");
        link = (String) num.get("link");
        television = (String) num.get("television");

        System.out.println(" 遍历集合。。。。。完毕。。。开始向数据库中添加。。。");


        System.out.println("得到的television -------"+television+"------");
        System.out.println("得到的name -------"+name+"------");
        System.out.println("得到的type -------"+type+"------");
        System.out.println("得到的region -------"+region+"------");
        System.out.println("得到的year -------"+year+"------");
        System.out.println("得到的actor -------"+actor+"------");
        System.out.println("得到的num -------"+numint+"------");
        System.out.println("得到的link -------"+link+"------");

        /*  Context c = doInBackground("com.android.testpalyer.sql", Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);  
        //载入这个类  
        Class clazz = c.getClassLoader().loadClass("com.android.testpalyer.sql.MovieService");  
        //新建一个实例  
        Object owner = clazz.newInstance();  
        //获取print方法,传入参数并执行  
        Object obj = clazz.getMethod("addOnline_All", MovieService.class);*/

        MovieService ms = new MovieService(this.getContext());
        Movie movie = new Movie();
        movie.setName(name);
        System.out.println("get........."+movie.getName());
        movie.setActor(actor);
        movie.setLink(link);
        movie.setNum(numint);
        movie.setRegion(region);
        movie.setTelevision(television);
        movie.setType(type);
        movie.setYear(year);

        ms.addOnline_All(movie);

        System.out.println("添加数据完毕。。。。。"+ms);

    }
    return typenum;
}

}

[/code]

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答