gpt4 book ai didi

java - 带数组的 ListView

转载 作者:行者123 更新时间:2023-12-01 10:55:53 24 4
gpt4 key购买 nike

我有一个带有 ListView 的 Android studio 项目,我想用数组名称填充它,但我做不到

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 不会用 Arraylist 的项目填充 ListView

最佳答案

这样做:

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);
}

关于java - 带数组的 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33614756/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com