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.
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've been searching and testing the whole day, but I didn't find any solution for this problem. I've got a ListView and I want to show some Views in that. But everytime I add a view, it only shows me the class or the id of the listItems instead of the Views themselves. Like:
android.widget.Button{40f090e0VFED.. C. ..........l. 0,0-0,0}
What am I doing wrong!? Do I have to set a certain tag!?
This is how my Listview is implemented in the xml-file
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:animateLayoutChanges="true"
android:clipChildren="true"
android:clipToPadding="true"
android:dividerHeight="1dp"
android:focusableInTouchMode="true"
android:headerDividersEnabled="true"
android:scrollbarStyle="insideInset"
android:scrollbars="vertical|horizontal"
android:splitMotionEvents="true" >
and this ist the java-code in the activity:
Button[] test = new Button[3];
test[0] = new Button((Activity) _mycontext);
test[1] = new Button((Activity) _mycontext);
test[2] = new Button((Activity) _mycontext);
test[0].setText("Test-Button 1");
test[1].setText("Test-Button 2");
test[2].setText("Test-Button 3");
_dt_columns.setAdapter(new ArrayAdapter<View>((Activity) _mycontext, android.R.layout.simple_list_item_1, test));
_dt_columns.notifyAll();
I also did it all the way with "extends ListActivity" and the Inflater, and it all shows up, but there's always just that text, not the views themselves.
ArrayAdapter populates ListView only with one TextView, each item may have some complex layout but the only difference between them will be a TextView value.
You are passing View elements as list items and adapter call toString method on them to populate this TextView, that is where You get this "strange" values from.
Create Your own xml layout resource for single list item with desired structure instead of using android.R.layout.simple_list_item_1.
And create Your asdapter like this
ArrayAdapter<YourObject> adapter = new ArrayAdapter<YourObject>(this, R.layout.listitem, R.id.textview_inside_listitem, YourObject[])
where YourObject is a class You created to hold data for single item and toString method return value You wish to display in list.
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 want to display some of the contents of my database to the user with a scroll ability.
So I have extended the ListActivity and I used the SimpleCursorAdapter. The problem is that i can not see the values displaced.
private void fillData() {
// Get all of the rows from the database and create the item list
ListData = data.findAllDbValues();
startManagingCursor(ListData);
// Create an array to specify the fields we want to display in the list
String[] from = new String[]{Data.ID, Data.DESC};
// and an array of the fields we want to bind those fields to
int[] to = new int[]{R.id.text1, R.id.text2};
// Now create a simple cursor adapter and set it to display
ListAdapter la = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, ListData, from, to);//R.layout.showdata
setListAdapter(la);
}
If I use a custom layout(R.layout.showdata),where it has only two TextViews, I can see the values but I have to touch the text which is displayed to do my job instead of just touching anywhere of the selection.
So I want to use a simple_list_item_2 so I can have this ability. Any ideas why I do not see the values or how can I make my custom layout to produce the same abilities with the simple_list?
This worked for me:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android_orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:paddingLeft="4dip"
android:paddingRight="5dp">
<TextView android:id="#+id/productRow"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
The trick is to wrap the TextView row in a LinearLayout. This lets me click anywhere on the list element to select it.
I know this question is old but I arrived here through Google and I think I have the answer since I had similar problem.
You are referencing custom TextView in your int[] to array, not the Android ones. You must use instead:
// and an array of the fields we want to bind those fields to
int[] to = new int[]{android.R.id.text1, android.R.id.text2};
Just check out that the ids in the XML and the ids in your class are the same. When you use custom views the reference in your class is
R.id.text1 and in your layout android:id="#+id:text1" but when you use Android resources the reference in your class is android.R.id.text1 and in your layout android:id="#android:id/text1".
You can use any of them but it must be consistent.
I was using android:id="#+id:text1" in my XML (creating a new TextView with id "text1") but using android.R.id.text1 in my Java Class so the CursorAdaptor was setting the information in a non-existent View and I was getting exactly that result: a listview with all the rows without text.
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.