问题描述
我试图从数据库中提取数据,并把它的内容以表格形式。我在网上查找各种教程和结构性我的code如下,但不会以表格形式显示数据在所有。当使用一个TextView工作正常。这里是我的code,请知道我要去哪里错了。遇到不强制关闭,只是数据不会显示!
包com.example.callandmessagemanager;进口android.app.Activity;
进口android.database.Cursor;
进口android.graphics.Color;
进口android.os.Bundle;
进口android.view.ViewGroup.LayoutParams;
进口android.widget.LinearLayout;
进口android.widget.ScrollView;
进口android.widget.TableLayout;
进口android.widget.TableRow;
进口android.widget.TextView;
进口android.widget.Toast;
公共类ViewEncryptedMessages延伸活动{ 的LinearLayout L,
曲儿q;
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.viewencryptedmessages);
L =(的LinearLayout)findViewById(R.id.LinearLayout1);
TableLayout TL =(TableLayout)findViewById(R.id.TL);
Q =新曲儿(本);
q.open();
游标c1;
C1 = q.fetchAllTodos();
长C0 = 1;
诠释计数= c1.getCount(); 如果(C1!= NULL)
{
做
{
的TextView吨; 光标C = q.fetchTodo(C0);
字符串消息= c.getString(c.getColumnIndexOrThrow(quer.KEY_MESSAGE));
串号= c.getString(c.getColumnIndexOrThrow(quer.KEY_NUMBER)); TR的TableRow =新的TableRow(本);
TextView的TV1 =新的TextView(本);
TextView中TV2 =新的TextView(本);
CreateView的(TR,TV1,消息);
CreateView的(TR,TV2,数);
TL.addView(TR); / *的LayoutParams LA =新的LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
T =新的TextView(本);
t.setLayoutParams(LA); t.setText(信息+\\ t+号);
L.addView(T); * /
C0 ++;
}而(C0< = c1.getCount());
q.close();
}
其他
{
q.close();
Toast.makeText(这一点,不加密消息尚未收到,Toast.LENGTH_SHORT).show();
}
} 公共无效CreateView的(TR的TableRow,TextView的T,串可视数据){
t.setText(可视数据);
//调整的TextView的调性质
t.setLayoutParams(新的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); //添加的TextView排。
}
}
试试这个
在你的 CreateView的
法
替换该
<$c$c>t.setLayoutParams(newLayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));$c$c>
本
t.setLayoutParams(新TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));
当您添加布局PARAMS同时加入以表,你必须确保你的布局PARAMS是的TableRow的。
I'm trying to fetch data from a database and placing its content in a Table format. I've looked up various tutorials online and structured my code as follows, but it wouldn't show the data in a table format at all. Works fine when a TextView is used. Here's my code, please know where I'm going wrong. No force close is encountered, just that the data won't display!
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.
}
}
Try this
In your createView
method
replace this
t.setLayoutParams(newLayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
with this
t.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));
Whenever you are adding layout params while adding view to Table, You have to make sure that your layout params are of TableRow.
这篇关于无法动态填充表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!