Reputation: 55
I have a Android studio project with a list view, that i want to fill with a array names but i can´t do it
MainActivity.java
public class MainActivity extends Activity {
@Override
// the array that i want to fill in listview
public String[]nombreSecciones = {"Perfil", "Juego", "Instrucciones", "Info"};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/secciones"
android:entries="@array/nombreSecciones"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
android:entries dont fill the listview with the items of the Arraylist
Upvotes: 0
Views: 122
Reputation: 839
Try this:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1, nombreSecciones);
listView.setAdapter(adapter);
Upvotes: 0
Reputation: 1736
Do it this way:
public String[] nombreSecciones = {"Perfil", "Juego", "Instrucciones", "Info"};
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.secciones);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, nombreSecciones);
listView.setAdapter(adapter);
}
Upvotes: 1
Reputation: 365
You have to define your array inside a XML file. In your case, it should look like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="nombreSecciones">
<item>Perfil</item>
<item>Juego</item>
<item>Instrucciones</item>
<item>Info</item>
</string-array>
</resources>
Save this file as res/values/arrays.xml and you're good to go.
If you want to have a non-static list of items, you have to implement an Adapter for the ListView.
Upvotes: 0