2012-04-04 53 views
0

因此,我在相对布局中有5列,然后从csv文件读取数据,并将相应列中需要的数据。我给这家代码如下:Android在显示数据时出现问题

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.past_results); 

    final global gs = (global)getApplication(); 
    AlertDialog enter_alert = new AlertDialog.Builder(past_results.this).create(); 
    List<String[]> values = gs.read_csv(); 

    if(values==null){ 
      enter_alert.setTitle("Failed To Read"); 
      enter_alert.setMessage("Unable to Read File"); 
      enter_alert.setButton("Close", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface arg0, int arg1) {} 
      }); 
      enter_alert.show(); 
    return; 
    } 

    else{ 


     for(int i=3;i<values.size();i++){ 
      //loops through all the rows in the file first 3 rows are not needed 
      String date = values.get(i)[0].toString();//get the date value 
      String sn = values.get(i)[9].toString();//get the Unit S/N 
      String fzero = values.get(i)[10].toString();//get the f_zero value 
      String foffset = values.get(i)[11].toString();//get the f offset value 
      String ffactor = values.get(i)[12].toString();//get the f factor value 
      TextView datetext=new TextView(this); 
      TextView sntext=new TextView(this); 
      TextView fzerotext=new TextView(this); 
      TextView foffsettext=new TextView(this); 
      TextView ffactortext=new TextView(this); 

      datetext.setText(date); 
      datetext.setId(1+(i-3)*5); //is this the right way to make an id? 
      datetext.setTextSize(15); 

      sntext.setText(sn); 
      sntext.setId(2+(i-3)*5); 
      sntext.setTextSize(15); 

      fzerotext.setText(fzero); 
      fzerotext.setId(3+(i-3)*5); 
      fzerotext.setTextSize(15); 

      foffsettext.setText(foffset); 
      foffsettext.setId(4+(i-3)*5); 
      foffsettext.setTextSize(15); 

      ffactortext.setText(ffactor); 
      ffactortext.setId(5+(i-3)*5); 
      ffactortext.setTextSize(15); 



      RelativeLayout.LayoutParams dateparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
      RelativeLayout.LayoutParams snparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
      RelativeLayout.LayoutParams fzeroparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
      RelativeLayout.LayoutParams foffsetparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
      RelativeLayout.LayoutParams ffactorparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 

      dateparams.addRule(RelativeLayout.ALIGN_LEFT,R.id.past_date); 
      snparams.addRule(RelativeLayout.ALIGN_LEFT,R.id.past_sn); 
      fzeroparams.addRule(RelativeLayout.ALIGN_LEFT,R.id.past_f_zero); 
      foffsetparams.addRule(RelativeLayout.ALIGN_LEFT,R.id.past_f_offset); 
      ffactorparams.addRule(RelativeLayout.ALIGN_LEFT,R.id.past_f_factor); 

      if (i==3){ 


       dateparams.addRule(RelativeLayout.BELOW,R.id.past_date); 
       snparams.addRule(RelativeLayout.BELOW,R.id.past_sn); 
       fzeroparams.addRule(RelativeLayout.BELOW,R.id.past_f_zero); 
       foffsetparams.addRule(RelativeLayout.BELOW,R.id.past_f_offset); 
       ffactorparams.addRule(RelativeLayout.BELOW,R.id.past_f_factor); 
      } 
      else{ 
       dateparams.addRule(RelativeLayout.BELOW,1+(i-3)*5); 
       snparams.addRule(RelativeLayout.BELOW,2+(i-3)*5); 
       fzeroparams.addRule(RelativeLayout.BELOW,3+(i-3)*5); 
       foffsetparams.addRule(RelativeLayout.BELOW,4+(i-3)*5); 
       ffactorparams.addRule(RelativeLayout.BELOW,5+(i-3)*5); 
      } 
      datetext.setLayoutParams(dateparams); 
      sntext.setLayoutParams(snparams); 
      fzerotext.setLayoutParams(fzeroparams); 
      foffsettext.setLayoutParams(foffsetparams); 
      ffactortext.setLayoutParams(ffactorparams); 

     }//end of for loop 
    }//end of if 


    }; 

}

现在只是正在显示的结果没有让我有一个怀疑,这是由于我如何设置参数时,我是调试我可以看到列表中的值。你看到我在做什么错了吗?

+0

您是否考虑过使用XML布局? – JRaymond 2012-04-04 18:16:35

+0

我有,但我不确定当我需要在每列中添加未知数量的值时,我会怎么做 – yawnobleix 2012-04-04 18:18:19

+0

你在哪里添加新的TextView到你的布局? – Simon 2012-04-04 18:25:43

回答

0

之所以什么都没有显示的是,你在呼唤

setContentView(R.layout.past_results); 

之初,这是告诉你要显示地处past_results.xml文件中的布局系统。

如果您想要将新创建的TextViews放到屏幕上,您必须从该xml文件获取对父布局的引用。然后,你作出这样

relLayout.addView(datetex); 
relLayout.addView(sntext); 
etc... 

然而,随着JRaymond指出,要完成这样的事情会以某种您创建专门究竟要如何显示数据适配器的首选方法调用。它会消除很多试图为所有个人视图创建硬编码的麻烦,并“手工”填充它们。

相关问题