我正在尝试从数据库中获取数据并将其内容放在表格格式中。我已经在线查找了各种教程,并按如下方式构建了我的代码,但它根本不会以表格格式显示数据。使用TextView时工作正常。这是我的代码,请知道我哪里出错了。没有遇到强制关闭,只是数据不会显示!
package com.example.callandmessagemanager;
import android.app.Activity;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
public class ViewEncryptedMessages extends Activity{
LinearLayout L;
quer q;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewencryptedmessages);
L=(LinearLayout)findViewById(R.id.LinearLayout1);
TableLayout TL = (TableLayout)findViewById(R.id.TL);
q = new quer(this);
q.open();
Cursor c1;
c1=q.fetchAllTodos();
long c0=1;
int count = c1.getCount();
if(c1!=null)
{
do
{
TextView t;
Cursor c=q.fetchTodo(c0);
String message = c.getString(c.getColumnIndexOrThrow(quer.KEY_MESSAGE));
String number = c.getString(c.getColumnIndexOrThrow(quer.KEY_NUMBER));
TableRow tr = new TableRow(this);
TextView tv1 = new TextView(this);
TextView tv2 = new TextView(this);
createView(tr, tv1, message);
createView(tr, tv2, number);
TL.addView(tr);
/*LayoutParams la=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
t=new TextView(this);
t.setLayoutParams(la);
t.setText(message+" \t"+number);
L.addView(t);*/
c0++;
}while(c0<=c1.getCount());
q.close();
}
else
{
q.close();
Toast.makeText(this, "No Encrypted Messages Received yet", Toast.LENGTH_SHORT).show();
}
}
public void createView(TableRow tr, TextView t, String viewdata) {
t.setText(viewdata);
//adjust the porperties of the textView
t.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
t.setTextColor(Color.DKGRAY);
t.setBackgroundColor(Color.CYAN);
t.setPadding(20, 0, 0, 0);
tr.setPadding(0, 1, 0, 1);
//tr.setBackgroundColor(Color.BLACK);
tr.addView(t); // add TextView to row.
}
}
答案 0 :(得分:2)
试试这个
使用createView
方法
替换此
t.setLayoutParams(newLayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
用这个
t.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));
每当您在向Table添加视图时添加布局参数时,您必须确保您的布局参数是TableRow。