picture
Hi I have developed an app which uses the MySQL database. I have a code which retrieves the data and am trying to display the data in the form of a database.
This is my java code which accesses the database and try to print it in the form of a table dynamically.
My problem is the app force closes when I execute on a phone.
I got the following error.
This is my php code.
Use TableLayout to display your data in the form of tables.Also to connect to a mySQL database, you would need to build a php script which connects to the database.You have not shown your php script,so please provide that.
java.lang.ClassCastException:
"Thrown to indicate that the code has attempted to cast an object to a subclass
of which it is not an instance."
You have casted a TableLayout as TextView in your java class. So, change the TableLayout to TextView in your XML layout.
[Edit]
Got it Now:
Create a new TextView for the error to be displayed. Then change the following line:
error = (TextView) findViewById(R.id.myErrorView);
[Edit 2]
Example for TableLayout (not compiled)
XML Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/myHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TableLayout Example"
/>
<ScrollView android:layout_width="fill_parent" android:layout_height="wrap_content">
<TableLayout
android:id="#+id/tablelayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TableRow android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginBottom="2dp" android:layout_gravity="center_horizontal">
<TextView android:id="#+id/tabletv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ID" android:textStyle="bold" android:layout_gravity="center_horizontal"/>
<TextView android:id="#+id/tabletv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Name" android:textStyle="bold" android:layout_gravity="center_horizontal"/>
<TextView android:id="#+id/tabletv3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Category" android:textStyle="bold" android:layout_gravity="center_horizontal"/>
<TextView android:id="#+id/tabletv4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Category" android:textStyle="bold" android:layout_gravity="center_horizontal"/>
</TableRow>
</TableLayout>
</ScrollView>
</LinearLayout>
Java Class
super.onCreate(savedInstanceState);
setContentView(R.layout.myLayout);
TableLayout table = (TableLayout) findViewById(R.id.tablelayout1);
TextView heading = (TextView) findViewById(R.id.myHeading);
// Your other codes
//////////////////////
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
TableRow row = new TableRow(this);
TableLayout.LayoutParams tableRowParams= new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
tableRowParams.setMargins(0, 0, 0, 2);
row.setGravity(Gravity.CENTER_HORIZONTAL);
row.setLayoutParams(tableRowParams);
TextView t1 = new TextView(this);
TextView t2 = new TextView(this);
TextView t3 = new TextView(this);
TextView t4 = new TextView(this);
t1.setText(""+json_data.getInt("prod_id"));
t2.setText(json_data.getString("prod_name"));
t3.setText(json_data.getString("prod_category"));
t4.setText(""+json_data.getDouble("prod_cost"));
t1.setGravity(Gravity.CENTER_HORIZONTAL);
t2.setGravity(Gravity.CENTER_HORIZONTAL);
t3.setGravity(Gravity.CENTER_HORIZONTAL);
t4.setGravity(Gravity.CENTER_HORIZONTAL);
row.addView(t1);
row.addView(t2);
row.addView(t3);
row.addView(t4);
table.addView(row);
}
Hope this helps!
Related
I am attempting to display data from the on-board SQLite database within a table in the Android UI, but it isn't working as it should. Here is what I have:
activity_main.xml
<TableLayout
android:id="#+id/data_table"
android:stretchColumns="3"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>
<TextView android:layout_column="1" android:text="id" android:layout_width="0dp" android:layout_weight="1" />
<TextView android:layout_column="2" android:text="firstname" android:layout_width="0dp" android:layout_weight="1" />
<TextView android:layout_column="3" android:text="lastname" android:layout_width="0dp" android:layout_weight="1" />
</TableRow>
</TableLayout>
MainActivity.java
List<People> listOfPeople = databaseHelper.getAllPeople();
TableLayout myTableLayout = (TableLayout) findViewById(R.id.data_table);
for(People person : listOfPeople){
TableRow newTableRow = new TableRow(this);
TableRow.LayoutParams rowParams = new TableRow.LayoutParams();
newTableRow.setLayoutParams(rowParams);
TextView idTV = new TextView(this);
idTV.setLayoutParams(new TableRow.LayoutParams(1));
idTV.setText(person.getId());
TextView firstnameTV = new TextView(this);
firstnameTV.setLayoutParams(new TableRow.LayoutParams(2));
firstnameTV.setText(person.getFirstname());
TextView lastnameTV = new TextView(this);
lastnameTV.setLayoutParams(new TableRow.LayoutParams(3));
lastnameTV.setText(person.getLastname());
newTableRow.addView(idTV);
newTableRow.addView(firstnameTV);
newTableRow.addView(lastnameTV);
myTableLayout.addView(newTableRow);
}
The table and data is displayed, but the data within the rows I built from the database are all smooshed together and unreadable. If I were to compare this to web development, it looks like all the data are in one cell of the table row.
I am very new at Android development (former JSP developer), and what I have here, I have tried to piece together from Google searches. I need to use a TableLayout so a grid layout (or any other layout) will not do.
EDIT:
I added some background color to my "cells" and discovered they are in different cells. The issue, apparently, is that they are not distributing themselves correctly as I am not sure how to add "layout_width" and "layout_weight" to the cells within Java. At least, I guess that is what the issue is.
Just change your xml to this:
<TableLayout
android:id="#+id/data_table" android:stretchColumns="1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>
<TextView android:layout_column="1" android:layout_weight="1" android:text="id" />
<TextView android:layout_column="2" android:layout_weight="1" android:text="firstname" />
<TextView android:layout_column="3" android:layout_weight="1" android:text="lastname" />
</TableRow>
Make sure to also set the layout_weight to 1, for all the TextViews added by java code.
I'm working in an Android application and I want to add a TableRow programmatically in my TableLayout.
I have this TableLayout:
<TableLayout
android:id="#+id/details_table"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow>
<TextView
android:text="4686"
android:layout_width="wrap_content"
android:layout_column="0"
android:layout_weight="1"
android:textSize="7px"
android:textColor="#color/black" />
<TextView
android:text="sdhiuf osdfh isdhf ihdf"
android:layout_width="wrap_content"
android:layout_column="1"
android:layout_weight="1"
android:textSize="7px"
android:textColor="#color/black" />
<TextView
android:text="2"
android:layout_width="wrap_content"
android:layout_column="2"
android:layout_weight="1"
android:textSize="7px"
android:textColor="#color/black" />
<TextView
android:text="UN"
android:layout_width="wrap_content"
android:layout_column="3"
android:layout_weight="1"
android:textSize="7px"
android:textColor="#color/black" />
</TableRow>
and I want to add this exactly TableRow programmatically.
I'm trying something like this:
TableLayout detailsTable = (TableLayout) l.findViewById(R.id.details_table);
for(Nfce_Product nfceProduct : nfceProducts){
TableRow tableRow = new TableRow(getActivity());
TextView tvProductCode = new TextView(getActivity());
tvProductCode.setLayoutParams(new TableRow.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT, 1f));
tvProductCode.setText(nfceProduct.getProduct_code());
tvProductCode.setTextSize(TypedValue.COMPLEX_UNIT_PX, productDetailsTextSize);
tvProductCode.setTextColor(getResources().getColor(R.color.black));
TextView tvProductDescription = new TextView(getActivity());
tvProductDescription.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT, 1f));
tvProductDescription.setText(nfceProduct.getProduct_description());
tvProductDescription.setTextSize(TypedValue.COMPLEX_UNIT_PX, productDetailsTextSize);
tvProductDescription.setTextColor(getResources().getColor(R.color.black));
TextView tvProductAmount = new TextView(getActivity());
tvProductAmount.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT, 1f));
tvProductAmount.setText(String.valueOf(nfceProduct.getAmount()));
tvProductAmount.setTextSize(TypedValue.COMPLEX_UNIT_PX, productDetailsTextSize);
tvProductAmount.setTextColor(getResources().getColor(R.color.black));
TextView tvProductMetric = new TextView(getActivity());
tvProductMetric.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT, 1f));
tvProductMetric.setText(nfceProduct.getProduct_metric());
tvProductMetric.setTextSize(TypedValue.COMPLEX_UNIT_PX, productDetailsTextSize);
tvProductMetric.setTextColor(getResources().getColor(R.color.black));
tableRow.addView(tvProductCode);
tableRow.addView(tvProductDescription);
tableRow.addView(tvProductAmount);
tableRow.addView(tvProductMetric);
detailsTable.addView(tableRow);
}
Here is my own answer to my own dynamically created TableRow question. I think my answer is detailed enough, you should have no problems taking it as your own! My issue involved not only dynamically creating TableRows, but also being able to touch each row and have something happen. I've gone further than that nowadays to make each CELL separately clickable.
Edit: You need to have a separate TableRow XML file that you can access, separate from the actual TableLayout you're trying to dynamically populate.
Edit2: I should probably try to make an actual solution for you, so that you see what I'm talking about:
First, create (or keep) your XML file containing your TableLayout. Second, Create a separate TableRow XML file.
(This is your potential tablerow.xml file)
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:focusable="true" >
<TextView
android:id="#+id/tableCell1"
android:layout_width="wrap_content"
android:layout_column="0"
android:layout_weight="1"
android:textSize="7px"
android:textColor="#color/black" />
<TextView
android:id="#+id/tableCell2"
android:layout_width="wrap_content"
android:layout_column="1"
android:layout_weight="1"
android:textSize="7px"
android:textColor="#color/black" />
<TextView
android:id="#+id/tableCell3"
android:layout_width="wrap_content"
android:layout_column="2"
android:layout_weight="1"
android:textSize="7px"
android:textColor="#color/black" />
<TextView
android:id="#+id/tableCell4"
android:layout_width="wrap_content"
android:layout_column="3"
android:layout_weight="1"
android:textSize="7px"
android:textColor="#color/black" />
</TableRow>
Now... back to your actual code!
for (Nfce_Product nfceProduct : nfceProducts) {
final TableLayout detailsTable = (TableLayout) findViewById(R.id.details_table);
final TableRow tableRow = (TableRow) getLayoutInflater().inflate(R.layout.tablerow, null);
TextView tv;
//Filling in cells
tv = (TextView) tableRow.findViewById(R.id.tableCell1);
tv.setText(nfceProduct.getProduct_code());
tv = (TextView) tableRow.findViewById(R.id.tableCell2);
tv.setText(nfceProduct.getProduct_description());
tv = (TextView) tableRow.findViewById(R.id.tableCell3);
tv.setText(nfceProduct.getAmount());
tv = (TextView) tableRow.findViewById(R.id.tableCell4);
tv.setText(nfceProduct.getProduct_metric());
//Add row to the table
detailsTable.addView(tableRow);
} //End for
If you still need/want to change the text size and color, you can do that after the setText() line and before you do findViewById again. If you want to have column headers that basically say Code, Description, Amount, and Metric, make a TableRow inside the TableLayout like you have currently. The TableRows created programmatically will fall in line after that "header" row.
I have a layout file called row which is basically a wrapper for how i want my row to look like
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:id="#+id/tableRow"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView
android:id="#+id/oid"
android:layout_width="0dp"
android:layout_weight="1" android:background="#dcdcdc"
android:textColor="#000000"
android:padding="5dip"
/>
<TextView
android:id="#+id/value"
android:layout_width="0dp"
android:layout_weight="1" android:background="#d3d3d3"
android:textColor="#000000"
android:padding="5dip"
/>
<TextView
android:id="#+id/type"
android:layout_width="0dp"
android:layout_weight="1" android:background="#dcdcdc"
android:textColor="#000000"
android:padding="5dip"
/>
</TableRow>
</LinearLayout>
Now i d like to be able to create a row using this layout and place it in my table that is defined in the fragment layout. This is my current code for creating a row(it doesn't use the row layout file) and adding it to the table, but the row is not formatted as i d like it to be.
TableLayout varbindTable = (TableLayout) view2.findViewById(R.id.tableLayout1);
TableRow row = new TableRow(getActivity());
row.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT));
varbindTable.addView(row);
TextView name = new TextView(getActivity());
name.setText(((EditText)view.findViewById(R.id.oid)).getText().toString());
name.setTextColor(Color.BLACK);
name.setLayoutParams(new TableRow.LayoutParams(
0,
TableRow.LayoutParams.MATCH_PARENT));
TextView value = new TextView(getActivity());
value.setText(((EditText)view.findViewById(R.id.value)).getText().toString());
value.setTextColor(Color.BLACK);
value.setLayoutParams(new TableRow.LayoutParams(
0,
TableRow.LayoutParams.MATCH_PARENT));
TextView type = new TextView(getActivity());
type.setText(((Spinner)view.findViewById(R.id.data_type_spinner)).getSelectedItem().toString());
type.setTextColor(Color.BLACK);
type.setLayoutParams(new TableRow.LayoutParams(
0,
TableRow.LayoutParams.MATCH_PARENT));
row.addView(name);
row.addView(value);
row.addView(type);
Well, at first glance, the issue looks like your settings are different in the 2 examples. In the xml, you are using layout_width="0dp" and layout_weight="1". However, in the code you are using MATCH_PARENT.
Closest example I could find:
How do you setLayoutParams() for an ImageView?
I want to create a very simple scheduler by inserting data into a table. I have no problem getting this information, however, I cannot seem to insert into a meaningful table. By meaningful table, I mean a table that starts out as such, with subsequent additions inserted below their respected title:
DAY 1:
DAY 2:
DAY 3:
DAY 4:
I get the course information from the user and want to categorize the courses based on when they occur in their timetable. I am having a rather difficult time organizing this into a table.
Any suggestions on how I might efficiently sort this?
Use TableLayout and create a template of your layout in XML. For example:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >
<TextView
android:id="#+id/textView1"
android:text="DAY 1:"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >
<TextView
android:id="#+id/textView1"
android:text="DAY 2:"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >
<TextView
android:id="#+id/textView1"
android:text="DAY 3:"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
<TableRow
android:id="#+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >
<TextView
android:id="#+id/textView1"
android:text="DAY 4:"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
</TableLayout>
Then inspect a list of courses information and for every course add programatically info about it as a TableRow to your previously created layout . Use TableLayout#addView(View child, int index) for that. For example to add TableRow containing TextView as the last row to TableLayout:
TableLayout tl = (TableLayout) findViewById(R.id.my_table_layout);
TableRow tr = new TableRow(context);
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
TextView tv = new TextView(context);
tv.setText("Some text");
tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
tr.addView(tv);
tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
In my Android application there is a screen with some data to be displayed in table format. This table will have around 5 columns and at least 50 rows.
How do I do I create a table in android?
I searched through and everybody seems to recommend to use TableLayout and textView for each cell. Using this technique seems to be a bit difficult as there are lots of textView Components.
Is there any other solution to this?
TableLayout lTableLayout;
lTableLayout = (TableLayout)findViewById(R.id.tblayout);
for(int i=0;i<10;i++){
TableRow tr1 = new TableRow(this);
tr1.setPadding(0, 5, 0, 0);
lTableLayout.addView(tr1, LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
TextView tv11 = new TextView(this);
tv11.setPadding(2, 0, 0, 0);
tv11.setTextSize(12);
tr1.addView(tv11,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); // Can give width and height in numbers also.
}
These will create 10 rows and 1 textview in each row in taken table layout.
take one table layout in your xml file like below :
<TableLayout
android:id="#+id/tblayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TableLayout>
Use ListView with Custom adapter , create five textview's in listview item as columns..
create listitem like this
row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:text="TextView" android:id="#+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<TextView android:text="TextView" android:id="#+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<TextView android:text="TextView" android:id="#+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<TextView android:text="TextView" android:id="#+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<TextView android:text="TextView" android:id="#+id/textView5" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</LinearLayout>
Its not necessary to user TableLayout.
you can user Custom ListView And set header of ListView as your Column name
To create custom listview see this example