I am developing an android application in android in which i want to use search option with which i can search particular data item either from web services or from a database ?
Create an XML file named list_item.xml and save it inside the res/layout/ folder. Edit the file to look like this:
?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp"
android:textColor="#000">
</TextView>
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Country" />
<AutoCompleteTextView android:id="#+id/autocomplete_country"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"/>
</LinearLayout>
java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES);
textView.setAdapter(adapter);
}
Inside the HelloAutoComplete class, add the string array:
static final String[] COUNTRIES = new String[] {
"Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",};
Searching inside your database
SQLLite provides built in functionality to allow for full text searching
http://bakhtiyor.com/2009/08/sqlite-full-text-search/
From a webservice
As far as searching via a webservice normally you simply send the search terms to the server via your webservice, the server performs the search and you parse the response.
Lucene
Unless what you are REALLY asking about is a search index, where you may have keywords that allows you to quickly index a large database, or a webservice as the data may not be duplicated between the two.
In which case you can use LUCENE
http://www.javacodegeeks.com/2010/05/introduction-to-apache-lucene-for-full.html
However to get lucene to work with Android you may need to check out the source and remove a few depenencies with RMI.
See the bottom of this page for the solution (only need to remove 2 files and it should compile and work with Android).
http://groups.google.com/group/android-developers/browse_thread/thread/601329551a87e601
How to implement the search dialog box
The developers guide only gives you enough to start implementing the interface to your search dialog box. Read through the example source code (posted below) to see how it fits together into an application.
Android Developers Guide
http://developer.android.com/guide/topics/search/search-dialog.html
Example Code
http://sampleandroidapps.com/searchable-dictionary-source-code-download.html
Use edittext with a button or auto complete text view with a button.
Load data from a file or database for the auto complete part. Once you obtain the data, pass it to the web-service call or search your local database for the same.
Hope I got the requirement right and helped you.
you can use the search dialog; see the following link:
http://developer.android.com/guide/topics/search/search-dialog.html
also:
http://mobileorchard.com/android-app-development-implementing-search-activities/
Related
I'm a beginner at Android development. I have a problem trying to show in a List view that the query doesn't bring any result. I just appear blank (nothing) without indications.
I'm using a Search dialog with customs suggestions, and when I start writing the query, the search starts suggesting, but when nothing match the query, no message appears.I want to create a massage that tells "no data found".
I've created a content provider, a custom suggest interface and a custom cursor adapter to query with a searchable activity an sqlite database.
All the app works perfect but the "no data" massage or toast.
Which part of the code you need?
Thanks
Add this in searchable class
mlistView.setEmptyView( findViewById( R.id.zero_result ) );
and this in xml layout file
<TextView android:id="#+id/zero_resultt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="No match data"/>
I am writing an application which contains a few simple views; first is just two buttons. each button leads to a map, and from this map is a button which opens a list of buildings on the site. From here the user will be able to select a building and view detailed information on it (much like a contacts list actually).
So far, I am able to populate the listview by creating the string array to populate it it directly within the activity, like so:
public class BuildingsActivity extends ListActivity {
static final String[] buildings=new String[]{
"Building 1",
"Building 2",
"Building 3",
"Building 4",
"Building 5"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buildings);
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,buildings));
}
This works all well and good for a 5 item list, but in it's final form, the list may contain up to a hundred buildings, and I have 2 concerns:
1) A one-hundred item long string array is going to look really gross defined in code like this
2) I am worried about how long it will take the application to open the activity if it has to generate such a large list of string values beforehand
To avoid this, I had the idea to define this string array in the strings.xml file and assign the strings to the list layout in the corresponding layout XML for the list activity. However, this so far doesn't seem to work; no errors but the android:entries command does not see to affect the layout at all. This is the XML. If anybody has knowledge of populating a list this way, I would appreciate your answers.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BuildingsActivity" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/buildings">
</ListView>
</RelativeLayout>
My other alternative is to retrieve it from a text file, which I will have to do for building details (as the volume of text is too large to bother saving as strings) but wanted to avoid for the list. Admittedly, I have not heavily researched this route (both because I had not really wanted to use it yet and based on what I have seen on other forums it looks difficult, but if there is a lot of support for this method then I will certainly appreciate any advice in using it.
Forgive me if any of this has already been covered in other threads. I DID search for similar topics, but nobody seemed to want to populate a list this way and a lot of the language from their questions went over my head (I am very new...this is my first real application, besides having worked through some of the tutorials on the developer website.
Java's ArrayList implements the Serializable interface, which allows you to save and load an array to and from a pure binary format using the functions writeObject and readObject. This page should be enough to get you started.
If you want to populate the listView with from a resource file you just have to use this in your code:
String[] values = getResources().getStringArray(R.array.<input_file>);
Once you have the values loaded you just have to assign it to the ArrayAdapter like this:
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
The second parameter is a predefined layout of android SDKs, you could use your own layout.
If your String[]'s ceiling is 100, I don't see it as a problem to just generate the array and pass it to the adapter. As for your concern about the code looking gross, have you considered storing this information in a database and then requesting it? For instance, you could have a Building Entity, with all of its information and persist it to MySQL using OrmLite. http://ormlite.com/javadoc/ormlite-core/doc-files/ormlite_1.html#SEC1 You could then write a getter than returns an array representation to avoid the ugly code.
First a little background about why I'm asking this question...
I'm working on a scheduling app for a machine shop. I have a database with tables for the machines, jobs and employees. It's possible that not all of these machines will have jobs or operators, so I needed a way to add "None" to the spinners (there are several reasons I won't go into for not adding that to the database itself). To accomplish this I settled on getting a cursor from the database and iterating over it, loading those bits I needed into an ArrayList and adding a "None" element at the end.
This works just fine, but now I've come upon a sticking point I can't seem to figure out.
The Job table has two columns (not counting the index): a job number and a part number. I created a custom layout to put them together and then use that in the spinner (so you would see Job # - Part # on each line of the spinner). Populating that from a cursor was simple. Trying to convert that to two arrays has me stumped.
I can get the data from the cursor into the two arrays with no problem, but I can't figure out how to relate them to the layout ids contained in the layout.
I'm new to android and long time ago schooled in Java, so if I've missed something obvious, please point it out!
listlayoutdouble.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:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="#+id/ListItem1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingLeft="12dip"
android:gravity="left|center_vertical"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/ListItem2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingRight="12dip"
android:gravity="right|center_vertical"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
What I have so far:
private void fillJobSpinner() {
Cursor jobsCursor = mDBHelper.fetchAllJobs();
startManagingCursor(jobsCursor);
ArrayList<String> jobnumbers = new ArrayList<String>();
ArrayList<String> jobnparts = new ArrayList<String>();
if (jobsCursor != null) {
operatorsCursor.moveToFirst();
while (operatorsCursor.isAfterLast() == false) {
jobnumbers.add(jobsCursor.getString(jobsCursor
.getColumnIndex(ScheduleDBAdapter.JOB_NUMBER)));
jobnparts.add(jobsCursor.getString(jobsCursor
.getColumnIndex(ScheduleDBAdapter.JOB_PART)));
jobsCursor.moveToNext();
}
jobnumbers.add("None");
jobparts.add("None");
}
}
I think what you need is a custom adapter for your spinner.
You need to extend BaseAdapter
http://developer.android.com/reference/android/widget/BaseAdapter.html
Then override the getView method.
A quick google turned up this tutorial:
http://app-solut.com/blog/2011/03/using-custom-layouts-for-spinner-or-listview-entries-in-android/
If I get the error "android.content.res.Resources$NotFoundException: Resource ID #0x7f050007 type #0x12 is not valid" can I find some what this resource is if I know its ID?
ListView list = (ListView)findViewById(R.id.messages_list_view);
list.setAdapter(new ArrayAdapter<String>(context,
R.layout.messages_list, headers));
messages_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/messages_list_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView android:id="#+id/messages_list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
I Got this error when using ListView in a Fragment.
Resolved by moving the setAdapter lines to the onViewCreated function of the Fragment. (makes sense that before the view is created the ListView is invalid).
so you get :
public void onViewCreated(View view,Bundle savedInstanceState){
ListView list = (ListView) getView().findViewById(R.id.thelist);
list.setAdapter(mAdapter);
}
For those whom other mentioned solutions don't work.
I did this silly mistake:-
setContentView(R.id.something);
Instead of
setContentView(R.layout.something);
Corrected that, and error was gone :D
You can either use the search function in eclipse, search for "0x7f050007" or go to projectfolder/gen/path/R.java that contains your resources.
You'll find something like this:
public static final int lineItem=0x7f07001c;
Then search for(in this example) lineItem with Eclipses search function. It'll take you to your resource in code.
Check your imports (at the top of your class-file). Maybe you imported
android.R
(which provides access to the platform-resources) instead of
{your_package_name}.R
(you can also leave it blank).
I've created an AutoCompleteTextView to search through a list of course titles (obtained from an sqlite db) and what I want to do is, when the user clicks on a title from the drop-down menu, the whole information from the database about his selection appears in a text view created below the AutoCompleteTextView.
I am pretty new to programming, especially for android and I would really appreciate it if someone could explain me how exactly to use setOnItemClickListener to call an instance in the database in the TextView below.
The code for the layout (R.layout.main_courses) is
<?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="wrap_content"
android:padding="5dp">
<AutoCompleteTextView
android:id="#+id/autocomplete_course"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Search for a course"/>
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/autocomplete_course"
android:hint="Information about the course will appear here" />
</RelativeLayout>
and the code for the AutoCompleteTextView I've written so far is:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_courses);
DataBase db = new DataBase(this.getApplicationContext());
db.openDataBase();
ArrayList<String> aCourses = db.getCoursesArr();
db.close();
AutoCompleteTextView search = (AutoCompleteTextView) findViewById(R.id.autocomplete_course);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_courses, aCourses);
search.setAdapter(adapter);
}
First of all you should try using a CursorAdapter instead of getting an array from it. Check this link for more info.
There is a method in AutoCompleteTextView that let you decide how many letters the user must type before the dropdown is shown, setThreshold. The issue is that it only allows >=1 values.
If you check this class src code, the good news is that the variable set by setThreshold() is only used in this method:
public boolean enoughToFilter() {
return getText().length() >= mThreshold;
}
So the first thing I would try is extending AutoCompleteTextView and override that method to always return true.
NOTE: Keep in mind that this might change in the future and it can get broken.