I want to set up a table which has a fixed amount of columns, and an X amount of rows with The first row listing the contents of each column. For example, one column will be 'Name' and a second column will be 'Age', then there will be an X amount of rows which store the data. Is there any way that I can set up arrays for this data elsewhere, and automatically create/fill the rows of the table with this data. I've done this previously with a more simple example using a Custom Adapter but I'm not quite sure how to go about this with a table involved. I'm quite stuck and any help would be greatly appreciated.
I think inside the table layout create the tablerow as below
<TableLayout
---
>
<TableRow
--
>
<Textview
for name
/>
<Textview
...for age
/>
</TableRow>
<TableRow
--
>
<Listview
for name
/>
<Listview
...for age
/>
</TableRow>
</TableLayout>
and populate the listview with a simple arrayadapter with your fixed data.
this's simple if you have textviews only in your listview you can use
ArrayAdapter ad=new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item1,namearray); list1.setAdapter(ad);
ArrayAdapter ad=new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item1,agearray); list2.setAdapter(ad);
A ListView basically acts as a row within any table of data. You should create a POJO with all the attributes you want to display in a row. You can create create a custom xml layout for the data, which would probably by a horizontal LinearLayout that corresponds to your columns.
POJO
public class MyData {
public String Name;
public int Age;
// More data here
}
ListView item layout (layout_list_item.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/Name"
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight="0.7" />
<TextView
android:id="#+id/Age"
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight="0.3" />
</LinearLayout>
Main Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight="0.7"
android:text="Name" />
<TextView
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight="0.3"
android:text="Age" />
</LinearLayout>
<ListView
android:id="+#id/MyDataListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
You then need a custom adapter which sets the fields in the list view with the values from your POJO. There are plenty of tutorials on the internet for this.
Related
I'd like to be able to display the name and price of the product on the same line. Right now, it's displayed as:
Product name
Product price
I've tried adding a linearlayout with the orientation horizontal, tried using weightsum / weight and can't figure out how to make it show properly, like in a normal menu, with the name at the left and the price at the right. I also tried setting their gravity, but it always displays them on two rows. I'd like to display them as such:
Product name...........................Product Price
The layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="#ffffff">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/Price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<LinearLayout
android:id="#+id/LinearLayout01"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<Button
android:id="#+id/Remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:text="Remove product(s)" />
</LinearLayout>
</LinearLayout>
How I'm adding the items:
oslist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put(PROD_NAME, name);
map.put(PROD_PRICE, price);
oslist.add(map);
adapter = new SimpleAdapter(shopping_cart.this, oslist,
R.layout.shoppingcart,
new String[] {PROD_NAME, PROD_PRICE}, new int[] {R.id.name, R.id.Price});
setListAdapter(adapter);
It's often easier to create a layout like this using a RelativeLayout as it gives you more control over the placement of views. You would give the name element layout_alignParentLeft="true" and the price element layout_alignParentRight="true" (and probably layout_gravity="right" to right align the text).
EDIT
Here's what your XML might look like. You won't actually need the layout_gravity attribute if you are using layout_width="wrap_parent", and I've omitted the remove button because I'm not sure where you want it to be.
This layout allows the name and price to overlap if they are too long - probably the best option to fix that is to put the name element after the price element in the XML and add layout_toLeftOf="#id/Price" to it. That forces it to wrap the name instead of overlap the price.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="#ffffff">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
/>
<TextView
android:id="#+id/Price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
I really need help
I have a ResultSet as a result of select query from MySQL DB. how can I display it in table layout?? I have setup a layout for tableview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<EditText
android:id="#+id/myFilter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/some_hint" />
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:padding="10dp"
android:text="#string/some_text" android:textSize="20sp" />
<TableLayout
android:id="#+id/producttablelayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dividerPadding="#dimen/activity_horizontal_margin"
android:scrollbars="horizontal|vertical"
android:showDividers="none|beginning|middle|end" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="top|bottom|left|right"
android:paddingTop="#dimen/activity_horizontal_margin" >
<TextView
android:id="#+id/ttt1"
android:text="Column 1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/ttt2"
android:text="Column 12"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
</TableLayout>
</LinearLayout>
I want to be able to add ResultSet data into it. Something like:
super.onCreate(savedInstanceState);
String s = null;
setContentView(R.layout.activity_list_all_products);
// Show the Up button in the action bar.
setupActionBar();
context333=this;
TableLayout myTable = (TableLayout)findViewById(R.id.producttablelayout);
try
{
connect2 = DriverManager.getConnection(LogonActivity.url, LogonActivity.user, LogonActivity.password);
statement2 = connect2.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
preparedStatement2 = connect2.prepareStatement("select article_code,article_desc from products limit 4");
resultSet2=preparedStatement2.executeQuery();
resultSet2.beforeFirst();
while (resultSet2.next()) {
how to add next record here to the table layout??
}
Please help
You can create a new Views (it seems you are populating your table rows with TextViews) and populate them with the results from your query as you wish. Then you can create a new TableRow with a constructor (see the documentation).
Add the views to to table row instnace using:
tableRowInstance.addView(viewInstance);
and then add the tableRow to the tableLayout the same way:
myTable.addView(tableRowInstance)
TableLayouts and TableRows are both subclasses of View. You should do some research on ViewGroup, View, and how they are structured. This link provides nice visualizations.
I want to create a multirow Button list. Something like this :
but I want to do it dynamically(in code). Is there a way to tell layout to do this automatically? Or i have to do this myself usingRelativeLayout.LayoutParams. I can do this by code but I should control so many things and I was wondering if there is another easier way to do this. For example tell layout to add elements in the next row when the current one is full!
You could also do this with LinearLayout and make all the buttons the same size using weight.
As for your question:
i can do this by code but i should control so many things and i was
wondering if there is another easier way to do this. for example tell
layout to add elements in the next row when the current one is full!
This is potentially possible if you measure the screen width and height and use the Functions in in the View class to figure out the specifics of that particular view and its children.
Alternative
But as mentioned in the comments, there are other views that you can use to solve your problem like GridView.
You can also use a table layout ,
Create first row of tabllayout in xml like this
<?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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:id="#+id/goBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:layout_marginTop="12dp"
/>
</LinearLayout>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_weight="0.80">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_weight="0.80"
android:background="#f0ffff" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableLayout
android:id="#+id/data_table"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:background="#006400"
android:orientation="vertical" >
<TableRow
android:id="#+id/second"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="105dp"
android:layout_height="fill_parent"
android:layout_margin="1dp"
android:background="#drawable/textbg"
android:gravity="center"
android:text="Number1"
android:textColor="#006400" >
</TextView>
<Button
android:layout_width="105dp"
android:layout_height="fill_parent"
android:layout_margin="1dp"
android:background="#drawable/textbg"
android:gravity="center"
android:text="Number2"
android:textColor="#006400" />
<TextView
android:layout_width="105dp"
android:layout_height="fill_parent"
android:layout_margin="1dp"
android:background="#drawable/textbg"
android:gravity="center"
android:paddingBottom="2dp"
android:paddingTop="2dp"
android:text="Distance"
android:textColor="#006400" >
</TextView>
<TextView
android:layout_width="105dp"
android:layout_height="fill_parent"
android:layout_margin="1dp"
android:gravity="center"
android:paddingBottom="8dp"
android:paddingLeft="8dp"
android:paddingRight="5dp"
android:paddingTop="8dp"
android:text="F/G/H/S"
android:textColor="#006400" >
</TextView>
</TableRow>
</TableLayout>
</ScrollView>
</FrameLayout>
</HorizontalScrollView>
</FrameLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_marginTop="4dp"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/savescore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
I created this xml for four three texts and on button in a single row
refer to the table in onCreate
TableLayout extendeedTable = (TableLayout)findViewById(R.id.data_table);
add rows like
while (extendeedTable.getChildCount() > 1)
{
// while there are at least two rows in the table widget, delete
// the second row.
extendeedTable.removeViewAt(1);
}
// collect the current row information from the database and
// store it in a two dimensional ArrayList
// iterate the ArrayList, create new rows each time and add them
// to the table widget.
// Here value is the number of rows you want in table
for (int position=0; position < value ; position++)
{
TableRow tableRow= new TableRow(this);
tableRow.setBackgroundDrawable(null);
// ArrayList<Object> row = data.get(position);
TextView idText = new TextView(this);
idText.setText(Integer.toString(position + 1));
idText.setGravity(Gravity.CENTER);
idText.setTextColor(Color.BLACK);
idText.setWidth(10);
idText.setHeight(45);
idText.setBackgroundResource(R.drawable.text2);
tableRow.addView(idText);
textOne = new Button(this);
textOne.setText("CLUB");
textOne.setBackgroundResource(R.drawable.text2);
textOne.setGravity(Gravity.CENTER);
textOne.setTextColor(Color.BLACK);//left top right bottom
textOne.setWidth(10);
textOne.setHeight(45);
textOne.setId(1+position);
tableRow.addView(textOne);
allbtns.add(textOne);
// textOne.setOnClickListener(this);
textOne.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// do something when the button is clicked
final Button button = (Button) arg0;
System.out.println("value of button is "+
button.getId());
dialog1.setTitle(" SELECT CLUB ");
textTwo = new EditText(this);
textTwo.setText("");
textTwo.setBackgroundResource(R.drawable.text2);
textTwo.setGravity(Gravity.CENTER);
textTwo.setTextColor(Color.BLACK);
textTwo.setWidth(10);
textTwo.setHeight(45);
textTwo.setInputType(InputType.TYPE_CLASS_NUMBER);
tableRow.addView(textTwo);
allEds1.add(textTwo);
textTwo.setId(position +1);
textThree = new EditText(this);
textThree.setText("");
textThree.setWidth(10);
textThree.setHeight(45);
textThree.setTextColor(Color.BLACK);
textThree.setBackgroundResource(R.drawable.text2);
textThree.setGravity(Gravity.CENTER);
textThree.setInputType(InputType.TYPE_CLASS_TEXT);
tableRow.addView(textThree);
allEds2.add(textThree);
textThree.setId(position +1);
extendeedTable.addView(tableRow);
}
for this i took help from here
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/7/
and
its xml
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/6/
after some more google search i finally found the best way to do this. it's so clean and simple, using Adapters and grids.
thanks for all the answers
here is a Tutorial: Creating a Custom Adapter for Gridview(ButtonAdapter)
I have ListView with layout for row like which I inflate at adapter ( extend BaseAdapter ) at getView method
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<CheckBox
android:id="#+id/chk"
android:button="#drawable/q_list_check_box"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
/>
<TextView
android:id="#+id/txtChoice"
android:textColor="#color/white"
android:layout_gravity="center_vertical|left"
android:gravity="center_vertical|left"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
I put at ListView tag android:choiceMode="singleChoice". How to make list single choice, that only one row can be checked at time ?
When I had to implement selection with my custom row., I used an arraylist of boolean to keep the state of the row. The size of boolean arraylist is same as rows of the listview. You can select/deselect the row via changing the value of corresponding boolean in OnListItemClick(). Anyways its just an idea. Here is a link that can help to understand this:
Custom List row with checkbox
I was just trying out with table layout to display some data....
The data is a 3 column data and i want that the columns should utilize the whole width available. But it seems that the layout XML code which i had used is just wrapping up the columns according to the content.
Layout XML code
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableRow
android:layout_width="fill_parent">
<TextView
android:padding="3dip"
android:gravity="left"
android:text="Name"
/>
<TextView
android:padding="3dip"
android:gravity="left"
android:text="Address"
/>
<TextView
android:padding="3dip"
android:gravity="left"
android:text="Age"
/>
</TableRow>
</TableLayout>
You can try adding android:stretchColumns="0,1,2" to your <TableLayout> element.
android:stretchColomns="0,1" is a good option. Also you can try using android:layout_span = "2" This will stretch the columns together depending on your value(Here it will span two columns together)