ListView 未显示在导航菜单中
ListView not displaying in Navigation Menu
我试图在我的导航菜单中显示一个字符串 ArrayList 中的 ListView,但它刚出现时是空白的,没有数据被传递到 ListView。
要访问 activity_main.xml 以外的新布局文件,我正在使用“ConstraintLayout layout1 = (ConstraintLayout) View.inflate(this, R.layout.nav_choose_foods, null);”然后,“ListView listView = layout1.findViewById(R.id.search_list);”
这样至少不会导致程序崩溃,貌似找ID没问题,就是ListView还是不显示。我这可能吗?或者有更好的方法来完成我在这里尝试的事情吗?
感谢您的帮助!
代码如下:
主要活动 -
public class MainActivity extends AppCompatActivity implements RecyclerViewAdapter_AllFoods.ItemClickListener, RecyclerViewAdapter_Nutrients.ItemClickListener, RecyclerViewAdapter_FoodTotals.ItemClickListener{
public DrawerLayout drawerLayout;
public ActionBarDrawerToggle actionBarDrawerToggle;
ArrayList<String> list;
ArrayAdapter<String> adapterList;
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Objects.requireNonNull(getSupportActionBar()).setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.custom_toolbar);
ActionBar action = getSupportActionBar();
action.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
drawerLayout = findViewById(R.id.my_drawer_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.nav_open, R.string.nav_close);
ConstraintLayout layout1 = (ConstraintLayout) View.inflate(this, R.layout.nav_choose_foods, null);
// pass the Open and Close toggle for the drawer layout listener
// to toggle the button
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
// to make the Navigation drawer icon always appear on the action bar
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Pineapple");
adapterList = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,list);
ListView listView = layout1.findViewById(R.id.search_list);
listView.setAdapter(adapterList);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<!--the root view must be the DrawerLayout-->
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/my_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="HardcodedText">
<com.google.android.material.navigation.NavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left"
app:menu="@menu/navigation_menu"
/>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
tools:context=".MainActivity">
</androidx.drawerlayout.widget.DrawerLayout>
nav_choose_foods.xml
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants">
<ListView
android:id="@+id/search_list"
android:layout_width="295dp"
android:layout_height="254dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.482"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/searchView"
app:layout_constraintVertical_bias="0.05" />
</androidx.constraintlayout.widget.ConstraintLayout>
navigation_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/menu"
>
<group android:checkableBehavior="single">
<item
android:title="Choose Foods">
<menu>
<group
android:id="@+id/menu_top"
android:checkableBehavior="single">
<item
app:actionLayout="@layout/nav_choose_foods"
android:id="@+id/nav_radios"
android:title=""
/>
</group>
</menu>
</item>
<item
android:title="Activity Level">
<menu>
<group
android:id="@+id/menu_bottom"
android:checkableBehavior="single">
</group>
</menu>
</item>
</group>
</menu>
好吧,如果这对任何人有帮助,我就放弃使用“navigation_menu.xml”文件了。相反,我只是在“com.google.android.material.navigation.NavigationView”下的 activity_main.xml 文件中使用了包含布局。无需充气或任何东西,id 没有问题。不确定这是否是最好的方法,但它有效。
如果有人对此有更好的解决方案,请告诉我。
我试图在我的导航菜单中显示一个字符串 ArrayList 中的 ListView,但它刚出现时是空白的,没有数据被传递到 ListView。
要访问 activity_main.xml 以外的新布局文件,我正在使用“ConstraintLayout layout1 = (ConstraintLayout) View.inflate(this, R.layout.nav_choose_foods, null);”然后,“ListView listView = layout1.findViewById(R.id.search_list);”
这样至少不会导致程序崩溃,貌似找ID没问题,就是ListView还是不显示。我这可能吗?或者有更好的方法来完成我在这里尝试的事情吗?
感谢您的帮助!
代码如下:
主要活动 -
public class MainActivity extends AppCompatActivity implements RecyclerViewAdapter_AllFoods.ItemClickListener, RecyclerViewAdapter_Nutrients.ItemClickListener, RecyclerViewAdapter_FoodTotals.ItemClickListener{
public DrawerLayout drawerLayout;
public ActionBarDrawerToggle actionBarDrawerToggle;
ArrayList<String> list;
ArrayAdapter<String> adapterList;
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Objects.requireNonNull(getSupportActionBar()).setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.custom_toolbar);
ActionBar action = getSupportActionBar();
action.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
drawerLayout = findViewById(R.id.my_drawer_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.nav_open, R.string.nav_close);
ConstraintLayout layout1 = (ConstraintLayout) View.inflate(this, R.layout.nav_choose_foods, null);
// pass the Open and Close toggle for the drawer layout listener
// to toggle the button
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
// to make the Navigation drawer icon always appear on the action bar
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Pineapple");
adapterList = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,list);
ListView listView = layout1.findViewById(R.id.search_list);
listView.setAdapter(adapterList);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<!--the root view must be the DrawerLayout-->
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/my_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="HardcodedText">
<com.google.android.material.navigation.NavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left"
app:menu="@menu/navigation_menu"
/>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
tools:context=".MainActivity">
</androidx.drawerlayout.widget.DrawerLayout>
nav_choose_foods.xml
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants">
<ListView
android:id="@+id/search_list"
android:layout_width="295dp"
android:layout_height="254dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.482"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/searchView"
app:layout_constraintVertical_bias="0.05" />
</androidx.constraintlayout.widget.ConstraintLayout>
navigation_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/menu"
>
<group android:checkableBehavior="single">
<item
android:title="Choose Foods">
<menu>
<group
android:id="@+id/menu_top"
android:checkableBehavior="single">
<item
app:actionLayout="@layout/nav_choose_foods"
android:id="@+id/nav_radios"
android:title=""
/>
</group>
</menu>
</item>
<item
android:title="Activity Level">
<menu>
<group
android:id="@+id/menu_bottom"
android:checkableBehavior="single">
</group>
</menu>
</item>
</group>
</menu>
好吧,如果这对任何人有帮助,我就放弃使用“navigation_menu.xml”文件了。相反,我只是在“com.google.android.material.navigation.NavigationView”下的 activity_main.xml 文件中使用了包含布局。无需充气或任何东西,id 没有问题。不确定这是否是最好的方法,但它有效。
如果有人对此有更好的解决方案,请告诉我。