Displaying two values on the same row in a listview - android

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>

Related

Populating a table with an array of data

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.

Android: ScrollView does not re-size when adding child views

I have a ScrollView that I am trying to populate dynamically. Here is the XML that contains the ScrollView:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f6f5f5" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#40ff0000"
android:paddingBottom="70dp"
android:paddingTop="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp">
<LinearLayout
android:id="#+id/option_list_container"
android:background="#40ffff00"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:background="#292929"
android:paddingBottom="5dp"
android:paddingLeft="15dp"
android:paddingTop="5dp"
android:text="Stap 3/5"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/white" />
</LinearLayout>
</ScrollView>
And here is the XML for the element I'm inserting in the ScrollvVew:
<?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" >
<np.custom.FontIcon
android:id="#+id/option_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/calendar_circleround"
android:textSize="40sp"
android:gravity="center"
android:text="#string/planit_selection_empty_circle"
android:textColor="#292929" />
<TextView
android:id="#+id/option_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_marginLeft="10dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
This is the code I use to populate the scrollview:
ArrayList<String> g = new ArrayList<String>();
g.add("Fix a Bug");
g.add("Buy a new PC");
g.add("Make Coffee");
g.add("Take a Break");
g.add("Don't do that");
g.add("Throw your hands in the air like you just don't care!");
LinearLayout optionListLayout = (LinearLayout) rootView.findViewById(R.id.option_list_container);
LayoutInflater inflater = LayoutInflater.from(getActivity());
for(String p:g){
View v = inflater.inflate(R.layout.planit_option_item, null);
TextView optText = (TextView) v.findViewById(R.id.option_text);
optText.setText(p);
optionListLayout.addView(v);
}
This all works almost fine, except that the sizes of the ScrollView and the LinearLayout it contains do not come out as I expected. This a screenshot of that it looks like on a device, which shows text being cut out when it goes to a second line:
So how can I make sure the linear layout re-sizes to accommodate children views of different heights?
Your issue isn't the ScrollView not accommodating new children. The problem is that the text, when it spans more than one line, is being clipped due to layout parameters in R.layout.planit_option_item. Try changing the TextView of ID #+id/option_text so that its height is wrap_content.
Change your TextView for the ScrollView element to accommodate two lines:
...
<TextView
android:id="#+id/option_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_marginLeft="10dp"
android:maxLines="2"
android:singleLine="false"
android:textAppearance="?android:attr/textAppearanceMedium" />
...
This is not an issue with ScrollView, it's an issue with the specifications of the element you are inserting into the ScrollView. If you expect there may be more than 2 lines of text in some cases set maxLines = X where X is the maximum number of lines you expect.
Please let me know if this does not work and I would be happy to help further.

How to have some textviews in a row in a ListView? Android

I have a ListActivity where I have some items sorted by datetime.
I have 3 textviews. Here what I've done:
Here is the code for the layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/bpSysHolder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" >
</TextView>
<TextView
android:id="#+id/bpDiaHolder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" >
</TextView>
<TextView
android:id="#+id/bpDtHolder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" >
</TextView>
</LinearLayout>
For calling them from my list activity i use:
String[] from = new String[] { BpDAO.bp_SYS, BpDAO.bp_DIA, BpDAO.bp_DT };
int[] target = new int[] { R.id.bpSysHolder, R.id.bpDiaHolder,
R.id.bpDtHolder };
dbAdapter = new SimpleCursorAdapter(this, R.layout.history_bp, bpList,
from, target);
setListAdapter(dbAdapter);
Now it is like this:
---120952012-09-20 15:05 ---
---145902012-09-20 10:44 ---
---145952012-09-20 10:42 ---
...
---14595 2012-09-20 10:42 ---
---------------------------------
How to have space between them and some little space up and down of the row so to be easier to touch?
you can do this also in same layout.
Change android:orientation="vertical" > to android:orientation="horizontal"
and define use the property "Android:layout_weight" for text-view to aline according to your reqirement.
Use this on the linear layout to have all in one line
android:orientation="horizontal"
On the picture you already have a title on the action bar, if you want on the list you can add a header to the list
you are setting the orientation as vertical:
android:orientation="vertical"
remove it, and you should get the textview horizzontal

can't view a listview inside a listview correctly in android app

I want to display data from a very generic JSON string in my android app. One view/screen shall display an arbitrary number of "details" where each detail has an arbitrary number of "values".
I'm trying to use a ListView inside a Listview, and all the data is nicely put into each list, but the lists won't display fully (see image below).
In the lower part of the screen is the first ListView. It contains a number of TextView labels (e.g. Usage and Info), which in turn has a number of values (units, sw version etc). The values is put in a new list which displays a TextView.
The top part is also a ListView, but is displayed ok.
Looks like this:
First part, status tab with the two lists, first (fine) and second (problem):
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:id="#+id/tabStatus">
<ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_weight="1" android:id="#+id/listThingStatus"/>
<ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_weight="1" android:id="#+id/listThingDetails"/>
</LinearLayout>
The lower ListView with the label and the second ListView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="70px">
<LinearLayout android:orientation="vertical" android:layout_width="0dip" android:layout_weight="1" android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30px" android:id="#+id/textDetails" />
<ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="#+id/listThingValues"/>
</LinearLayout>
</LinearLayout>
And finally the TextViews inside the second ListView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="70px">
<LinearLayout android:orientation="horizontal" android:layout_width="0dip" android:layout_weight="1" android:layout_height="wrap_content" android:gravity="center_vertical">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30px" android:id="#+id/textValuesLabel" />
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="30px" android:gravity="right|center" android:id="#+id/textValuesUnits" />
</LinearLayout>
</LinearLayout>
The result is this (see image), where the content in the inner lists is mostly "hidden" (SW version and text below etc). I use "wrap_content" almost everywhere but the content is too wrapped here :(. How and where do I format my XML to make the content in the inner lists display properly?
Image (from emulator, but same result on phone):
http://i.imgur.com/8mcDq.png
Placing a ListView inside a ListView will only bring you headaches.
Try using an ExpandableListView instead.

android: how i can make a list from database sqlite?

I want to make a list contains some username from my sqlite database. I want it has white background and black text. I felt difficult when find every tutorial thought me to use extends listactivity....is there any technique to make it ?
here is some of my code in nir.java
setContentView(R.layout.changeplayer);
btnCreateN = (Button)findViewById(R.id.btnCreateNew);
btnCreateN.setOnClickListener(this);
btnSetP = (Button)findViewById(R.id.btnOK);
btnSetP.setOnClickListener(this);
//buat listview
lstUnameView = (ListView)findViewById(R.id.listUsername);
adapter = new ArrayAdapter <String>(this,
android.R.layout.simple_list_item_1, lstUname);
lstUnameView.setAdapter(adapter);
//koneksi ke db, ngambil username-username
helper = new sqlite(this, "tamadb", 1);
db1 = helper.getWritableDatabase();
c = db1.rawQuery("SELECT username FROM tama", null);
if(c.moveToFirst())
{
do
{
lstUname.add(c.getString(0));
}while(c.moveToNext());
}
helper.close();
and this my code in layout changeplayer
<RelativeLayout xmlns:android="schemas.android.com/apk/res/android";
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="#+id/listUsername"
android:layout_width="fill_parent"
android:layout_height="350px"
android:drawSelectorOnTop="false"
android:background = "#FFFFFFFF"
android:cacheColorHint = "#191919" />
<Button android:id="#+id/btnCreateNew"
android:layout_width="120px"
android:layout_height="wrap_content"
android:text="Create New"
android:layout_below="#id/listUsername"
android:layout_marginLeft="40px" />
<Button android:id="#+id/btnOK"
android:layout_width="120px"
android:layout_height="wrap_content"
android:text="OK"
android:layout_below="#id/listUsername"
android:layout_toRightOf="#id/btnCreateNew" />
</RelativeLayout>
Please help, i want to finish this project too...he9x..thx before...^^
If you want your whole app to have white background and black text you can set the theme for your app to Theme.Light. Therefore you have to add the attribute android:theme="#android:style/Theme.Light" to the element of the manifest file.
This is the layout.xml I have put together for a listview with white background and black text.
You need to set the colours list_bg and menu_text to white and black respectively in your res folder then call the layout and id in your activity.
As far as populating a listview there is a good example here with downloadable code:
http://www.youtube.com/watch?v=Awu7Rlsez_k
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/list_bg">
<TextView android:background="#color/list_bg"
android:textColor="#color/menu_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding ="10dp"
android:textSize="22sp"
android:textStyle="bold"
android:id="#+id/row" />
<ListView android:id="#+id/list_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>

Categories

Resources