I want to use AutoCompleteTextView in android and read the official developer.android documentation about it.
There is a code snippet which looks like:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, COUNTRIES);
AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id.countries_list);
textView.setAdapter(adapter);
}
private static final String[] COUNTRIES = new String[] {
"Belgium", "France", "Italy", "Germany", "Spain"
};
I do not understand what the second parameter (android.R.layout.simple_dropdown_item_1line) in the constructor of ArrayAdapter means, where does this come from?
Is it a layout which is available from android or do I have to replace this layout with a layout created on my own and how to define this layout file in that case?
Concrete my code lokks like
xml:
<AutoCompleteTextView
android:id="#+id/search"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
java:
AutoCompleteTextView search =(AutoCompleteTextView) findViewById(R.id.search);
String[] vocabs = new String[1001];
//fill the String array
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line ,vocabs);
search.setAdapter(adapter);
They are calling the constructor of ArrayAdapter with 3 arguments (documentation): ArrayAdapter(Context context, int resource, T[] objects)
The resource R.layout.simple_dropdown_item_1line is just one of the default android framework layouts for dropdowns. See here a list of other default layouts.
EDIT to answer your 2nd question:
You can either use the default android layout (the example you provided should work) or a custom one defined by you. If it's this last case then just create a xml layout for this layout:
layouts/dropdown_custom_view.xml
<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/vocab_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="vocab"/>
</LinearLayout>
Then you can use the ArrayAdapter constructor ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects) pointing to your custom layout and to the TextView you want to be populated with vocabs:
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.dropdown_custom_view, R.id.vocab_text ,vocabs);
search.setAdapter(adapter);
Also check that you're populating the vocabs array well.
This layout which is available within Android System that's the reason you use android.R.It is used to show an item from the array adapter.It is basically a textview with some styling
You can use the custom layout for AutoCompleteTextView like
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, R.layout.custom_layout, R.id.text_title, COUNTRIES);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.countries_list);
textView.setAdapter(adapter);
custom_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e4e4e4"
>
<TextView
android:id="#+id/text_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
tools:text="AA"
android:padding="15dp"
/>
</LinearLayout>
Related
Firstly, I'm extremely unsure on the majority of Android coding, this is only my second day doing it, so please excuse any mistakes I make in my explanations!
I've got a database via SQLite and currently display my data via a listView. Currently, it works fine displaying the data and I can scroll through and click on it no issues. However, I'd like to place my data within the "Scrolling Activity" template you can find in Android Studio. From Googling around, I understand somewhat that you can't place a listView in a nestedScrollView, which is what the activity uses.
I am unsure, however, how to display my data from the database without using a listView. Could somebody please help me either convert the listView into something compatible, or explain a way to combine them (Hacky methods are fine for now!)
I've displayed all of the necessary code below.
MainActivity.java:
mydb = new DBHelper(this);
ArrayList array_list = mydb.getAllCotacts();
ArrayAdapter arrayAdapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1, array_list);
obj = (ListView)findViewById(R.id.listView1);
TextView emptyText = (TextView)findViewById(android.R.id.empty);
obj.setEmptyView(emptyText);
obj.setAdapter(arrayAdapter);
obj.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
// TODO Auto-generated method stub
int id_To_Search = arg2 + 1;
Bundle dataBundle = new Bundle();
dataBundle.putInt("id", id_To_Search);
Intent intent = new Intent(getApplicationContext(),DisplayContact.class);
intent.putExtras(dataBundle);
startActivity(intent);
}
});
content_scrolling.xml:
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="uk.ac.tees.q5065885.diary.ScrollingActivity"
tools:showIn="#layout/activity_scrolling">
<android.support.v7.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
/>
</android.support.v7.widget.LinearLayoutCompat>
<TextView
android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/emptyText"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
/>
</android.support.v4.widget.NestedScrollView>
Apologies for any mistakes, as I say, this is my second day; I'm learning as quick as I can!
You can create views programmatically and add them to LinearLayout on the fly. You can use any kind of view, but in this example I wanted to keep things clear so I assumed that your contacts are Strings and you want to just show them as TextViews. You can try something like this:
mydb = new DBHelper(this);
List<String> contacts = mydb.getAllCotacts();
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear_layout);
for(String data : contacts) {
//Create a view for an LinearLayout
TextView textView = new TextView(this);
//Do whatever you like with a view - add listener, adjust style, add text etc.
textView.setOnClickListener(...);
textView.setText(data);
textView.setGravity(Gravity.CENTER);
//Add textView to linearLayout
linearLayout.addView(textView);
}
xml (not very pretty, customize it if you like :))
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="uk.ac.tees.q5065885.diary.ScrollingActivity"
tools:showIn="#layout/activity_scrolling">
<LinearLayout android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/linear_layout"
android:orientation="vertical"/>
</android.support.v4.widget.NestedScrollView>
I transfrom arraylist to list view. It works but it doesn't show the first item. Here is my code. Help me please. Even I change the arraylist to String array, it doesn't work.
ListView list = (ListView) mView.findViewById(R.id.outputList);
ArrayList<String> listItem = new ArrayList<String>();
listItem.add("one");
listItem.add("two");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1,listItem);
list.setAdapter(adapter);
Layout is here.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/outputList"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
I'm having a problem with a custom ListView I am using in the ListActivity of my application. My problem is that all TextView items added to the ListView through an ArrayAdapter show up with a gray bar above them. I would include an image of the displayed ListView, but am unable since I do not have a reputation of 10 required by the stackoverflow site.
The layout file (index.xml) used to produce the ListView is defined as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical" >
<ListView
android:divider="#color/mg_red"
android:dividerHeight="1sp"
android:id="#android:id/list"
android:layout_height="fill_parent"
android:layout_width="fill_parent" />
<TextView
android:title="text_view"
android:background="#drawable/listitemback"
android:cacheColorHint="#drawable/listitemback"
android:id="#+id/listItem"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10sp"
android:textColor="#color/listitemtext"
android:textSize="16sp" />
</LinearLayout>
The ListActivity code used to display the list is as follows:
public class IndexListActivity extends ListActivity
{
private ListView m_listView = null;
#Override
public void onCreate(Bundle savedInstanceState)
{
try
{
if (MGApplication.DEBUG_BUILD)
Log.i("TMG", "IndexListActivity.onCreate");
super.onCreate(savedInstanceState);
// Get our global data object.
MGApplication mgApp = (MGApplication) getApplication();
// Set view layout
SetContentView(R.layout.index);
// Get references to our ListView and AdView objects
m_listView = (ListView) findViewById(android.R.id.list);
// Create a new ArrayAdapter object that will be used to initialize
// the underlying ListView object.
ArrayAdapter<String> aa = new ArrayAdapter<String>(this, R.layout.index,
R.id.listItem,
mgApp.m_strAZRhymeNames);
// Assign array adapter
m_listView.setAdapter(aa);
}
catch (Exception e)
{
}
return;
}
}
Any help is greatly appreciated as I am at my wits end with this issue. I think I've tried every suggestion I could find on the web and I am unable to remove the gray bar.
Thank You,
Bob
you have to set this property in test.xml file with.
<ListView
android:id="#+id/listview_middle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="1dip"
android:layout_weight="1"
android:background="#ffffff"
android:cacheColorHint="#android:color/transparent"
android:choiceMode="singleChoice"
android:scrollingCache="false" />
and set some property in listview object.
lv.setVerticalFadingEdgeEnabled(false);
In my side that's work perfect.
Someone could tell me how can I create a ListView without using a ListActivity?
I need to put in my layout several other views, and I wish the layout was not fully occupied by the ListView. It must be part of the layout, not the entire layout. I want to create a normal Activity.
Code snippet:
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.savings);
//...
ListView lvPoupanca = (ListView)this.findViewById(R.id.lvPoupanca);
// the adapter
TestRepository repo = new TestRepository(this);
Cursor c = repo.getCursor();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list_item, c, new String[]{"Nome", "ValorAtual"}, new int[] {R.id.tvNome, R.id.tvValorTotal});
lvPoupancas.setAdapter(adapter);
// ...
}
Edit:
The saving.xml file:
<?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="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/lvPoupancas"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout>
Hope somebody can help me.
Thanks and sorry about my bad english.
Like with any other View in any Activity...
ListView l = new ListView(this);
// OR
ListView l = (ListView) findViewById(R.id...);
...
l.setAdapter(...);
// If using first way: setContentView(l);
For example
I have replied to a similar question before. But that was in a different context. So, I'm putting some reference code here. It will help you to fix the issue.
public class ListandtextActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String [] str = {"ONE","TWO","THREE"};
final ListView lv = (ListView) findViewById(R.id.listView1);
final TextView tv = (TextView)findViewById(R.id.tv1);
ArrayAdapter<Object> adapt = new ArrayAdapter<Object>(getApplicationContext(), android.R.layout.simple_list_item_1, str);
lv.setAdapter(adapt);
lv.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
tv.setText("You Clicked Something");
}
});
}
}
And the XML is
<?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="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
<TextView
android:id="#+id/tv1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" android:textSize="20dip"/>
</LinearLayout>
By the way, before asking questions, collect as much information as possible for others to help you. If it does not work, it will be better to put the error log in the question rather than just saying it does not work.
Don't Extend ListActivity then. Extend an Activity and in onCreate() put the following code as per your requirement.
setContentView(R.layout.ur_xml);
Listview list = (ListView)findViewById(R.id.lvPoupancas);
list.setAdapter(your Adapter);
I have created four tabs using tabhost, and placed four listviews in each like below:
public class prem extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
String[] names = new String[] { "Pr"};
this.setListAdapter(new ArrayAdapter<String>(this,
R.layout.simple_list_item_checked, names));
}
Problem is I have created background images for each listview but when I scroll the listview goes black.I know that I should add android:cacheColorHint="#00000000"
to the xml file to make the listview transparent, so I have created a new xml and id
and tried to add android:cacheColorHint="#00000000" in the xml to make transparent, but it just force closes;
this.setListAdapter(new ArrayAdapter<String>(this,
R.layout.list_item, R.id.listb, names));
?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">
<TextView android:text="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30px"></TextView>
<ListView android:id="#+id/listb"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
</ListView>
</LinearLayout>
Have you tried to add setCacheColorHint(00000000) in the prem java file?
ListView lv = getListView();
lv.setCacheColorHint(00000000);
lv.setAdapter(new ArrayAdapter<String>(this,
R.layout.simple_list_item_checked, names));
android:cacheColorHint=#00000000 should do the trick. Where in your layout XML did you put it? It should go in ListView, for example:
<ListView
...
android:cacheColorHint="#00000000"
...
/>
The Android Developers' blog had a post about that a while ago. According to their post "Why is my list black? An Android optimization", all you need to do is add the android:cacheColorHint="#00000000" attribute to the ListView element.