有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java列表视图和下拉菜单安卓

我有一个eclipse生成的actionBar下拉菜单和一组数组。我想打印一个数组,其索引与用户选择的节号相同。下面的代码打印所有部分中的最后一个数组。我以前用SimpleTextView做过这个,但我需要用ListView

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(),
                    安卓.R.layout.simple_list_item_1,
                    安卓.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 安卓.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;
    }
}
}

共 (1) 个答案

  1. # 1 楼答案

    我想的是,你会从getArguments().getInt(ARG_SECTION_NUMBER)得到一个“节号”。然后,如果节号=0,则使用pirmadienis
    填充listview 否则,如果节号=1,则使用antradienis填充listview,依此类推

    给你:-

        /*for (int i = 0; i < dienos.length; i++) {
            String[] today = dienos[i];
            populateListView(today);
        }*/
    
        dummyTextView.setText(Integer.toString(getArguments().getInt(
                ARG_SECTION_NUMBER)));
        // instead of the above loop, add this line
         populateListView(dienos[getArguments().getInt(ARG_SECTION_NUMBER)]);// if section_number starts with 1, then dienos[getArguments().getInt(ARG_SECTION_NUMBER)-1]
        return rootView;