0

我正在尝试从数据库中获取数据并将其内容以表格格式放置。我在网上查找了各种教程并将我的代码构造如下,但它根本不会以表格格式显示数据。使用 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.
        }
}
4

1 回答 1

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。

于 2013-03-31T18:17:07.370 回答