Display data from database in listview and delete checked item - java

Can I know how to display the name of item from database with checkbox beside which enable the users to delete the item when the checkbox is ticked.
I have gone through several articles that mentioned about this but most of the articles are not displaying the data from database. They mentioned about the adapter but I am not very sure how does it function.
Can anybody explain to me:
how does the adapter work between listview, textview and checkbox
how to detect and delete the selected item when the checkbox is ticked
Below are the two different .xml that I have done:::
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/wishlistln"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp" >
<CheckBox
android:id="#+id/wishlistcb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="" />
<TextView
android:id="#+id/wishlist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:textSize="16sp" />
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="400dp" />
Thank you.

here a nice tutorial on ListView and Adapter, it show how to display data and how to handle events on the ListView

Related

Widget ListView loading view

I'm trying to display a dynamic list on a widget and so far i've been able to display my 3 headers (which are simply textview inside a linearlayout) but for my items (2 textview in one linearlayout inside another linear layout) the view displays Loading... instead of the content (a task label).
I've already bumped getViewTypeCount to 2 in my RemoteViewsFactory to match the number of views returned by the getViewAt.
I can't figure out what is wrong inside this layout that would cause a problem:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="3dp"
android:clickable="true"
android:focusable="true"
style="#style/SelectableItemBackground"
android:id="#+id/widget_item"
>
<LinearLayout
android:paddingLeft="22dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="•"
android:paddingRight="6dp"
android:textSize="17dp"
android:textColor="#color/colorText" />
<TextView
android:id="#+id/widget_item_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="17dp"
android:textColor="#color/colorText" />
</LinearLayout>
Thank you for reading :)
To anyone reading this, i have been able to solve it by suppressing the style attribute

diffculty choosing an item from a list view

Hello I've built a list view that in its XML file have 2 layouts with background and 1 text view on each layout,I'm getting from the strings array text, And changing the text view,this is my code for the row I'm getting for for the list view:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:weightSum="2"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#drawable/purple"
android:layout_weight="1"
android:id="#+id/rl1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textColor="#ffffff" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#drawable/purple"
android:layout_weight="1"
android:id="#+id/rl2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textColor="#ffffff" />
</RelativeLayout>
</LinearLayout>
Now my problem is when I'm trying to choose only one of the boxes on list view on item selected and doing a switch on the positions I'm getting the 2 boxes on the same position so it don't matter what what box I'm choosing, Iv'e try to give the linear layouts id and then use set on click listener on them with no successes,I'll appreciate and suggestions.
I'm not sure I understand.
Do you use this layout to create ListView items ?
If that is the case, the problem may be that when the ListView is refreshed, it inflates instances of this layout (in its default configuration, the components are in their default position). In order to preserve the changes, you will need to save each item's state and modify its layout every time the list is refreshed.

Can I put Button in Spinner

I can use String-array inside Spinner
But i don't know if i can put anything else ?
I want to put some Button's in a Spinner but when i put some in XML , the application crashed !!
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinner"
android:layout_below="#+id/textView"
android:layout_toRightOf="#+id/textView"
android:layout_toEndOf="#+id/textView"
android:layout_marginTop="125dp" >
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</Spinner>
Is this the Right way to do it
Or what ???
This is not going to work bgecause this is not the way that you place buttons in a spinner.
By Definition Spinner is a dropdown menu that can almost take any layout. First of all you have to decide how your spinner row will be.
e.g.:
If you would like to have 2 buttons you got to make a layout/spinner_row.xml file with:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:padding="20dp"
android:layout_height="wrap_content">
<Button
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button" />
<Button
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button2" />
</LinearLayout>
and looks like:
Then you got to make an adapter that adapts each row of the spinner with the data that you would like.

Is there a more systematic way to populate my layout?

I'm working on the layout as shown below.
So far I've had success with manually defining each View in xml (RelativeLayout within a ScrollView) and manually populating each spinner in java. But this is tedious. I wish I could somehow use "I have this array of items. For each item I want a row on the screen with a spinner and an EditText." I must then be able to grab the user input to put into a database. Eventually, I'll want to be able to come back and edit this screen, which means I'll have to repopulate these fields from a database query.
I tried working some with a ListView, but had trouble populating multiple rows and then read that RelativeLayout is supposed to perform better. What would you suggest? If I have to declare every field in xml and java manually, then so be it.
This is what my XML looks like...
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="180dp" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<Spinner
android:id="#+id/category_spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
/>
<EditText
android:id="#+id/editWeight1"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/percentSign1"
android:layout_alignBaseline="#id/category_spinner1"
android:inputType="numberDecimal" >
</EditText>
<TextView
android:id="#id/percentSign1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignBaseline="#id/category_spinner1"
android:text="#string/percentSign"
android:textSize="20sp" >
</TextView>
<Spinner
android:id="#+id/category_spinner2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#id/category_spinner1"
/>
<EditText
android:id="#+id/editWeight2"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/percentSign2"
android:layout_alignBaseline="#id/category_spinner2"
android:inputType="numberDecimal" >
</EditText>
<TextView
android:id="#id/percentSign2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignBaseline="#id/category_spinner2"
android:text="#string/percentSign"
android:textSize="20sp" >
</TextView>
...
And my Java...
Spinner catSpin1 = (Spinner) rootView.findViewById(R.id.category_spinner1);
ArrayAdapter<String> catSpinAdapter1 = new ArrayAdapter<String>(this.getActivity(),android.R.layout.simple_spinner_item, DBAdapter.gradeTypes);
catSpinAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
catSpin1.setAdapter(catSpinAdapter1);
catSpin1.setSelection(0);
Spinner catSpin2 = (Spinner) rootView.findViewById(R.id.category_spinner2);
ArrayAdapter<String> catSpinAdapter2 = new ArrayAdapter<String>(this.getActivity(),android.R.layout.simple_spinner_item, DBAdapter.gradeTypes);
catSpinAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
catSpin2.setAdapter(catSpinAdapter2);
catSpin2.setSelection(1);
Spinner catSpin3 = (Spinner) rootView.findViewById(R.id.category_spinner3);
ArrayAdapter<String> catSpinAdapter3 = new ArrayAdapter<String>(this.getActivity(),android.R.layout.simple_spinner_item, DBAdapter.gradeTypes);
catSpinAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
catSpin3.setAdapter(catSpinAdapter3);
catSpin3.setSelection(2);
...
Sure, create a String array, for example:
<string-array name="my_array">
<item>One</item>
<item>Two</item>
</string-array>
Then add it to te spinner on the xml this way:
<Spinner
android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:entries="#array/my_array"/>
My first intention would also have been a ListView, you could create a simple datastructure like a class with two string values that will get updated/displayed by this spinner/edittext combination and you could use this class also to persist it in your database.
Some side note: You should have a look at re-using layouts: http://developer.android.com/training/improving-layouts/reusing-layouts.html
That would have made your approach much easier. (define one spinner/edittext layout and re-use it inside your main xml)

Selecting Rows in Listview and Setting up OnClickListener - Android

I have an app reading from an SQLite database using an Adapter to display the results in a RelativeLayout (code shown below).
I have two problems :
1) The rows in the list are no longer selectable. Before I added in the image (I think) they could be highlighted / selected.
EDIT - This seems to be caused by the white background I have added to the row ? Does anyone know how I can correct this ?
2) Once the above is fixed, how do I setup an OnClickListener to detect when someone selects an item in the list ?
Here is the code for the layout :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="70dip"
android:background="#drawable/white"
android:orientation="horizontal"
android:padding="10sp">
<ImageView
android:id="#+id/Logo"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="6dip" />
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF000000"
android:textStyle="bold"
android:textSize="12sp"
android:typeface="sans"
android:layout_toRightOf="#id/Logo" />
<TextView
android:id="#+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:layout_below="#id/name"
android:textColor="#0000CC"
android:layout_toRightOf="#id/Logo" />
<TextView
android:id="#+id/address"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="12sp"
android:layout_below="#id/description"
android:textColor="#990000"
android:layout_toRightOf="#id/Logo" />
</RelativeLayout>
As far as to set the on click listener to an item in list view , see the following link
http://developer.android.com/reference/android/widget/AdapterView.html#setOnItemClickListener(android.widget.AdapterView.OnItemClickListener)

Categories