I have a list view on this XML:
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:layout_above="#+id/txtMsg" />
I want to add items to the list view in my code. This is my code:
ListView mesList = (ListView) findViewById(R.id.listView);
ArrayAdapter<String> adapter;
if(messages.size()>0) {
adapter = new ArrayAdapter<String>(this, android.R.layout.activity_list_item, messages);
}
messages is an ArrayList of String and is not empty. For some reason the list view is empty on the screen. What should I put instead of android.R.layout.activity_list_item? I think this is my problem. Thank you in advance!
set adapter of listview
mesList.setAdapter(adapter);
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, messages);
Related
I am building an Android application and I need to change the text color of all cells in the ListView.
This is the code I use to populate and connect the ListView with the Activity file:
protected void onCreate(Bundle savedInstanceState) {
...
// Create a temp array.
ListView lv = (ListView) findViewById(R.id.gamesList);
displayGames = new ArrayList<String>();
// Populates ListView
updateList();
// Create Array Adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,displayGames);
lv.setAdapter(adapter);
}
My problem is that the text of the TextView in the cells does not have the correct color even though I specifically changed it on the XML file. See this code below:
This is the XML code for the creation of the ListView:
<ListView
android:id="#+id/gamesList"
android:layout_width="396dp"
android:layout_height="342dp"
android:layout_marginStart="9dp"
android:layout_marginEnd="10dp"
app:layout_constraintBottom_toTopOf="#+id/linearLayout6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout5"
app:layout_constraintVertical_bias="0.854" />
I defined the Text cells in a new XML file called simple_list_item_1.xml.
<TextView
android:id="#+id/game"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#color/white"
android:textColorHint="#color/white"
tools:ignore="MissingConstraints" />
I am running the app on a Pixel 2 emulator using API 22 Android 5,1.
While setting Adapter to ListView you are referring android.R.layout.simple_list_item_1 which is default system resource and not the one you created in your xml. You need to use R.layout.simple_list_item_1
// Create Array Adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.simple_list_item_1,displayGames);
Change android.R.layout.simple_list_item_1 to R.layout.simple_list_item_1
// Create Array Adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.simple_list_item_1,displayGames);
I'm trying to show simple spinner
mSpinnerHeaderType = (Spinner) findViewById(R.id.spinner);
String[] items = new String[]{Constants.TYPE_112R, Constants.TYPE_314R};
ArrayAdapter<String> adapter = new ArrayAdapter<>(mContext, android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinnerHeaderType.setAdapter(adapter);
In layout xml
<Spinner
android:id="#+id/spinner"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|left"
android:spinnerMode="dropdown"></Spinner>
But when I click on spinner,
either 1. the dropdown list width is almost zero
OR 2. there are no itmes in dropdown
I tried, 1. giving spinner width as match_parent in xml layout and 2. using dropDownWith property for spinner etc, but nothing working
See image below:
What wrong I'm doing?
try this.
mSpinnerHeaderType = (Spinner) findViewById(R.id.spinner);
String[] items = {Constants.TYPE_112R, Constants.TYPE_314R};
ArrayAdapter<String> adapter = new ArrayAdapter<>(mContext, android.R.layout.simple_spinner_item, items);
mSpinnerHeaderType.setAdapter(adapter);
xml file
<Spinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dropdown"></Spinner>
I am trying this cool library that uses Ken Burn effect here (his github page)
I got it to work but I cant add different Icons on different rows. I tried to use Mkyongs CustomAdaptor example but what he used is a layout with TextView and ImageView. What I am using is ListView. I am already able to change the text font and size of the ListView but lost trying to add icons. How do you do this?
Below is my Layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:background="#drawable/blur4"
tools:context="com.gio.hia.hiarewired.NoBoringActionBarActivity">
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent" />
<FrameLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="#dimen/header_height">
<com.gio.hia.hiarewired.KenBurnsView
android:id="#+id/header_picture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/picture0" />
<ImageView
android:id="#+id/header_logo"
android:layout_width="#dimen/header_logo_size"
android:layout_height="#dimen/header_logo_size"
android:layout_gravity="center"
android:src="#drawable/ic_header_logo" />
</FrameLayout>
</FrameLayout>
This is my Activity Class
private void setupListView() {
ArrayList<String> FAKES = new ArrayList<String>();
//for (int i = 0; i < 1000; i++) {
FAKES.add("Row 1");
FAKES.add("Row 2");
FAKES.add("Row 3");
FAKES.add("Row 4");
//}
mPlaceHolderView = getLayoutInflater().inflate(R.layout.view_header_placeholder, mListView, false);
mListView.addHeaderView(mPlaceHolderView);
//mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, FAKES));
mListView.setAdapter(new ArrayAdapter<String>(this, R.layout.list_text_config, FAKES));
mListView.setOnScrollListener(new AbsListView.OnScrollListener() { ...
I used a custom R.layout.list_text_config to change the font
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:paddingTop="10dip"
android:paddingBottom="10dip"
android:paddingLeft="15dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="35dp"
android:textColor="#color/colorWhite"/>
In his example he used a layout inflater How do I use my Custom Adapter with a layout inflator or am I looking at the problem on the wrong direction?
I usually just do this
CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgid);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
but how do I do that when he used a layout inflater like below
mPlaceHolderView = getLayoutInflater().inflate(R.layout.view_header_placeholder, mListView, false);
mListView.addHeaderView(mPlaceHolderView);
mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, FAKES));
Like #Arslan said this can be done with a custom adapter. You are actually doing it correct
CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgid);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
That actually creates the list with the icons. What you need to add to that is the header image to the mPlaceHolderView but instead of using mListView use the list that you created with your "custom adapter". I placed the modified line of code from the original line of code so you can easily see the difference.
CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgid);
list=(ListView)findViewById(R.id.list);
mPlaceHolderView = getLayoutInflater().inflate(R.layout.view_header_placeholder, list, false);
//mPlaceHolderView = getLayoutInflater().inflate(R.layout.view_header_placeholder, mListView, false);
list.setAdapter(adapter);
Now and since your no longer using the mListView you should probably lookout for instances of mListView and replace it with your list
I need to extend the ListActivity and populate a custom layout so I do the following
public class BizList extends ListActivity {
String bizNames[]={"one", "two","theree","four"};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.bizlist);
//ListView listviewMe = (ListView)findViewById(android.R.id.list);
//ListView listview = this.getListView();
setListAdapter(new ArrayAdapter<String>(this, android.R.id.list, bizNames));
/* ListView list1 = (ListView) findViewById(R.id.listView1);
list1.setAdapter(new ArrayAdapter<String>(getApplicationContext(),R.layout.list_item,startDates_str));;*/
}
and my ListView in my layout is
<ListView
android:id="#id/android:list"//***have also tried*** android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/imageView2"
android:layout_centerHorizontal="true" >
</ListView>
but I keep getting this error
android.content.res.resources$notfoundexception: File form xml type layout resoucse ID
Any and all help is greatly appreciated. I hope it's something simple. thx agin.
Firstly for ListActivity to automatically find the list, the resource id MUST be...
android:id="#android:id/list"
Secondly, when you create the ArrayAdapter, don't pass the id of the ListView itself into the constructor. Instead you should pass the resource id of a list item layout.
Try this...
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, bizNames));
if you want to add it to the resources you have to write
android:id="#+id/listView" you forgot the "+"
<ListView
android:id="#+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/imageView2"
android:layout_centerHorizontal="true" >
</ListView>
And in you onCreate method
ListView lv = (ListView) findViewById(R.id.listView);
ArrayAdapter<String> myadpater = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, bizNames);
lv.setAdapter(myadpater);
hope it works now. please vote if correct
Hi i'm trying to add a relative view to a merge adapter but it's currently scrolling separetly to the lists so does anyone know how i can add a relative layout with an image view and a text view to a merge adapter?
my aim is to have it look like this
header(relativelyout red bar and title);
List
header(relativelyout red bar and title);
List
header(relativelyout red bar and title);
List
and to have this all scroll as if it was all one list
heres my attempt so far
arrayAdapter = new ArrayAdapter<String>(this, R.layout.single_item, newsList);
arrayAdapter2 = new ArrayAdapter<String>(this, R.layout.single_item, newsList2);
arrayAdapter3 = new ArrayAdapter<String>(this, R.layout.single_item, newsList3);
ListView list = getListView();
list.setTextFilterEnabled(true);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE );
View header = inflater.inflate( R.layout.redcell, list, false);
setListAdapter (arrayAdapter);
adapter = new MergeAdapter();
adapter.addView(header);
adapter.addAdapter(arrayAdapter);
adapter.addView(header);
adapter.addAdapter(arrayAdapter2);
adapter.addView(header);
adapter.addAdapter(arrayAdapter3);
setListAdapter(adapter);
}
redcell.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/redcelllayout" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="30dp"
android:src="#drawable/titlerect" />
<TextView
android:id="#+id/redheadertext"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="left"
android:paddingLeft="15dp"
android:paddingTop="3dp"
android:textColor="#color/white"
android:textSize="15dp"
android:textStyle="bold"/>
</RelativeLayout>
Instead of creating them in the program, create an xml file for that view, inflate it, then add it to the adapter.
Like this:
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View header1 = inflater.inflate(R.layout.redbar_title);
View header2 = inflater.inflate(R.layout.redbar_title);
View header3 = inflater.inflate(R.layout.redbar_title);
adapter = new MergeAdapter();
adapter.addView(header1);
adapter.addAdapter(arrayAdapter);
adapter.addView(header2);
adapter.addAdapter(arrayAdapter2);
adapter.addView(header3);
adapter.addAdapter(arrayAdapter3);
setListAdapter(adapter);
Set each list layout_height to wrap_content, and wrap everything within a ScrollView.
Perhaps it would be easier if you could make your headers part of the adapter itself, as separators, and use a single list instead of 3.