我按照“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的代码段:
@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方法。任何人都可以帮我确定屏幕显示为空白的原因吗?
答案 0 :(得分:0)
您正在onClick方法的末尾调用setContentView(R.layout.activity_display_result)
,这会将视图重置为xml的内容,从而为您提供activity_display_result.xml中定义的空白屏幕。您需要删除该行并查看它是否有效。
答案 1 :(得分:0)
有几件事:
我自己理解的问题:为什么col
设置为最终?这会不会阻止变量被赋予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);