Android表行以编程方式添加是不可见的

时间:2014-08-05 18:07:05

标签: java android

以下是动态添加TableRows的代码片段

TableLayout tl =(TableLayout)findViewById(R.id.MyTable);

    try
    {
        AssetManager assetManager = getAssets();
        InputStream ims = assetManager.open("TempData.xml");
        InputStreamReader reader = new InputStreamReader(ims);
        XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
         parser.setInput(reader);
         parser.require(XmlPullParser.START_DOCUMENT, null, null);

         int eventType = parser.getEventType();

         int count = 0;
         while(eventType != XmlPullParser.END_DOCUMENT) {
            String pName = parser.getName();
            switch(eventType) {
               case XmlPullParser.START_TAG:
                  if(pName.equals("name")) {

              String msg = parser.nextText();
                     //Update the Table with this data
                     TableRow tr = new TableRow(this);
                     tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
                     TextView tv = new TextView(this);
                    tv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

                     tv.setText(msg);
                     tv.setVisibility(View.VISIBLE);
                    tr.addView(tv);
                     tr.setVisibility(View.VISIBLE);
                     tl.addView(tr);

                //This below code is just to confirm that i am readin the correct data from XML. It is correct btw.
                    Context context = getApplicationContext();
                    CharSequence text = "data is : " + msg;
                    int duration = Toast.LENGTH_SHORT;
                    Toast toast = Toast.makeText(context, text, duration);
                    toast.show();
                    //Thread.sleep(2);
                  break;
                  }

               default:
                  break;
            } 

            eventType = parser.next();
         }// parse next and gen
    }
    catch(Exception e){
        String ExceptionMessage = e.getLocalizedMessage();
        Context context = getApplicationContext();
        CharSequence text = "Error is : " + ExceptionMessage;
        int duration = Toast.LENGTH_LONG;
        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
    }

有人可以告诉我为什么运行这个,我在Android APP上看不到任何TextViews。 没有异常,但仍然是空白输出。 我在这里错过了什么吗?

1 个答案:

答案 0 :(得分:0)

尝试在添加新行后调用invalidate()。