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.