I am working on a first basic android app as a university project and I have an array list of names. I want it to be put into a ListView. The problem I have is nothing appears when I bring in the ListViewunder the xml document. I don't get the basic template for item1, sub item 1 etc.
Then when I set up the array to play in the list view still empty list. Below I have the xml document
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="wrap_content"
android:layout_height="277dp"
android:id="#+id/listView" android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" android:layout_marginTop="7dp"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
then here is the java code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
populateListView();
}
private void populateListView() {
String[] myItems = {"frank","george","alice", "anna"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.main, myItems);
ListView list = (ListView)findViewById(R.id.listView);
list.setAdapter(adapter);
}
Your problem is that you have the same layout R.layout.main for two different use.
You set the content view as setContentView(R.layout.main); where your ListView is but you also use this layout with your adapter as new ArrayAdapter<String>(this, R.layout.main, myItems);.
So with your code, you don't have any item layout to display your array myItems. You need to create another layout as R.layout.item_layout with a TextView inside (or use an android layout like android.R.layout.simple_list_item_1) for your adapter.
Note: to avoid some bugs with ListView, you have also to set the height to match_parent or fill_parent
Try this for your adapter:
ArrayAdapter<String> adapter=
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myItems);
The adapter has to inflate a TextView. You can make your own TextView (if you want to customize it) in XML.
Then it will be:
ArrayAdapter<String> adapter=
new ArrayAdapter<String>(this, R.layout.yourcustomtextview, myItems);
The default array adapter in Android takes the layout for the row as the second argument.
Change your ArrayAdapter constructor to something like this:
ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items)
Related
I have a problem regarding the spinner design. I am using this code to generate a dropdown spinner:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
LinearLayout layout = new LinearLayout(this);
ArrayList < String > spinnerArray = new ArrayList < String > ();
spinnerArray.add("one");
spinnerArray.add("two");
spinnerArray.add("three");
spinnerArray.add("four");
spinnerArray.add("five");
Spinner spinner = new Spinner(this);
ArrayAdapter < String > spinnerArrayAdapter = new ArrayAdapter < String > (this,
android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
layout.addView(spinner);
setContentView(layout);
}
It displays this:
How can I remove the black color that blocks the spinner?
layout.addView(spinner);
setContentView(layout);
When you dynamically add the view to the layout. you are missing some configuration. thats the reason you see a black box.
try the below code:
xml:
<Spinner
android:id="#+id/planets_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
activity:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
arraydata, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
Create a layout file simple_list.xml in your layout folder:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:text="Sample Text"
android:padding="5dp"
android:gravity="center"
android:textColor="#android:color/black"
android:background="#android:color/white"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"/>
And refer this in arrayadapter:
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,
R.layout.simple_list, spinnerArray);
Update 1 Add this:
spinner.setBackgroundColor(ContextCompat.getColor(getApplicationContext(),android.R.color.white));
update 2
Instead of using constraint use Linear once in your layout file:
<?xml version="1.0" encoding="utf-8"?>
<LineartLayout xmlns:android="schemas.android.com/apk/res/android" xmlns:app="schemas.android.com/apk/res-auto"
android:id="#+id/linearLayout"
xmlns:tools="schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> </LinearLayout>
Set id to LinearLayout (your root view), then use findViewById for this view and add spinner to this rootView and remove setContentView(layout);
Change it like this:
LinearLayout layout = new LinearLayout(this);
ArrayList<String> spinnerArray = new ArrayList<String>();
spinnerArray.add("one");
spinnerArray.add("two");
spinnerArray.add("three");
spinnerArray.add("four");
spinnerArray.add("five");
Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
layout.addView(spinner);
setContentView(layout);
Interestingly your code worked for me. So make sure that there is no element there or some other piece of code related to theme or colors.
Add a linearlayout to your activity_my layout and name it linearLayoutContainer . Then get a reference to it from code.
LinearLayout container = findViewById(R.id.linearLayoutContainer);
Spinner spinner = new Spinner(this);
container.addView(spinner);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
Edit3: My own item list layout:
<?xml version="1.0" encoding="utf-8"?>
<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"
/>
Edit2: Honestly, I might just make a for-loop that creates buttons for each file. This is way too much of a headache to be worth the time.
Edit: I'd like to emphasize the fact that I've copy+pasted my exact code into a new test app and it works fine. That might give you guys a clue.
After tons of debugging, I've narrowed down a problem. I'm trying to add items (files) to an ArrayList, then put that into an ArrayAdapter, and finally display the items in a ListView. The problem is that only the first added item is being displayed.
Here's how I was trying to do it:
ListView listView = (ListView) view.findViewById(R.id.templateFilesList);
ArrayList<String> templateList = new ArrayList<>();
File myDir = getContext().getFilesDir();
File[] templateFiles = myDir.listFiles();
int length = templateFiles.length;
for(int i = 0; i < length; i++){
templateList.add(templateFiles[i].getName());
}
ArrayAdapter arrayAdapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1,
templateList);
listView.setAdapter(arrayAdapter);
The templateFiles[] array properly gets the 8 files in Internal Storage (confirmed via logcat/debugger), but only the first item is displayed in the ListView. Here's a clearer look at the issue:
// If I replace the for loop with this:
templateList.add(templateFiles[1].getName());
templateList.add(templateFiles[0].getName());
Only templateFiles[1] is displayed. Similarly, if I do this:
templateList.add(templateFiles[0].getName());
templateList.add(templateFiles[1].getName());
Only templateFiles[0] is displayed. Any ideas on what's going wrong?
Here's my XML:
<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"
tools:context="com.template_housing.MyTemplatesFrag"
android:orientation="vertical"
android:gravity="center_horizontal"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/my_templates"
android:textSize="34sp"
android:textColor="#color/black"
android:background="#color/Gold1"
android:gravity="center_horizontal"
android:padding="5dp"
android:layout_marginBottom="10dp"
/>
<ListView
android:id="#+id/templateFilesList"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</ListView>
If you are updating the content of your adapter you should be calling notifyDataSetChanged() as well to make sure your adapter gets to know that your content did change
I copied your code and ran it in Android Studio. It seems NO problem.
Here is my activity code(same xml code), the only difference is that I use an Activity, you may use a Fragment.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.templateFilesList);
ArrayList<String> templateList = new ArrayList<>();
File myDir = getFilesDir();
File[] templateFiles = myDir.listFiles();
int length = templateFiles.length;
//case 1: in my project, length is 1, show only one element, see case2.
// for (int i = 0; i < length; i++) {
// templateList.add(templateFiles[i].getName());
// }
//case 2: try to add simple strings,
//because you said put simple strings in the list could cause the problem, too.
templateList.add("a1");
templateList.add("a2");
templateList.add("a3");
ArrayAdapter arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,
templateList);
listView.setAdapter(arrayAdapter);
}
}
I shared my code on GitHub.I strongly recommend you post your fully code on Github, let's review your code.
Try to convert it to an array of Strings:
String [] templateListArray = templateList.toArray(new String[templateList.size()]);
and then do the assignment as follows:
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1,
templateListArray);
Hope this will fix it.
Can you post the simple list item 1 XML file too. This issue is not Java code related it has something to do with XML and size of elements. Try deleting that text view on top of the list view and your simple list item view should have wrap content as layout height.
And if you still want to use the textview you can use weight attribute. Give your textview a weight of 0 and the list view a weight of 1.
It may be possible that your array adapter is not setting properly and therefore it is not working properly, so try to replace your array adapter by below code and check if it is working,
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, templateList);
I've created a spinner using the code below. Now I want to use a drawable resource called circle.xml, to set the background for each item in the spinner so that each number appears inside a circle.
Spinner equationSpinner = new Spinner(this);
ArrayList<String> spinnerArray = new ArrayList<String>();
spinnerArray.add("");
spinnerArray.add("1");
spinnerArray.add("2");
spinnerArray.add("3");
spinnerArray.add("4");
spinnerArray.add("5");
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
equationSpinner.setAdapter(spinnerArrayAdapter);
I can use the circle as a background in a TextView so I tried creating the spinnerArray as an array of TextViews but that appeared to be incompatible with the simple_spinner_dropdown_item.
Anybody have an idea how I can do this? I just want each number to appear inside a circle in the spinner. I will also need to be able to access the number selected in the spinner.
Thanks
P.S. Is there any way in Android to create a 'slot machine' style spinner like you have in iOS? I prefer the look of those.
EDIT:
I've now found that I can use my circle resource by creating a custom layout (called spinner_item.xml) like this:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="28dip"
android:layout_height="28dip"
android:gravity="center"
android:textSize="20sp"
android:background="#drawable/circle"
android:textColor="#ff0000" />
I then set the adapter for my spinner programmatically like this:
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.equations, R.layout.spinner_item);
adapter.setDropDownViewResource(R.layout.spinner_item);
equationSpinner.setAdapter(adapter);
However, this method requires that I have a predefined string array set up in my strings.xml file (R.array.equations). I need to be able to specify the array of strings to be used programmatically depending on the state of the app. Can I modify the array once its set up?
EDIT 2:
Additionally I've found I can set the background of the dropdown items and use my own programmatical array like this:
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_item);
equationSpinner.setAdapter(spinnerArrayAdapter);
The only problem then is being able to apply the resource to the background of the spinner itself (not the drop down items). There does not appear to be an equivalent to setDropDownViewResource for the main spinner item.
lets say textview_background.xml is the background you want to give to the textView to Spinner
make a file in drawable and name it this textview_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#android:color/holo_blue_dark" />
<corners android:radius="7dp" />
<stroke
android:width="3dp"
android:color="#android:color/tertiary_text_light" />
</shape>
make a Spinner in your activity
<Spinner
android:id="#+id/SpinnerOneActivity_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
make a layout with name list_text_view.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:padding="4dp"
android:text="some"
android:background="#drawable/textview_background"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#android:color/black" />
make a class which will fill data from a list to your spinner, in this code your Adapter class is taking list of String but you can have your own custom object list given to it to populate the spinner, watch out how adapter's constructor parameters.
first-Context
Second-layout which will be given to each item in spinner
Third-a textView id from the above layout
Forth-your list of objects which you want to populate
TestAdapter.java
public class TestAdapter extends ArrayAdapter {
private ArrayList<String> strings;
private Context context;
private LayoutInflater layoutInflater;
public TestAdapter(Context context, int resource,int id, ArrayList<String> strings) {
super(context, resource, id, strings);
this.context = context;
this.strings = strings;
this.layoutInflater = LayoutInflater.from(this.context);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = layoutInflater.inflate(R.layout.list_text_view, parent, false);
TextView textView = (TextView) view.findViewById(R.id.text1);
textView.setText("" + strings.get(position));
return view;
}
}
in your activity you can write this code
ArrayList<String> list = new ArrayList<>();
list.add("one");
list.add("Two");
list.add("Three");
list.add("four");
Spinner spinner = (Spinner) findViewById(R.id.SpinnerOneActivity_spinner);
spinner.setAdapter(new TestAdapter(getApplicationContext(), R.layout.list_text_view, R.id.text1,list));
As you wanted you can increase the list or generate things dynamically to add or remove from the list once your list is attached to adapter which will be provided to Spinner you can call notifyDataSetChanged() on adapter to updated the list
Check this answer about Android Wheel: https://stackoverflow.com/a/9503372/3677394
There's a very good 3rd party open source project called Android Wheel
that pretty much does everything involved in creating a slot machine
for you. And if you want to customise it, it's under an Apache
licence, so no issues there.
This question already has answers here:
Your content must have a ListView whose id attribute is 'android.R.id.list'
(7 answers)
Closed 8 years ago.
I am new to Android. when I run my project, I got the following error:
Your content must have a ListView whose id attribute is 'android.R.id.list'
it crush on this line:
setContentView(R.layout.activity_main);
my activity_main.xml
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.soft8.sql_test.MainActivity" >
<ListView
android:id="#+id/TheClient"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
and main class:
public class MainActivity extends ListActivity {
private ArrayList results = new ArrayList();
SQLController dbcon;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView lv1 = (ListView) findViewById(R.id.TheClient);
dbcon = new SQLController(this);
dbcon.open();
dbcon.insertSomeItmes();
results = (ArrayList) dbcon.ReadData();
lv1.setAdapter(new CustomListAdapter(this, results));
dbcon.close();
}
Thank you.
Change this
android:id="#+id/TheClient"
to
android:id="#android:id/list"
There is no need to ListView Cast it because already extends your Activity with ListActivity
and also remove
final ListView lv1 = (ListView) findViewById(R.id.TheClient);
and directly set adapter by using
setListAdapter(new CustomListAdapter(this, results));
When you extend your activity from ListActivity, you don't need to define your list again.
The ListActivity automatically looks in the layout set for it for a ListView with id "list". If it does not find a ListView with id "list" it gives your error.
So change
android:id="#+id/TheClient"
to
android:id="#android:id/list"
and remove definition of your list from activity. (remove findViewById line)
Or just extend normal Activity instead of ListActivity.
One potential error will come from the fact that you use upper case in your id
android:id="#+id/TheClient"
try with lower case
android:id="#+id/the_client"
Rename the id of your ListView
android:id="#+id/TheClient"
to
android:id="#android:id/list"
Since you are using ListActivity your xml file must specify the keyword android while mentioning to a ID
and remove
final ListView lv1 = (ListView) findViewById(R.id.TheClient);
from activity code.
i.e. simply rewrite your code as
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbcon = new SQLController(this);
dbcon.open();
dbcon.insertSomeItmes();
results = (ArrayList) dbcon.ReadData();
setAdapter(new CustomListAdapter(this, results));
dbcon.close();
}
I am trying to populate a spinner but I seem to be missing something in my layout file
ArrayAdapter<String> cuisines = new ArrayAdapter<String>(this, R.layout.spinner_view,
getResources().getStringArray(R.array.cuisines));
I can't find R.layout.spinner_view and can only assume that I have to make it myself in my layout file. How do I do that?
Include spinner in xml file as:
<Spinner
android:id="#+id/spin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
and in activity:
Spinner spinner=(Spinner) findViewById(R.id.spin);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(YourActivity.this, android.R.layout.simple_spinner_item,R.array.cuisines);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
Use import com.companyname.product.R;
instead of
import android.R;
If you want to load with default spinner view, then use,
ArrayAdapter<String> cuisines = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
getResources().getStringArray(R.array.cuisines));