【问题标题】:How to display database table records using TableLayout如何使用 TableLayout 显示数据库表记录
【发布时间】:2013-11-21 07:59:48
【问题描述】:

我点击“How to get data from database in my android table view as columns”链接解决了我的问题 但出现没有数据的空白屏幕。单击按钮后,我编写了代码以从 MS SQL DB 中获取记录。

我的 XML,用于布局目的,activity_display_result.xml:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/tab" >
</TableLayout>

以及MainActivity.java中的代码sn-p:

@SuppressLint("NewApi") @Override
public void onCreate(Bundle b)
{
    super.onCreate(b);
    setContentView(R.layout.activity_main);

    if( Build.VERSION.SDK_INT >= 9)
    {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy); 
    }

    initilize();

    button1.setOnClickListener(new OnClickListener() 
    {
        public void onClick(View v) 
        {
            List <Map<String,String>> dataList  = querySQL(e1.getText().toString(),e2.getText().toString(),e.getText().toString());

            StringBuilder builder = new StringBuilder();
            for (int i=0;i<dataList.size();i++)
            {               
               Log.i("onClick Method()", "UserName : "+dataList.get(i).get("UserName"));
               Log.i("onClick Method()", "Password : "+dataList.get(i).get("Password"));
               builder.append(dataList.get(i).get("UserName")).append(";").
               append(dataList.get(i).get("Password")).append("_");
            }

            builder.toString();

            String st = new String(builder);
            String[] rows =st.split("_");
            setContentView(R.layout.activity_display_result);
            TableLayout tableLayout =(TableLayout) findViewById(R.id.tab);
            tableLayout.removeAllViews();

            for(int i=0;i<rows.length;i++)
            {
                String row  = rows[i];
                TableRow tableRow = new TableRow(getApplicationContext());
                                                tableRow.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

                final String[] cols = row.split(";");

                Log.i("MainActivity ","Column length :"+cols.length);

                //Handler handler = null;

                for (int j = 0; j < cols.length; j++) 
                {             
                    final String col = cols[j];                                 
                    TextView TextView11 = new TextView(getApplicationContext());
                    TextView11.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
                    TextView11.setTextColor(color.black);
                    //TextView11.setText(String.format("%7s", col));
                    TextView11.setText(col);
                    tableRow.addView(TextView11);
                }

                tableLayout.addView(tableRow);
                setContentView(R.layout.activity_display_result);
            }
        }
    });
}

全部声明

public void declere()
{
    e = (EditText)findViewById(R.id.editText3);
    e1 = (EditText)findViewById(R.id.editText1);
    e2 = (EditText)findViewById(R.id.editText2);
    button1=(Button)findViewById(R.id.button1);
}

//initializing 
public void initilize()
{
    Log.i("initilize() Method", "initilize() Method is called..Now Calling declere() method");
    declere();
    Log.i("initilize() Method", "declere() method is called ");
    Log.i("initilize() Method", "Before Calling CONN() method..");
    connect=CONN("sa","C!@C2@!2","RequestCenter");
    Log.i("initilize() Method", "CONN() method called Successfully..");

 }

我还添加了一种用于获取连接的 CONN 方法。谁能帮我确定为什么屏幕显示为空白?

【问题讨论】:

    标签: android


    【解决方案1】:

    您在 onClick 方法的末尾调用 setContentView(R.layout.activity_display_result),这会将视图重置为 xml 的内容,从而为您提供在 activity_display_result.xml 中定义的空白屏幕。您需要删除该行并查看它是否有效。

    【讨论】:

    • 我删除了那行,但在那个新窗口上仍然没有结果
    【解决方案2】:

    有几点:

    1. 您继续重置 setContentView(R.layout.activity_display_result);多次(通过循环)。有原因吗?一旦所有控件都放置在布局中,您可能只需要在最后设置一次。我已经把它移到了我认为它应该在下面的代码中的位置。如果这不起作用,请重新考虑按照上述 2Dee 将其取出
    2. 一个我自己理解的问题:为什么col设置为Final?这不会阻止为变量分配 cols 数组中的下一个值吗?谢谢!

          for(int i=0;i<rows.length;i++){
              String row  = rows[i];
              TableRow tableRow = new TableRow(getApplicationContext());
                                              tableRow.setLayoutParams(new     TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
      
              final String[] cols = row.split(";");
      
              Log.i("MainActivity ","Column length :"+cols.length);
      
              //Handler handler = null;
      
              for (int j = 0; j < cols.length; j++) 
              {             
                  final String col = cols[j];                                 
                  TextView TextView11 = new TextView(getApplicationContext());
                  TextView11.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
                  TextView11.setTextColor(color.black);
                  //TextView11.setText(String.format("%7s", col));
                  TextView11.setText(col);
                  tableRow.addView(TextView11);
      
              }
      
             tableLayout.addView(tableRow);
          }
          setContentView(R.layout.activity_display_result);
      

    【讨论】:

      猜你喜欢
      • 2012-08-19
      • 2015-08-12
      • 1970-01-01
      • 2014-06-23
      • 2014-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多