Android在显示数据时遇到问题

时间:2012-04-04 18:09:00

标签: android layout relative

所以我在相对布局中有5列,然后我从csv文件中读取数据并将我需要的数据放在相应的列中。我的代码如下:

 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


    };

}

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

1 个答案:

答案 0 :(得分:0)

没有显示任何内容的原因是您正在调用

setContentView(R.layout.past_results);

一开始,这告诉系统您要显示位于past_results.xml文件中的布局。

如果要将新创建的TextView放到屏幕上,则必须从该xml文件中获取对父布局的引用。然后你就这样打电话

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

然而,正如JRaymond指出的那样,完成这样的事情的首选方法是使用某种专门创建的适配器来显示数据的确切方式。它会消除很多麻烦,试图为所有个人观点硬编码并“手动”填充它们