0

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

3 Answers 3

1

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); 
} 
Sign up to request clarification or add additional context in comments.

2 Comments

i have a problem with listView eclipse said "cannot resolve symbol 'listView'
add this line: import android.widget.ArrayAdapter;
0

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.

Comments

0

Try this:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1, nombreSecciones); 
listView.setAdapter(adapter); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.