0
@Override
public void onCreate(Bundle savedInstanceState) {

    /*Intent z=new Intent(this, HomePage.class);
    startActivityForResult(z, REQUEST_CODE);*/

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ctx = this;
    datasource = new CommentsDataSource(this);
    datasource.open();
    refresh();
}
private void refresh(){
    List<Comment> values = datasource.getAllComments();
    List<Comment> value_dif= new ArrayList<Comment>();
    for(int i=0;i<values.size();i++){
        Comment comment=new Comment();
        comment=values.get(i);
        if(comment.getToGive()>comment.getToTake())
            comment.setComment(comment.getComment()+" "+(comment.getToGive()-comment.getToTake()));
        else
            comment.setComment(comment.getComment()+" "+(comment.getToTake()-comment.getToGive()));
            value_dif.add(i,comment);
    }
    ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this,
            android.R.layout.simple_list_item_1, value_dif);
    setListAdapter(adapter);



    ListView listView = (ListView)findViewById(android.R.id.list);
    listView.setAdapter(new ArrayAdapter<Comment>(this, android.R.layout.simple_list_item_1, value_dif) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView textView = (TextView) super.getView(position, convertView, parent);
            List<Comment> values = datasource.getAllComments();
            Comment comment=datasource.getHisaab(values.get(position).getComment());
            if(comment.getToGive()>comment.getToTake()){
                textView.setTextColor(Color.RED);
            }
            else{
                textView.setTextColor(Color.GREEN);
            }
            /*String currentLocation = RouteFinderBookmarksActivity.this.getResources().getString(R.string.Current_Location);
            int textColor = textView.getText().toString().equals(currentLocation) ? R.color.holo_blue : R.color.text_color_btn_holo_dark;
            textView.setTextColor(RouteFinderBookmarksActivity.this.getResources().getColor(textColor));*/
            return textView;
        }
    });

}

如果我在其他活动中使用数据库操作,通过再次创建数据源对象并给出上下文,那么它会显示错误......如果我在这个活动中进行数据库操作,一切运行正常......

4

2 回答 2

0

在您的方法中初始化ListView listView = (ListView)findViewById(android.R.id.list);in youronCreate或 beforesetListAdapter(adapter);refresh()

于 2013-01-21T05:00:13.263 回答
0

尝试使用应用程序的上下文而不是活动的上下文:替换thisthis.getApplicationContext()

于 2013-01-21T05:02:05.597 回答