我正在Android应用中成功检索我的sqlite表数据。然而,它有一些小问题,我无法弄清楚。
代码段:
int flag=1;
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
String day = c.getString(c.getColumnIndex("day"));
String slot1 = c.getString(c.getColumnIndex("7:30-9:10AM"));
String slot2 = c.getString(c.getColumnIndex("9:20-11:00AM"));
String slot3 = c.getString(c.getColumnIndex("11:10-12:50PM"));
String slot4 = c.getString(c.getColumnIndex("1:40-3:20PM"));
String slot5 = c.getString(c.getColumnIndex("3:30-5:00PM"));
TableRow tr=new TableRow(viewtimetable.this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
if(flag==1)
{
TextView col1=new TextView(viewtimetable.this);
col1.setText("Day");
col1.setTextColor(Color.BLUE);
col1.setTextSize(15);
tr.addView(col1);
TextView col2=new TextView(viewtimetable.this);
col2.setPadding(10, 0, 0, 0);
col2.setTextSize(15);
col2.setText("7:30-9:10AM");
col2.setTextColor(Color.BLUE);
tr.addView(col2);
TextView col3=new TextView(viewtimetable.this);
col3.setPadding(10, 0, 0, 0);
col3.setText("9:20-11:00AM");
col3.setTextColor(Color.BLUE);
col3.setTextSize(15);
tr.addView(col3);
TextView col4=new TextView(viewtimetable.this);
col4.setPadding(10, 0, 0, 0);
col4.setText("11:10-12:50PM");
col4.setTextColor(Color.BLUE);
col4.setTextSize(15);
tr.addView(col4);
TextView col5=new TextView(viewtimetable.this);
col5.setPadding(10, 0, 0, 0);
col5.setText("1:40-3:20PM");
col5.setTextColor(Color.BLUE);
col5.setTextSize(15);
tr.addView(col5);
TextView col6=new TextView(viewtimetable.this);
col6.setPadding(10, 0, 0, 0);
col6.setText("3:30-5:00PM");
col6.setTextColor(Color.BLUE);
col6.setTextSize(15);
tr.addView(col6);
tv.addView(tr);
final View vline = new View(viewtimetable.this);
vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 2));
vline.setBackgroundColor(Color.BLUE);
tv.addView(vline);
flag=0;
}
else
{
TextView b=new TextView(viewtimetable.this);
//String stime=json_data.getString("day");
b.setText(day);
b.setTextColor(Color.RED);
b.setTextSize(15);
tr.addView(b);
TextView b1=new TextView(viewtimetable.this);
TextView b2=new TextView(viewtimetable.this);
TextView b3=new TextView(viewtimetable.this);
TextView b4=new TextView(viewtimetable.this);
TextView b5=new TextView(viewtimetable.this);
b1.setPadding(10, 0, 0, 0);
b1.setTextSize(15);
b1.setTextColor(Color.RED);
b2.setPadding(10, 0, 0, 0);
b2.setTextColor(Color.RED);
b2.setTextSize(15);
b3.setPadding(10, 0, 0, 0);
b3.setTextColor(Color.RED);
b3.setTextSize(15);
//tr.addView(b3);
b4.setPadding(10, 0, 0, 0);
b4.setTextColor(Color.RED);
b4.setTextSize(15);
//tr.addView(b4);
b5.setPadding(10, 0, 0, 0);
b5.setTextColor(Color.RED);
b5.setTextSize(15);
//tr.addView(b5);
if(day.equals("Monday"))
{
if(slot1 != null){
b1.setText(slot1);
}
if(slot2 != null)
{
b2.setText(slot2);
}
if(slot3 != null)
{
b3.setText(slot3);
}
if(slot4 != null)
{
b4.setText(slot4);
}
if(slot5 != null)
{
b5.setText(slot5);
}
}
else if(day.equals("Tuesday"))
{
if(slot1 != null){
b1.setText(slot1);
}
if(slot2 != null)
{
b2.setText(slot2);
}
if(slot3 != null)
{
b3.setText(slot3);
}
if(slot4 != null)
{
b4.setText(slot4);
}
if(slot5 != null)
{
b5.setText(slot5);
}
}
else if(day.equals("Wednesday"))
{
if(slot1 != null){
b1.setText(slot1);
}
if(slot2 != null)
{
b2.setText(slot2);
}
if(slot3 != null)
{
b3.setText(slot3);
}
if(slot4 != null)
{
b4.setText(slot4);
}
if(slot5 != null)
{
b5.setText(slot5);
}
}
else if(day.equals("Thursday"))
{
if(slot1 != null){
b1.setText(slot1);
}
if(slot2 != null)
{
b2.setText(slot2);
}
if(slot3 != null)
{
b3.setText(slot3);
}
if(slot4 != null)
{
b4.setText(slot4);
}
if(slot5 != null)
{
b5.setText(slot5);
}
}
else if(day.equals("Friday"))
{
if(slot1 != null){
b1.setText(slot1);
}
if(slot2 != null)
{
b2.setText(slot2);
}
if(slot3 != null)
{
b3.setText(slot3);
}
if(slot4 != null)
{
b4.setText(slot4);
}
if(slot5 != null)
{
b5.setText(slot5);
}
}
else if(day.equals("Saturday"))
{
if(slot1 != null){
b1.setText(slot1);
}
if(slot2 != null)
{
b2.setText(slot2);
}
if(slot3 != null)
{
b3.setText(slot3);
}
if(slot4 != null)
{
b4.setText(slot4);
}
if(slot5 != null)
{
b5.setText(slot5);
}
}
tr.addView(b1);
tr.addView(b2);
tr.addView(b3);
tr.addView(b4);
tr.addView(b5);
tv.addView(tr);
结果:
a http://www.4shared.com/download/mhKMKta4ba/random4.png
问题:
我想知道为什么星期五的行没有显示出来?
P.S:由于我已经成功检索了除星期五数据之外的所有必需数据,因此我猜测上述代码片段存在一些小问题。
修改:
问题出在else块中。由于第一个数据是星期五,而且第一次迭代没有执行其他块,因此出现了问题。
尝试删除else阻止并生成异常:
指定的孩子已经有了父母。你必须先在孩子的父母身上调用removeView()。
解决方案:
我所做的是在循环内创建了一个新的Tablerow
,然后将views
添加到该行。
最终更正代码:
TableLayout tv=(TableLayout) findViewById(R.id.table);
tv.removeAllViewsInLayout();
int flag=1;
TableRow tr=new TableRow(viewtimetable.this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView col1=new TextView(viewtimetable.this);
col1.setText("Day");
col1.setTextColor(Color.BLUE);
col1.setTextSize(15);
tr.addView(col1);
TextView col2=new TextView(viewtimetable.this);
col2.setPadding(10, 0, 0, 0);
col2.setTextSize(15);
col2.setText("7:30-9:10AM");
col2.setTextColor(Color.BLUE);
tr.addView(col2);
TextView col3=new TextView(viewtimetable.this);
col3.setPadding(10, 0, 0, 0);
col3.setText("9:20-11:00AM");
col3.setTextColor(Color.BLUE);
col3.setTextSize(15);
tr.addView(col3);
TextView col4=new TextView(viewtimetable.this);
col4.setPadding(10, 0, 0, 0);
col4.setText("11:10-12:50PM");
col4.setTextColor(Color.BLUE);
col4.setTextSize(15);
tr.addView(col4);
TextView col5=new TextView(viewtimetable.this);
col5.setPadding(10, 0, 0, 0);
col5.setText("1:40-3:20PM");
col5.setTextColor(Color.BLUE);
col5.setTextSize(15);
tr.addView(col5);
TextView col6=new TextView(viewtimetable.this);
col6.setPadding(10, 0, 0, 0);
col6.setText("3:30-5:00PM");
col6.setTextColor(Color.BLUE);
col6.setTextSize(15);
tr.addView(col6);
tv.addView(tr);
final View vline = new View(viewtimetable.this);
vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 2));
vline.setBackgroundColor(Color.BLUE);
tv.addView(vline);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
String day = c.getString(c.getColumnIndex(dba.KEY_DAY));
String slot1 = c.getString(c.getColumnIndex("7:30-9:10AM"));
String slot2 = c.getString(c.getColumnIndex("9:20-11:00AM"));
String slot3 = c.getString(c.getColumnIndex("11:10-12:50PM"));
String slot4 = c.getString(c.getColumnIndex("1:40-3:20PM"));
String slot5 = c.getString(c.getColumnIndex("3:30-5:00PM"));
TableRow tr1=new TableRow(viewtimetable.this);
tr1.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView b=new TextView(viewtimetable.this);
b.setText(day);
b.setTextColor(Color.RED);
b.setTextSize(15);
tr1.addView(b);
TextView b1=new TextView(viewtimetable.this);
TextView b2=new TextView(viewtimetable.this);
TextView b3=new TextView(viewtimetable.this);
TextView b4=new TextView(viewtimetable.this);
TextView b5=new TextView(viewtimetable.this);
b1.setPadding(10, 0, 0, 0);
b1.setTextSize(15);
b1.setTextColor(Color.RED);
b2.setPadding(10, 0, 0, 0);
b2.setTextColor(Color.RED);
b2.setTextSize(15);
b3.setPadding(10, 0, 0, 0);
b3.setTextColor(Color.RED);
b3.setTextSize(15);
b4.setPadding(10, 0, 0, 0);
b4.setTextColor(Color.RED);
b4.setTextSize(15);
b5.setPadding(10, 0, 0, 0);
b5.setTextColor(Color.RED);
b5.setTextSize(15);
if(day.equals("Friday"))
{
if(slot1 != null){
b1.setText(slot1);
}
if(slot2 != null)
{
b2.setText(slot2);
}
if(slot3 != null)
{
b3.setText(slot3);
}
if(slot4 != null)
{
b4.setText(slot4);
}
if(slot5 != null)
{
b5.setText(slot5);
}
}
else if(day.equals("Monday"))
{
if(slot1 != null){
b1.setText(slot1);
}
if(slot2 != null)
{
b2.setText(slot2);
}
if(slot3 != null)
{
b3.setText(slot3);
}
if(slot4 != null)
{
b4.setText(slot4);
}
if(slot5 != null)
{
b5.setText(slot5);
}
}
else if(day.equals("Tuesday"))
{
if(slot1 != null){
b1.setText(slot1);
}
if(slot2 != null)
{
b2.setText(slot2);
}
if(slot3 != null)
{
b3.setText(slot3);
}
if(slot4 != null)
{
b4.setText(slot4);
}
if(slot5 != null)
{
b5.setText(slot5);
}
}
else if(day.equals("Wednesday"))
{
if(slot1 != null){
b1.setText(slot1);
}
if(slot2 != null)
{
b2.setText(slot2);
}
if(slot3 != null)
{
b3.setText(slot3);
}
if(slot4 != null)
{
b4.setText(slot4);
}
if(slot5 != null)
{
b5.setText(slot5);
}
}
else if(day.equals("Thursday"))
{
if(slot1 != null){
b1.setText(slot1);
}
if(slot2 != null)
{
b2.setText(slot2);
}
if(slot3 != null)
{
b3.setText(slot3);
}
if(slot4 != null)
{
b4.setText(slot4);
}
if(slot5 != null)
{
b5.setText(slot5);
}
}
else if(day.equals("Saturday"))
{
if(slot1 != null){
b1.setText(slot1);
}
if(slot2 != null)
{
b2.setText(slot2);
}
if(slot3 != null)
{
b3.setText(slot3);
}
if(slot4 != null)
{
b4.setText(slot4);
}
if(slot5 != null)
{
b5.setText(slot5);
}
}
tr1.addView(b1);
tr1.addView(b2);
tr1.addView(b3);
tr1.addView(b4);
tr1.addView(b5);
tv.addView(tr1);
答案 0 :(得分:1)
我认为发生的事情是第一次(当前行是星期五)你有flag = 1来表示创建列。但是你的结构是这样的:
while GetNextRow
if flag = 1
create cols, set flag = 0
else
process row
endif
loop
第一次,你得到星期五,创建列然后循环。 在第二次通过你得到星期六并处理行。等等。
你需要拿出别的东西。你也可以在循环之前放置创建列逻辑:那就是如果根本没有数据,你仍然会得到一个定义了列的屏幕并且没有数据,而不是你得到的没有数据的空白时刻。
干杯 -
答案 1 :(得分:0)
解决方案:
@simon和@Andrew建议我摆脱if else block
。然后我在for loop
里面做了什么我创建了一个新的Tablerow
,然后添加了views
到那一行。
最终更正代码:
TableLayout tv=(TableLayout) findViewById(R.id.table);
tv.removeAllViewsInLayout();
int flag=1;
TableRow tr=new TableRow(viewtimetable.this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView col1=new TextView(viewtimetable.this);
col1.setText("Day");
col1.setTextColor(Color.BLUE);
col1.setTextSize(15);
tr.addView(col1);
TextView col2=new TextView(viewtimetable.this);
col2.setPadding(10, 0, 0, 0);
col2.setTextSize(15);
col2.setText("7:30-9:10AM");
col2.setTextColor(Color.BLUE);
tr.addView(col2);
TextView col3=new TextView(viewtimetable.this);
col3.setPadding(10, 0, 0, 0);
col3.setText("9:20-11:00AM");
col3.setTextColor(Color.BLUE);
col3.setTextSize(15);
tr.addView(col3);
TextView col4=new TextView(viewtimetable.this);
col4.setPadding(10, 0, 0, 0);
col4.setText("11:10-12:50PM");
col4.setTextColor(Color.BLUE);
col4.setTextSize(15);
tr.addView(col4);
TextView col5=new TextView(viewtimetable.this);
col5.setPadding(10, 0, 0, 0);
col5.setText("1:40-3:20PM");
col5.setTextColor(Color.BLUE);
col5.setTextSize(15);
tr.addView(col5);
TextView col6=new TextView(viewtimetable.this);
col6.setPadding(10, 0, 0, 0);
col6.setText("3:30-5:00PM");
col6.setTextColor(Color.BLUE);
col6.setTextSize(15);
tr.addView(col6);
tv.addView(tr);
final View vline = new View(viewtimetable.this);
vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 2));
vline.setBackgroundColor(Color.BLUE);
tv.addView(vline);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
String day = c.getString(c.getColumnIndex(dba.KEY_DAY));
String slot1 = c.getString(c.getColumnIndex("7:30-9:10AM"));
String slot2 = c.getString(c.getColumnIndex("9:20-11:00AM"));
String slot3 = c.getString(c.getColumnIndex("11:10-12:50PM"));
String slot4 = c.getString(c.getColumnIndex("1:40-3:20PM"));
String slot5 = c.getString(c.getColumnIndex("3:30-5:00PM"));
TableRow tr1=new TableRow(viewtimetable.this);
tr1.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView b=new TextView(viewtimetable.this);
b.setText(day);
b.setTextColor(Color.RED);
b.setTextSize(15);
tr1.addView(b);
TextView b1=new TextView(viewtimetable.this);
TextView b2=new TextView(viewtimetable.this);
TextView b3=new TextView(viewtimetable.this);
TextView b4=new TextView(viewtimetable.this);
TextView b5=new TextView(viewtimetable.this);
b1.setPadding(10, 0, 0, 0);
b1.setTextSize(15);
b1.setTextColor(Color.RED);
b2.setPadding(10, 0, 0, 0);
b2.setTextColor(Color.RED);
b2.setTextSize(15);
b3.setPadding(10, 0, 0, 0);
b3.setTextColor(Color.RED);
b3.setTextSize(15);
b4.setPadding(10, 0, 0, 0);
b4.setTextColor(Color.RED);
b4.setTextSize(15);
b5.setPadding(10, 0, 0, 0);
b5.setTextColor(Color.RED);
b5.setTextSize(15);
if(day.equals("Friday"))
{
if(slot1 != null){
b1.setText(slot1);
}
if(slot2 != null)
{
b2.setText(slot2);
}
if(slot3 != null)
{
b3.setText(slot3);
}
if(slot4 != null)
{
b4.setText(slot4);
}
if(slot5 != null)
{
b5.setText(slot5);
}
}
else if(day.equals("Monday"))
{
if(slot1 != null){
b1.setText(slot1);
}
if(slot2 != null)
{
b2.setText(slot2);
}
if(slot3 != null)
{
b3.setText(slot3);
}
if(slot4 != null)
{
b4.setText(slot4);
}
if(slot5 != null)
{
b5.setText(slot5);
}
}
else if(day.equals("Tuesday"))
{
if(slot1 != null){
b1.setText(slot1);
}
if(slot2 != null)
{
b2.setText(slot2);
}
if(slot3 != null)
{
b3.setText(slot3);
}
if(slot4 != null)
{
b4.setText(slot4);
}
if(slot5 != null)
{
b5.setText(slot5);
}
}
else if(day.equals("Wednesday"))
{
if(slot1 != null){
b1.setText(slot1);
}
if(slot2 != null)
{
b2.setText(slot2);
}
if(slot3 != null)
{
b3.setText(slot3);
}
if(slot4 != null)
{
b4.setText(slot4);
}
if(slot5 != null)
{
b5.setText(slot5);
}
}
else if(day.equals("Thursday"))
{
if(slot1 != null){
b1.setText(slot1);
}
if(slot2 != null)
{
b2.setText(slot2);
}
if(slot3 != null)
{
b3.setText(slot3);
}
if(slot4 != null)
{
b4.setText(slot4);
}
if(slot5 != null)
{
b5.setText(slot5);
}
}
else if(day.equals("Saturday"))
{
if(slot1 != null){
b1.setText(slot1);
}
if(slot2 != null)
{
b2.setText(slot2);
}
if(slot3 != null)
{
b3.setText(slot3);
}
if(slot4 != null)
{
b4.setText(slot4);
}
if(slot5 != null)
{
b5.setText(slot5);
}
}
tr1.addView(b1);
tr1.addView(b2);
tr1.addView(b3);
tr1.addView(b4);
tr1.addView(b5);
tv.addView(tr1);