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.
Related
I am new to stack, but I figured I need help. I hope someone can help me :).
What I want to do is the following:
the user of the App is saving some database (db3) files via PC in a folder he has to create. In my case he needs to call it "My Custom Foler".
implement a Spinner which lets the user choose from all db3 files of this directory and then be able to do db_selected.getReadableDatabase
Just to put it in context: I am gonna implement a ContenProvider and the populate a ListView with Items from specific table using SQliteOpenHelper and LoaderManager.LoaderCallbacks<Cursor>. BUT THAT IS NOT MY QUESTION.
I want to setup this spinner. Maybe someone can help?
Here is my code so far with still a lot missing but the basic structure should be clear.
Greetings :)
I added:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
In activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/list">
</ListView>
<LinearLayout
android:id="#+id/login_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="8dp">
<Spinner
android:id="#+id/select_database"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>
MainActivity.class
public class MainActivity extends AppCompatActivity {
private final String DATABASE_PATH = "My Custom Folder";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//either we show login page or the populated ListView
ListView populatedListView = (ListView) findViewById(R.id.list);
View logInView = findViewById(R.id.login_view);
populatedListView.setEmptyView(logInView);
//How to setup a Spinner which lets select a ".db3" from a custom created Folder
Spinner spinner = findViewById(R.id.select_database);
//When Spinner item is clicked/touched I am gonna load it into my ListView
}
}
}
I'm completely new to Android programming, so I'm not really sure what I should be searching for. I have a LinearLayout element on one of my activities.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:id="#+id/comment_area"
android:orientation="vertical"/>
I have a JSON array of comments (userName, commentText, commentDate) that I want to add into this LinearLayout though a loop. I created a comment_view.xml layout and created a CommentWidget class extending LinearLayout. Frankly I have no idea if this is the correct approach, and I don't think it is because I can't get the comments to load in.
My class is
public class CommentWidget extends LinearLayout {
private String text;
public void setText(String text) {
this.text = text;
}
public CommentWidget(Context context){
super(context);
}
public CommentWidget(Context context,AttributeSet attrs){
super(context,attrs);
}
#Override
protected void onFinishInflate(){
super.onFinishInflate();
TextView textView=(TextView) findViewById(R.id.comment_text);
textView.setText(text);
}
}
My widget layout is
<?xml version="1.0" encoding="utf-8"?>
<com.myproject.CommentWidget xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/comment_text"/>
</com.myproject.CommentWidget>
Inside my loop on the activity I was calling:
CommentWidget w = new CommentWidget(this);
w.setText(comment.getText());
mtxtArea.addView(w);
But nothing shows up. Can someone point me in the right direction? I'm correctly receiving the JSON into an array already.
Update: Answer
Windsurfer's answer below set me on the right track to use a ListView for what I am trying to accomplish. Using his links and some searching I found out that extending the ArrayAdapter is the most appropriate for JSON type data. I ended up following the tutorial at the following link
https://devtut.wordpress.com/2011/06/09/custom-arrayadapter-for-a-listview-android/
You can very well extend the LinearLayout to do this, but Android already has a couple of widgets designed for this. I believe you're looking for a ListView to display an array of data. Rather than creating a new widget, take a look at how a ListView works. A ListView uses an adapter to bind data to it. You will still have to design the layout for a single comment item, but a lot of the heavy lifting is done by the ListView and it's adapter.
Here are some links to get you started:
http://developer.android.com/guide/topics/ui/layout/listview.html
http://www.vogella.com/tutorials/AndroidListView/article.html
Take a look at this link by Romain Guy who introduces ListViews too.
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/
I have a list of "categories" that are stored as Strings in an ArrayAdapter in my app. This much is pretty simple. The adapter is a field of the Activity and accessible everywhere. It is populated with values during onCreate().
I have a "Entry" Dialog that contains an AutoCompleteTextView which uses this adapter and works very well. This is basically a dialog to add new items to a list.
I also have a second Dialog which acts as a filter for my list and has a single Spinner which when the Dialog is created, uses the same ArrayAdapter. Here's the problem.
If I use the filter Dialog before the entry Dialog, the Spinner is populated with all of the items from the adapter. Works well.
Any and every time I use the entry Dialog, the AutoCompleteTextView works properly.
The problem comes if I use the entry Dialog and select an item in the AutoCompleteTextView when it suggests something. After selecting a suggested item in the popup, even if I cancel the entry Dialog, the next time I bring up the filter Dialog, the Spinner initially shows the item that was last selected from the AutoCompleteTextView (instead of the first item in the adapter), and only displays that single item in the Spinner's list if clicked/touched. The only way to fix it is to end the application and re-open it. I'm not getting any errors or anything in logcat that is helpful.
EDIT - Okay, I've removed the previous code to replace it with a simple test case that I produced. The bottom line is that I would like to know if this is a bug, or if if it is expected results. When a suggestion is selected in an AutoCompleteTextView, I can confirm that the ArrayAdapter that is linked to it has filtering applied so that it's count is affected and the only items shown if the adapter is accessed in a Spinner will be those that were filtered down to. I also added button to display a toast to show that the count is affected. Type "te" in the autocomplete, select either Test entry. They try the spinner or click the button to see the adapter's count is only 2.
So the final question is now... can the adapter's filter be reset (other than by typing in and clearing the AutoCompleteTextView)? I can't find any method to do this. I have worked around the problem in my actual app by setting up a temporary adapter, copying over the main adapters items and setting the views to use the temporary adapter instead. My phone is running 2.2, and I have tested as high as 2.3.3 API level 10 in an emulator.
The xml for main.xml
<?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="fill_parent" >
<AutoCompleteTextView android:id="#+id/actv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Spinner android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<Button android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Check It"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true" />
</RelativeLayout>
The code for MainActivity.java
public class MainActivity extends Activity {
/** Called when the activity is first created. */
AutoCompleteTextView actv;
Spinner spinner;
Button button;
String [] adapterList = {"Test1", "Test2", "Garbage1", "Garbage2"};
ArrayAdapter<String> adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, adapterList);
actv = (AutoCompleteTextView) findViewById(R.id.actv);
spinner = (Spinner) findViewById(R.id.spinner);
button = (Button) findViewById(R.id.button);
actv.setAdapter(adapter);
spinner.setAdapter(adapter);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "" + adapter.getCount() + " Items in Adapter", Toast.LENGTH_SHORT).show();
}
});
}
}
Looking at AutoCompleteTextView source code, it does filter the ArrayAdapter:
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/widget/AutoCompleteTextView.java#AutoCompleteTextView.performFiltering%28java.lang.CharSequence%2Cint%29
You may want to remove the filter on an onClick callback in the spinner:
Filter filter = adapter.getFilter();
filter = null;
(see the setAdapter method in AutoCompleteTextView, around line 600)
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/widget/ArrayAdapter.java#ArrayAdapter.getFilter%28%29
Let me know if it works
BTW, is the adapter going to change dynamically? You're "workaround" of two adapters might be a better option than messing with the filter. What if the user starts a word, then clicks the spinner, cancels it and goes back to the word? Now the autocompletion will not make sense, unless you save the filter and restore it somehow.
I do believe that two adapters (based on the same string array) is a better solution.
I am just starting out with Android and I am attempting my first test app, but I am a bit stuck.
I have an SQLite database and a user is selecting a picture and a reference to this a long with a short description is getting stored in the database.
This is all working fine, now I want to bind the images from to database to a gallery, but I am a bit confused how I do this.
My layout xml is as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Gallery
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/Weeksgallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button android:id="#+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text1"
android:layout_marginLeft="10dip"
android:text="Take a Picture"
android:onClick="TakePicture" />
</LinearLayout>
I want the gallery with a button below
My on create function I have the following:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mDbHelper = new DbAdapter(this);
mDbHelper.open();
Cursor cur = mDbHelper.fetchDaysImages();
//array of fields to display
String[] from = new String[] { mDbHelper.KEY_ROWID,mDbHelper.KEY_PICREF};
int[] to = new int[] { R.id.Weeksgallery };
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter images = new SimpleCursorAdapter(this,
R.layout.main, cur, from, to);
setListAdapter(images);
mDbHelper.close();
}
I am using the notepad tutorial as an example and my class extends ListActivity.
When I run it I get the error
Your content must have a ListView whose id attribute is 'android.R.id.list'
I'm assuming I am using the wrong type of binding, but I can't seem to find anywhere that tells me how to bind from an sqlite DB to a gallery.
I also want to add a reference to the rowId in the gallery so when the image is clicked it will open the relevant page (so it can show the message)
Can anyone help?
Thanks
Bex
Your Activity inherits ListActivity, right? When you use ListActivity, you must make sure there is a ListView whose id is "android.R.id.list" in the layout of your Activity, that is what the log told you.
If there is no ListView in your Activity, then don't use ListActivity. If you want to set adapter to Gallery, use Gallery`s setAdapter method.