Android listview and list view template - android

I'm begining android developing and I would ask a question:
StableArrayAdapter adapter = new StableArrayAdapter(this,
android.R.layout.simple_list_item_1, list);
I'm making here a adapter which layout is a builded layout.simple_list_item_1.xml and looks that:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:minHeight="?android:attr/listPreferredItemHeightSmall" />
but if I want to make my own list item named "symply" in layout folder with this same xml code Android-Studio tells me that "Cannot resolve method symply". Of course i changed to
StableArrayAdapter adapter = new StableArrayAdapter(this,
android.R.symply, list);
What I should do?

You have to use R.layout.symply and not android.R.symply

Related

ListView doesn't show any added items

I'm trying to add some items to ListView - they aren't showing.
I realize this question has been asked many times, but I tried to follow any advice I found there - none worked for me.
Here is content of my onCreate method of public class MyListActivity extends AppCompatActivity ->
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
((EditText) findViewById(R.id.editText2)).setInputType(InputType.TYPE_NULL);
ListView view = (ListView) findViewById(R.id.myList);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
view.setAdapter(adapter);
adapter.add("aaaaa");
adapter.add("bbbbb");
adapter.notifyDataSetChanged();
And here's my associated layout XML ->
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
tools:context="com.example.kuba.myapplication3.MyListActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay"
>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/background_dark"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="#string/txt_searching"
android:textColor="#android:color/white" />
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ButtonTxt" />
</LinearLayout>
<ListView
android:id="#+id/myList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:dividerHeight="1dp" />
</LinearLayout>
Well and like I said, "aaaa" and "bbbb" don't show anywhere.
Thanks in advance for any advice!
Your activity code is perfect. The problem is in your layout file and very silly one.
You set your main LinearLayout background as background_dark. And your ListView Text color is also dark. So it is not visible.
Try background color as background_light and your ListView will be visible.
Or, If you want to change ListView Text Color then do as following:
1) Create a Custom Layout as below : (custom_list_layout.xml)
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tvlist"
android:textColor="#color/white"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_height="fill_parent"/>
2) Set this to your Array Adapter:
adapter = new ArrayAdapter<>(this, android.R.layout.custom_list_layout);
Then your ListView with white text will be visible in dark background. Hope this helps.
I see you are initializing the adapter without a reference to its set of items and it may be the problem. Please try this code instead:
Class field:
private List<String> items;
onCreate method:
...
items = new ArrayList<>();
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, items);
view.setAdapter(adapter);
items.add("aaaaa");
items.add("bbbbb");
adapter.notifyDataSetChanged();
...
Or you could simply initialize the items list with the two elements instead of adding them later.
I'm not sure, but i think that sometimes UI elements modifications may be executed on another threads. Therefore, maybe when you add elements then notifyDataSetChanged(), views are not shown neither displayed yet, therefore, nothing is done. Then the adapter try to display something and has no elements.
I'g suggest two things :
-First, try to add elements to adapter list (like Juan Martinez answer) BEFORE adapter declaration and setAdapter(adapter);. Then create your adapter, set adapter.
-Second, try to add elements and notifyDataSetChanged on "onStart" instead of "onCreate()".
First test will allow you to see if the problem is in constructors, threads, etc. Second test will show you if you can add elements a little later in order to have things working if first test shows that you cannot add elements immediatly after "setAdapter".
It seems that your code is made for example or little tests, so, without knowing where you really need add objects and what is the display problem it may be hard to give you precise code.
on Oncreate method :
items = new ArrayList<>();
items.add("aaaaa");
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, items);
view.setAdapter(adapter);
items.add("bbbbb");
adapter.notifyDataSetChanged();
Override onStart too
#Override
public void onStart() {
super.onStart();
adapter.add("cccc");
adapter.notifyDataSetChanged();
}
if your view shows only aaaa and cccc, then you cannot add items just after setAdapter. If nothing still show, the problem is somewhere else.

Android spinner item disappear during scroll

I have created two spinner, but when I add a custom layout to adapter, during scroll some items disappear.
xml file:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:padding="20sp"
android:textColor="#000000"
android:textAlignment="center"/>
java file:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_text, list);
spinner.setAdapter(adapter);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, R.layout.spinner_text, list2);
spinner2.setAdapter(adapter2);
How can i resolve the problem?
This is probably happens when you are supporting multi languages in you application, where different languages requires different layout direction.
Try adding android:layoutDirection to your text view in the custom spinner item layout and this will be fixed.

Adding custom layout to listview in android not recognized

I am trying to add a custom layout to my listview where I am calling:
mylistAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, carList);
I was told I am able to replace the simple_list_item_single_choice with a custom xml. I made a xml and added it to the layout folder in my project and called it mytextview.xml, here it is:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:paddingTop="2dip"
android:paddingBottom="3dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="2dp" />
My issue is that when I go to change simple_list_item_single_choice to mytextview, it does not find my custom layout file. Anybody have any idea how to correct this?
Just replace your code
mylistAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, carList);
With this
mylistAdapter = new ArrayAdapter<String>(this, R.layout.simple_list_item_single_choice, carList);
I hope this will help you.

Custom Listview with multiple lines per row

Basically, I need some rows to be 3 lines and others for the text to be centered on the middle line. I already have the 3 lines working. I just need to figure out how to check if the lines R.id.text2 and R.id.text3 are empty and set TAG_1 to be displayed on R.id.text2. I'm posting what I think it relevent. If more code is needed just let me know
ListAdapter simpleAdapter = new SimpleAdapter(this, makeList,
R.layout.custom_list_view,
new String[] { TAG_1, TAG_2, TAG_3 }, new int[] { R.id.text1, R.id.text2, R.id.text3 });
listView.setAdapter(simpleAdapter);
custom_list_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="19sp"/>
<TextView android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"/>
<TextView android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="fill_parent"/>
</LinearLayout>
SimpleAdapter is pretty useless for displaying anything but the most basic list. You will have much more success extending your own adapter. Start with an ArrayAdapter.
The basic concept is that you extend the adapter and implement its getView() function. In this you inflate (or reuse) a view for each row and populate it with your data.
There are many adapter tutorials on the internet, I found this one with a quick search.
Understanding how to utilize adapters is a fundamental part of android development.

How do I change the font color of the selection list in a spinner?

I've created a spinner, from where a user can select a value. I CAN change the textcolor of the spinner itself like shown in this picture:
Unfortunately when I press the spinner a list of items, to be selected, is shown, but these have a white font color on white background, and I just haven't been able to change this:
I've googled the problem and others have experienced the same problem. None of the fixes suggested worked for me.
As far as I understand I have to make a custom list of some kind, but well.. I'm not able to make it work. Any suggestions?
My code looks like this (array_spinner is a array of strings):
ArrayAdapter adapter = new ArrayAdapter(this, R.layout.row, array_spinner);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
And my row.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinnerText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textColor="#000000"
android:textSize="14dp" />
Try this (untested). I'm basically reproducing simple_spinner_dropdown_item.xml but with a black text color:
Replace the line
ArrayAdapter adapter = new ArrayAdapter(this, R.layout.row, array_spinner);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
with
ArrayAdapter adapter = new ArrayAdapter(this, R.layout.row, array_spinner);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
Add this file in res/layout, called spinner_dropdown_item.xml:
<CheckedTextView
android:id="#android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textColor="#FF000000"
/>
Simple and Crisp
private OnItemSelectedListener your_spinner _name = new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
((TextView) parent.getChildAt(0)).setTextColor(Color.BLUE);
}
public void onNothingSelected(AdapterView<?> parent) {
}
};
I was having this same issue. My answer isn't the most proper answer, but it's a very quick solution for those using a basic app theme.
If you are creating the arrayadapter for the spinner using
createfromResource()
Do NOT use getApplicationContext(). Declare Myclass.this instead. My text now stays the same as the basic app theme. Hopefully this will help someone else.

Categories

Resources