2013-01-21 26 views
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; 
     } 
    }); 

} 

零指示字例外,如果我通過再次創建具有給予上下文此DataSource對象使用在其他活動的數據庫操作,它示出了錯誤.... ..如果我做數據庫操作S IN本次活動一切都正常運行.....上下文中,比主要,給在數據庫操作

回答

0

初始化您onCreate或之前setListAdapter(adapter);ListView listView = (ListView)findViewById(android.R.id.list);refresh()方法。

+0

謝謝你們所有的答案取代this ......這是愚蠢的錯誤由我...我沒有打開數據庫....: - | – OLDRomeo

0

嘗試使用應用程序的上下文,而不是活動的背景:與this.getApplicationContext()