New to android programming.
I have a layout XML, to which I want to add a list (with items from a database), formatted as a table.
Here’s the code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_cat);
DatabaseHandler db = new DatabaseHandler(this);
List<Category> allCategories = db.getAllCategories();
//Get the main layout from XML
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.edit_cat);
//Create table layout for the categories
TableLayout catList = new TableLayout(this);
//Iterate the categories, and organize in tables
for (Category category : allCategories) {
TableRow tr = new TableRow(this);
//Display category name
TextView name = new TextView(this);
name.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
name.setText(category.getName());
//Display category type
TextView type = new TextView(this);
type.setText(category.getType());
type.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//Display edit button
Button btnEdit = new Button(this);
btnEdit.setText(getResources().getString(R.string.edit));
btnEdit.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//Display delete button
Button btnDelete = new Button(this);
btnDelete.setText(getResources().getString(R.string.delete));
btnDelete.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr.addView(name);
tr.addView(type);
tr.addView(btnEdit);
tr.addView(btnDelete);
catList.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
mainLayout.addView(catList);
}
This does not add anything to the layout. Any ideas why this does not work?
EDIT: Added the xml code here:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/edit_cat"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView style="@style/pageHeader"
android:text="@string/catEditor"/>
<Button
android:id="@+id/btnNewCategory"
android:text="@string/newCategory"
android:textSize="11pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onBtnClicked"/>
<Button
android:id="@+id/back"
android:text="@string/back"
android:textSize="11pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onBtnClicked"/>
</LinearLayout>
Ok, I think I have an answer for you.
You need to be more specific when you are specifying LayoutParams. While it doesn’t throw a compile exception, if you don’t specify the correct parent class of the LayoutParams, nothing will show up.
When you specify layout params, you need to use the class of the container where the object is going. If you are setting the layout paramters of a textview that is going inside a LinearLayout, you would use
new LinearLayout.LayoutParams(...)similarily if you are creating a textview to go inside a TableRow, you need to useTableRow.LayoutParams(...)Below is the full code with minor modifications because I didn’t have a db setup.