0

私は Android の初心者です (プログラミングや Java でさえありません)。

フラグメントの使用方法を理解しようとしています。

デフォルトのスワイプ/アクションバーを使用して作成したプロジェクトがあります。必要な設定を処理するためにこれをさらに拡張しました....しかし、何が起こっているのか/これを修正する方法がよくわかりません。

/**
 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a DummySectionFragment (defined as a static inner class
        // below) with the page number as its lone argument.
        Fragment fragment = new DummySectionFragment();
        Bundle args = new Bundle();
        args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getCount() {
        // Show 8 total pages.
        return 8;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
        case 0:
            return getString(R.string.title_section1).toUpperCase(l);
        case 1:
            return getString(R.string.title_section2).toUpperCase(l);
        case 2:
            return getString(R.string.title_section3).toUpperCase(l);
        case 3:
            return getString(R.string.title_section4).toUpperCase(l);
        case 4:
            return getString(R.string.title_section5).toUpperCase(l);
        case 5:
            return getString(R.string.title_section6).toUpperCase(l);
        case 6:
            return getString(R.string.title_section7).toUpperCase(l);
        case 7:
            return getString(R.string.title_section8).toUpperCase(l);
        }
        return null;
    }
}

/**
 * A dummy fragment representing a section of the app, but that simply
 * displays dummy text.
 */
public class DummySectionFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public String ARG_SECTION_NUMBER = "section_number";

    public DummySectionFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        int position;

        position = getArguments().getInt(ARG_SECTION_NUMBER)-1;
        View rootView;
        TextView dummyTextView;

ここで静的または最終的なものは本当に必要ありません。ほとんどうまくいきましたが、次の行や修正方法がわかりません。私はそれが何をしているのかちょっと分かります。

args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);

エラー: 非静的フィールド DummySectionFragment.ARG_SECTION_NUMBER への静的参照を作成できません

これにはおそらく簡単な修正方法があります。現在の仕事は SQL Server ですべての時間を費やしているため、Android と Java に慣れていないだけです。

-- 編集された追加 私は静的または最終的なものなどに反対していません。私がよく理解していない問題は、これらのフラグメントのそれぞれで何かをしたいときです。これらのレイアウトのそれぞれにテキストビューがあり、それらをループで操作できるようにしたいと考えています。輪にはまって、抜け出せないと思います…笑。

たとえば、私が上に置いたコードの下には

    case 4:
            rootView = inflater.inflate(R.layout.fragment_main_location,container, false);
            dummyTextView= (TextView) rootView .findViewById(R.id.section_label);

            // location
            Button btnShowLocation = (Button) rootView.findViewById(R.id.btnShowLocation);
            Button btnShowDBLocList = (Button) rootView.findViewById(R.id.btnShowDBLocList);
            Button btnLocationsCount = (Button) rootView.findViewById(R.id.btnLocationsCount);
            Button btnTruncateDBLocationsTable = (Button) rootView.findViewById(R.id.btnTruncateDBLocationsTable);

            btnTruncateDBLocationsTable.setOnClickListener(new OnClickListener() {
                @Override                
                public void onClick(View v) {
                    Activity activity = getActivity();
                    int intCount = 0;

                    /*if (activity != null) {
                        //dummyTextView.setText("");
                     try {
                         locationDatabaseHandler.truncateLocationTable();
                         intCount = locationDatabaseHandler.getLocationCount();
                     } catch (Exception e){
                         //dummyTextView.append(e.toString());
                     }
                     //dummyTextView.append("Count:" + intCount + "\n\n");
                     Toast.makeText(activity, "toast_you_just_clicked_a_fragment btnTruncateDBLocationsTable button", Toast.LENGTH_LONG).show();
                    }*/
                }
            });

            dummyTextView = (TextView) rootView    .findViewById(R.id.section_label);
            dummyTextView.append("\nLocation Stuff\n");
            break;

//dummyTextView.append("Count:" + intCount + "\n\n");

私は、dummyTextView が onClick イベントで dummmyText を使用しようとすると、次の不平を言うエラーで静的 (クイック修正) にする必要があると言うサークルに遭遇します。別のメソッドで定義された inder クラス。

これを処理する変数を onCreate 内に追加して (LayoutInflater と Viewgroup に対して入力し、onclick でそれらを参照します (表示されていません)。 ...

私がここに到達していないものがあります。そのハードルを乗り越えたら、ボールを持って、自分のやりたいことを実行できるようになります.

4

2 に答える 2

0

つまり、ARG_SECTION_NUMBERとして宣言する必要がありますpublic static。として宣言した方が良い public static final

于 2013-10-29T20:34:52.150 に答える