i have a problem with AutoCompleteTextView.
I use it in several activities, it works just fine with all of them but one, which is a dialog themed activity.
in this specific activity, the suggestion list doesn't show at all
this is my code:
<AutoCompleteTextView android:layout_height="wrap_content" android:id="#+id/etTesterName" android:completionThreshold="1" android:gravity="center" android:layout_width="fill_parent" android:layout_weight="1"></AutoCompleteTextView>
mTestersList = new ArrayList<Testers>();
mAdapter = new ArrayAdapter<Testers>(this, android.R.layout.simple_dropdown_item_1line, mTestersList);
((AutoCompleteTextView) findViewById(R.id.etTesterName)).setAdapter(mAdapter);
...
mTestersList = result;
mAdapter.notifyDataSetChanged();
mTestersList, is a list of Testers class which has the toString() overrided. this code works for all my activities but the dialog themed on..anyone has an idea why?
Vlad
hold reference of (AutoCompleteTextView) in some pointer that has visibility in activity:
AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) find......
autoCompleteTextView.setAdapter(mAdapter);
Related
I've been trying to figure out how to make my spinner work for days straight now, and can't find anything to help me. My spinner doesn't show the values. The spinner opens up, and the spaces for each value is there, but they're all blank. I have tried using an ArrayList<String>, and also a String[] array, neither of which made a difference, but do I need to use one over the other? I have tried many different types of ArrayAdapters, but none of them have worked. The one in my code below is the one that works the closest, (as described above), and the other 3 are suggestions I saw online. I think my main problem is in my array adapter, but I'm not certain.
Here is my java class
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info);
engineSpinner = (Spinner)findViewById(R.id.rockets);
engineNameList = StagesData.getEngineNameList();
engStrList = engineNameList.toArray(new String[engineNameList.size()]);
adapter = new ArrayAdapter<String>(InfoActivity.this, android.R.layout.simple_spinner_item, engStrList);
// adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, R.array.array_engines);
// adapter = new ArrayAdapter<String>(InfoActivity.this, android.R.layout.simple_dropdown_item_1line, R.id.rockets, engStrList);
// adapter = ArrayAdapter.createFromResource(InfoActivity.this, android.R.layout.simple_spinner_item, R.array.engines);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
engineSpinner.setAdapter(adapter);
engineSpinner.setSelection(0);
Here is my xml spinner setup
<TableRow>
<TextView
android:text="#string/rocket"
android:padding="3dp"/>
<Spinner
android:id="#+id/rockets"
android:gravity="right"
android:entries="#array/engines"
android:clickable="true"
android:textColor="#color/colorblack"
android:spinnerMode="dropdown"
android:backgroundTint="#color/colorPrimary" />
</TableRow>
And finally, this is my string-array file I wanted to pull from (Do I put my string-array in it's own file in the values folder, or does it go inside the string file, I've seen both and don't know which, Thanks)
<?xml version="1.0" encoding="utf-8">
<resources>
<string-array name="engines">
<item>Ant</item>
<item>Dart</item>
<item>Dawn</item>
<item>Flea</item>
<item>Hammer</item>
<item>Kickback</item>
<item>MainSail</item>
<item>Mammoth</item>
<item>Nerv</item>
<item>Poodle</item>
<item>Puff</item>
<item>R.A.P.I.E.R</item>
<item>Reliant</item>
<item>Rhino</item>
<item>Skipper</item>
<item>Spark</item>
<item>Spider</item>
<item>Swivel</item>
<item>Terrier</item>
<item>Thud</item>
<item>Thumper</item>
<item>Twin Boar</item>
<item>Twitch</item>
<item>Vector</item>
</string-array>
</resources>
So to recap my questions, do I need to use ArrayList<String> or String[], how do I set up my ArrayAdapter, and where do I need to put my string-array?
Thank you in advance!
I recreate your sample and works fine for me changing
engStrList = engineNameList.toArray(new String[engineNameList.size()]);
adapter = new ArrayAdapter<String>(InfoActivity.this, android.R.layout.simple_spinner_item, engStrList);
to
engStrList = new String[]{"a","b","c"};
adapter = new ArrayAdapter<String>(InfoActivity.this, android.R.layout.simple_spinner_item, engStrList);
So my guess is that you should check what StagesData.getEngineNameList() is returning.
Also if you what to use your string array this is the right way
ArrayAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.engines));
Hope this helps
I have the same problem as with this thread. I attempted the answer in the comment but still no luck.
Here is my code:
Layout:
<AutoCompleteTextView
android:id="#+id/test"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:dropDownHeight="wrap_content"
/>
Java code:
String[] values = {
"abc_0", "abc_00", "abc_000",
"abc_1", "abc_11", "abc_111",
"abc_2", "abc_22", "abc_222",
"abc_3", "abc_33", "abc_333",
};
final AutoCompleteTextView actv = (AutoCompleteTextView) dialog.findViewById(R.id.test);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, values);
actv.setAdapter(adapter);
Upon typing "abc", it will display the suggestion list as expected. But on the last 2 items abc_33 and abc_333, they are blocked by the soft keyboard.
Is there any suggestion on how to have the list not to be covered by keyboard?
Try to put the AutoCompleteTextView into RelativeLayout parrent
I have an activity that uses setContentView to initiate a listview, and then I have an If-Else condition. In the If part of the statement I use a simpleAdapter to place a new layout in the listview and other data from a cursor. In the Else part, I just want to put a sentence in a label to provide some information. How can I do that? I tried parameters and textview but they didn't work. I tried to put another setcontentView but it can't work either.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);
doMySearch() is a cursor that extracts data from database based on a keyword input by user.
public void onPostExecute() {
SimpleCursorAdapter adapter;
Int count = doMysearch().getCount;
If (count >= 1){
adapter=new SimpleCursorAdapter(this, R.layout.activity_results,
doMySearch(), new String[] {
DB.COL_NAME,
DB.COL_CITY },
new int[] { R.id.lblName, R.id.lblCity },
0);
setListAdapter(adapter);
}
Else {
}
I just want a sentence that says "No data found" for the Else part. Thanks.
It is not clear for me what you are asking but here we go.
There is empty view setting in ListView for empty lists, you have to add another empty view in your current layout. Then you will set the empty view layout in code like below:
//add to your layout
<LinearLayout
android:id="#+id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="There is no item" >
</TextView>
</LinearLayout>
//set empty view
ListView listView = (ListView) container.findViewById(R.id.list);
LinearLayout emptyView = (LinearLayout) container.findViewById(R.id.empty);
listView.setEmptyView(emptyView);
As far as I understand the label doesn't show up because of the listview, right?`
EDIT:
Okay I think I get it now.
To keep it simple you could hide your ListView with myList.setVisibility(View.GONE);
Then make a premade Label visible with myLabel.setVisibility(View.VISIBLE) that already holds your error-string.
The harder way you could make a StringArray, like this in (for example) the strings.xml
<string-array name="error">
<item>No results found</item>
</string-array>
Then, in your else-block you can do somethink like that:
ListView lv = (ListView) findViewById(R.id.myList);
String[] content = getResources().getStringArray(R.array.error);
ArrayAdapter<String> typeList = new ArrayAdapter<String>(this, R.layout.simple_list_item_custom, content);
lv.setAdapter(typeList);
I'm writing an IM client and I need to download (from filesystem or network) and show new elements at the top of ListView (it is history of messages -- older messages are at the top, newer -- at the bottom). I implemented my own Adapter for ListView but I can't add new elements at the beginning of the list and redraw it. (notifyDataSetChanged() isn't good for me, because indexes of messages in ListView changes and android can't redraw it normally).
How do other apps do something similar?
I don't create special code for it, I am simply creating new Adapter for my ListView:
messagesListView.setAdapter(new MessagesListAdapter(this));
And redefine getView() and getCount() method in MessagesListAdapter (extends ArrayAdapter now).
My XML for ListView is
<ListView
android:id="#+id/dialog_messages_list"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_marginTop="#dimen/title_height">
</ListView>
And my XML for one element (one message) is
<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/dialogMessageText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:background="#drawable/dialog_message_in"
/>
<TextView
android:id="#+id/dialogMessageDatetime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""/>
</LinearLayout>
May be you need other code?
EDIT: I tried
messagesListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList));
(new Thread() {
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
arrayList.add(0, "qwer");
}
}).start();
But it also not seems good. I tried to call ((ArrayAdapter<String>)messagesListView.getAdapter()).notifyDataSetChanged(); in thread, but it makes exception.
I suggest reversing the order of the List to display the newest result first.
Run this example:
public class Example extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] array = {"oldest", "older", "old", "new", "newer"};
List<String> list = new ArrayList<String>();
Collections.addAll(list, array);
Collections.reverse(list);
// When you want to add new Strings, put them at the beginning of list
list.add(0, "newest");
ListView listView = (ListView) findViewById(R.id.list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter);
}
}
You don't have to override anything in the ArrayAdapter or ListView this way.
you can programmatically add some views in the class associated with your list view. For example:
To add stuff to a layout and make new elements:
TextView tv = new TextView(getApplicationContext());
EditText edit = new EditText(getApplicationContext());
relative.addView(tv);
relative.addView(edit);
This is to manipulate an existing element in the xml layout:
final TextView tv = (TextView) v.findViewById(R.id.listItem);
tv.setText("This an item I am changing");
If you look at some of the related questions, they will give your more information on this. But you can also checkout other people's custom listviews and adapters online. This one is really nice: http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
I have a Listview that will list the alarms which are in the database.I need to add a Toggle Button beside each list item to set the alarm on/off.
How can I add the Toggle Button in the ListView?
R.layout.alarm_list:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ListView android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<TextView android:id="#+id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/no_reminders"
android:textColor="#FFF"/>
</LinearLayout>
Java Code:
private void fillData() {
Cursor remindersCursor = aDbHelper.fetchAllAlarms();
startManagingCursor(remindersCursor);
// Create an array to specify the fields we want to display in the list
// (only TITLE)
String[] from = new String[] { AlarmDbAdapter.KEY_TITLE };
// and an array of the fields we want to bind those fields to (in this
// case just text1)
int[] to = new int[] { R.id.text1};
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter reminders = new SimpleCursorAdapter(this,
R.layout.alarm_row, remindersCursor, from, to);
setListAdapter(reminders);
}
R.layout.alarm_row:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text1"
android:padding="10dip" android:layout_width="242dp"
android:layout_height="wrap_content"/>
My project is getting delayed.
Help
There is no small snippet ans. to your problem. I assume you need to have multi-selection. Now here are the things you need.
Since you are using SimpleCursorAdapter, you should replace that with CursorAdapter. To do so you have to extend it as it is a abstract adapter. Once you done that you will be overriding two functions.
newView Where you will create your list item views by inflating R.layout.alarm_row (it should contain your toggle button too). You have make toggle button non-clickable.
bindView where you will set state of toggle button and text for your text view
That said this what you need on the Activity side.
You have make your ListView to multi-selection mode by android:choiceMode in xml or using setChoiceMode.
Now bindView will look like:
ListView lv = ((ListActivity)context).getListView();
// Containing all check states
SparseBooleanArray sba = lv.getCheckedItemPositions();
// I am using check box
cb.setChecked(false);
// Cursor is passed as an argument.
if(sba != null)
if(sba.get(cursor.getPosition()))
cb.setChecked(true);
Ref to docs:
CursorAdapter docs
http://developer.android.com/reference/android/widget/ListView.html
Try creating a custom adapter like:
public class YourAdapter extends BaseAdapter
and a custom layout for the rows with a toggle button (you will have to inflate the layout in the method geView).
Here you can find an example: http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
Hope this helps..