У меня есть раскрывающееся меню ActionBar сгенерированное затмением и массив массивов. И я хочу напечатать массив, индекс которого совпадает с номером раздела, который выбрал пользователь. Приведенный ниже код печатает последний массив во всех разделах. Мне удалось сделать это с помощью простого TextView раньше, но мне нужно сделать это с помощью ListView.Меню списка и выпадающего меню android
public class MainActivity extends FragmentActivity implements
ActionBar.OnNavigationListener {
/**
* The serialization (saved instance state) Bundle key representing the
* current dropdown position.
*/
private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up the action bar to show a dropdown list.
final ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
// Set up the dropdown list navigation in the action bar.
actionBar.setListNavigationCallbacks(
// Specify a SpinnerAdapter to populate the dropdown list.
new ArrayAdapter<String>(getActionBarThemedContextCompat(),
android.R.layout.simple_list_item_1,
android.R.id.text1, new String[] {
getString(R.string.title_section1),
getString(R.string.title_section2),
getString(R.string.title_section3),
getString(R.string.title_section4),
getString(R.string.title_section5),}), this);
}
private void populateListView(String[] arr) {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.da_item, arr);
ListView list = (ListView) findViewById(R.id.listViewMain);
list.setAdapter(adapter);
};
/**
* Backward-compatible version of {@link ActionBar#getThemedContext()} that
* simply returns the {@link android.app.Activity} if
* <code>getThemedContext</code> is unavailable.
*/
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private Context getActionBarThemedContextCompat() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
return getActionBar().getThemedContext();
} else {
return this;
}
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
// Restore the previously serialized current dropdown position.
if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
getActionBar().setSelectedNavigationItem(
savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
// Serialize the current dropdown position.
outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar()
.getSelectedNavigationIndex());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onNavigationItemSelected(int position, long id) {
// When the given dropdown item is selected, show its contents in the
// container view.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position);
fragment.setArguments(args);
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, fragment).commit();
return true;
}
@SuppressLint("ValidFragment")
public class DummySectionFragment extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_dummy,
container, false);
TextView dummyTextView = (TextView) rootView
.findViewById(R.id.section_label);
String[] laikai = new String[] {"8:30 - 10:00", "10:30 - 12:00", "13:00 - 14:30",
"14:30 - 16:00", "16:00 - 17:30", "18:00 - 19:30"};
String[] pirmadienis = new String[] {"x", "y"};
String[] antradienis = new String[] {"a", "b"};
String[] treciadienis = new String[] {"c", "d"};
String[] ketvirtadienis = new String[] {"e", "f"};
String[] penktadienis = new String[] {"g", "h"};
String[][] dienos = {pirmadienis, antradienis, treciadienis, ketvirtadienis, penktadienis};
for (int i = 0; i < dienos.length; i++) {
String[] today = dienos[i];
populateListView(today);
}
dummyTextView.setText(Integer.toString(getArguments().getInt(
ARG_SECTION_NUMBER)));
return rootView;
}
}
}
Почему вам нужен «массив массива» в то время как существует единственный элемент внутри каждого индивидов? – Nizam
ой, есть несколько элементов, я только что отредактировал, поэтому требуется меньше места, извините за запутанность – Lynce
Хорошо. теперь, где «номер раздела»? Вы получаете его внутри 'dummyTextView'? – Nizam