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

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>

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.

Displaying two values on the same row in a listview

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>

Android: Set Visibility of TextView in ListAdapter

I have a layout that contains two ListViews (see image below), which are both created using two SimpleAdapters; let's call the ListView on top the QLocations ListView and the one on bottom the AllLocations ListView.
I would like to hide all red TextView elements from my AllLocations ListView. I can't figure out how to isolate the TextViews only from the AllLocations List View and not from the QLocations ListView as well.
So, my question is, how do I hide all TextView elements from a particular ListView?
Here is my code:
Creating the ListViews
// Getting qListView: List View for locations with Qs
qLV = (ListView) activity.findViewById(R.id.qListView);
// list adapter
ListAdapter qAdapter = new SimpleAdapter(activity, qListItems,
R.layout.google_places_item,
new String[] { KEY_REFERENCE, KEY_NAME}, new int[] {
R.id.reference, R.id.name });
// Adding data into listview
qLV.setAdapter(qAdapter);
// Getting allListView: List View for locations without Qs
allLV = (ListView) activity.findViewById(R.id.allListView);
// list adapter
ListAdapter allAdapter = new SimpleAdapter(activity, placesListItems,
R.layout.google_places_item,
new String[] { KEY_REFERENCE, KEY_NAME}, new int[] {
R.id.reference, R.id.name });
// Adding data into listview
allLV.setAdapter(allAdapter);
google_places_item.xml
<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/reference"
android:visibility="gone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<!-- Row for each location -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/qLocationRow"
android:orientation="horizontal"
>
<!-- Q Name -->
<TextView android:id="#+id/name"
style="#style/qName"
/>
<!-- Q WaitTime -->
<TextView android:id="#+id/qName.qWaitTime"
style="#style/qName.qWaitTime"
android:text="14"
/>
</LinearLayout>
</LinearLayout>
Activity Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/fullWidth">
<!-- Label for List View of QLocations -->
<TextView
android:id="#+id/qListLabel"
style="#style/widthMargins.Label"
android:text="#string/qListLabel" />
<!-- Horizontal Rule -->
<View
android:id="#+id/horizontalRule1"
style="#style/widthMargins.horizontalRule"
android:layout_below="#+id/qListLabel"
/>
<!-- Linear Layout containing progress bar + text description -->
<LinearLayout
android:layout_width="200dp"
android:layout_height="60dp"
android:id="#+id/lin_progressBar"
android:visibility="visible"
android:layout_centerHorizontal="true"
android:layout_below="#id/horizontalRule1"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<ProgressBar
android:id="#+id/loadPlacesProgress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/loadPlacesText"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="5dp"
android:text="Loading..."
android:gravity="center_vertical"
android:textSize="15sp"
/>
</LinearLayout>
<!-- ListView for Q Locations -->
<ListView
android:id="#+id/qListView"
style="#style/widthMargins"
android:layout_height="140dp"
android:layout_below="#+id/horizontalRule1" />
<!-- Label for List View of All Locations -->
<TextView
android:id="#+id/allListLabel"
style="#style/widthMargins.Label"
android:layout_below="#+id/qListView"
android:text="#string/allListLabel" />
<!-- Horizontal Rule -->
<View
android:id="#+id/horizontalRule2"
style="#style/widthMargins.horizontalRule"
android:layout_below="#+id/allListLabel"
/>
<!-- Linear Layout containing progress bar + text description -->
<LinearLayout
android:layout_width="200dp"
android:layout_height="60dp"
android:id="#+id/lin_progressBar2"
android:visibility="visible"
android:layout_centerHorizontal="true"
android:layout_below="#id/horizontalRule2"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<ProgressBar
android:id="#+id/loadPlacesProgress2"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/loadPlacesText2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="5dp"
android:text="Loading..."
android:gravity="center_vertical"
android:textSize="15sp"
/>
</LinearLayout>
<!-- ListView for All Locations -->
<ListView
android:id="#+id/allListView"
style="#style/widthMargins"
android:layout_height="140dp"
android:layout_below="#+id/horizontalRule2"
/>
<!-- Show on Map button -->
<Button android:id="#+id/btn_show_map"
style="#style/fullWidth"
android:layout_centerHorizontal="true"
android:text="Map"
android:padding="20dp"
android:layout_alignParentBottom="true"
android:layout_marginTop="10dip"/>
</RelativeLayout>
So after you set your Adapters, you should be able to run the code below to give the the desired affect. I'm not sure how efficient it is, but it should at least get the job done.
int numChildren = allLV.getListView().getChildCount();
for(int i = 0; i < numChildren; i++)
{
LinearLayout ll = (LinearLayout) allLV.getListView().getChildAt(i);
TextView tv = (TextView) ll.findViewById(R.id.qName.qWaitTime);
tv.setVisibility(View.INVISIBLE);
}
Then when you want to show the red boxed text, use a similar method to set them to visible again.
First off know that there is not a 1 to 1 mapping between your listview items and the data. If you have many items the views get reused as you scoll through the list.
First you want to update the of all items in the adapter to have the correct values. Once you have updated the data. Then notify the adapter that its data has changed and it will rebuild itself adapter.notifyDataSetChanged(). Make rebuilding the list fast by caching data as appropriate.
You might read up on MVC (Model-View-Controller) to gain a better understanding of what is going on here.

ANDROID: SQLite with ListViews

-- This is my layout thus far for the input --
<?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="wrap_content"
android:orientation="vertical" >
<EditText
android:id="#+id/etClassName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:hint="#string/className"/>
<EditText
android:id="#+id/etAssignmentName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:ems="10"
android:hint="#string/assignmentName"/>
<EditText
android:id="#+id/etDueDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:layout_marginBottom="20dp"
android:hint="#string/dueDate" />
<EditText
android:id="#+id/etNotes"
android:layout_width="match_parent"
android:layout_height="194dp"
android:ems="10"
android:inputType="textMultiLine"
android:layout_marginBottom="5dp"
android:hint="#string/notes"/>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal">
<Button
android:id="#+id/bAddAssignments"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/addAssignment"
android:layout_weight="1" />
<Button
android:id="#+id/bViewAssignments"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/viewAssignments"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
-- Clicking the add button brings you to this page which is supposed to list the assignment name that you can click to view your assignment --
<?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" >
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.63" >
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/bNewAssignment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/newAssignment" />
<Button
android:id="#+id/bdeleteAssignment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/deleteAssignment" />
</LinearLayout>
</LinearLayout>
My problem is that I don't know how to populate the second layout when my first layout keeps track of the data. How does the second layout know what the user inputted etc in order to populate the listview
I currently have a dbadapter class(with helper class inside), an entry class(creates entry objects), and an assignmentclass(supposed to display listview)
Any Ideas, please?
Pass the data you need in an Intent bundle.
When you set the intent, put the data you need in ther next class as an extra (assuming strings here, but can be other forms):
Intent myIntent = new Intent(YourActivity.this, NewActivity.class);
myIntent.putExtra("Data1", data1var);
myIntent.putExtra("Data2", data2var);
YourActivity.this.startActivity(myIntent);
In the new class:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value1 = extras.getString("Data1");
String value2 = extras.getString("Data2");
}
//use your passed strings
}
Your description of the problem is rather vague. I can refer you to one of the tutorials Lars Vogella made about how to use SQLLite together with a ListView. They explain everything nice and simple. I am sure that you will find your answer there!
Update:
onClickListener of the Add-button in InputActivity:
Add your user input to the database (with you DatabaseHelper)
List item Start you second activity including the ListView
onCreate of the ListActivity:
Create for example an Array
Let your DatabaseHelper fill the array with the records you want in the database
Create an Adapter giving you filled array with it

android - filling ListView with array

I can't fill a ListView layout with an array of strings. Here is the Activity:
ListView symptomList = (ListView) findViewById(R.id.ListView_Symptom);
String symptomsArray[] = new String[1024];
// Then I fill up symptomsArray
ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, R.layout.menu_item, symptomsArray);
symptomList.setAdapter(adapt);
Here is menu_item:
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:textSize="#dimen/menu_item_size"
android:text="test string"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"
android:shadowRadius="5"
android:gravity="center"
android:shadowDy="3"
android:shadowDx="3" />
And here is symptom.xml -
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_height="wrap_content"
android:id="#+id/ListView_Symptom"
android:layout_width="fill_parent"
android:divider="#drawable/divider"
android:listSelector="#drawable/textured"
android:layout_alignParentTop="true"></ListView>
</LinearLayout>
Does anyone have any idea what is wrong? Does my public class need to extend "ListActivity". I tried that and that didn't work.
Try stripping out some of the presentational bits from your TextView. For example, I don't think that you want android:layout_gravity="center" there. Also, try setting your ListView's android:layout_height="fill_parent".
Turns out Eclipse is so slow running on my 5 GB Dram MacPro that it eventually worked and displayed the array in the ListView, it just took a few minutes! Sorry about the confusion.

Categories

Resources