2013-10-29 56 views
0

我是新来的Android,(不编程,甚至Java),所以忍受着我。Android的片段问题

我想要得到使用片段的句柄。

我有一个使用默认轻扫/操作栏创建的项目。我进一步扩展了这一点,以处理我想要的设置....但是我不太明白发生了什么/如何解决这个问题。

/** 
* 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

有可能是这一个简单的解决,我只是感到与Android和Java够陌生的,因为我的目前的工作我花了我所有的时间在SQL Server。

- 编辑添加 我不反对静态或最终等任何事情。我不是很明白的问题是当我想在每个片段中做些事情。我在每个布局上都有一个textview,我希望能够在循环中操作它们。我想我被困在一个圈子里,无法想出我的出路......哈哈。

例如我把上面的代码下面是

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 w /它说我需要使它变成静态的(快速修复),抱怨错误:无法引用non-在不同的方法中定义的indder类中的最终变量dummy7Text。

我已经添加了一个变量来处理onCreate内部的填充(LayoutInflater和Viewgroup,然后在onclick中引用它们(未显示),但是当我进入并instaniate ...没有与textviews发生...

有东西我不是很到这里,一旦我通过这个障碍,我会有这个由球,并将能够使它做我想做的事情。

回答

1

我真的不希望任何静态或最终这里

为什么?它们不会对性能产生负面影响,也不是糟糕的编码实践。

我不理解以下行

每个片段可以用含有任何数量的键 - 值对的捆绑来创建。DummySectionFragment.ARG_SECTION_NUMBER是一个键(一个字符串),并且position + 1是值。因此,这段代码告诉新的DummySectionFragment Fragment应该显示哪部分内容。

此方法比将这些参数放入构造函数中更可取,因为您不能保证调用Fragment的自定义构造函数。 Android可以通过多种方式生成碎片,这样可以降低出现NullPointerExceptions等问题的可能性。

错误的是:你似乎知道,DummySectionFragment.ARG_SECTION_NUMBER指的是静态字段名为ARG_SECTION_NUMBERDummySectionFragment类中不能使静态参考非静态字段DummySectionFragment.ARG_SECTION_NUMBER

。通过使该字段非静态,您不能再引用此常数值,而不需要DummySectionFragment实例。

另一种选择(如果你真的不想要一个静态字段)将硬编码字符串。因此,你的代码将是:

args.putInt("section_number", position + 1); 

然而,公共静态字段是一个更好的编码实践,并防止在你的字符串输入错误愚蠢的错误。

我碰上了cirle在那里,如果我dummyTextView尝试使用dummmyText瓦特/ onClick事件,它说,我需要一个抱怨错误,使其静态(快速修复):不能指在不同的方法中定义的indder类中的非final变量dummy7Text。

而不是使用匿名内部类,我会让你的片段实现OnClickListener

例如:

public class MyFragment extends Fragment implements OnClickListener { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     // ... 

     Button btnTruncateDBLocationsTable = (Button) rootView.findViewById(R.id.btnTruncateDBLocationsTable); 
     btnTruncateDBLocationsTable.setOnClickListener(this); 

     // ... 
    } 

    @Override 
    public void onClick(View v) { 
     // You can reference dummyTextView here without any problems 
    } 
} 
+0

好吧,我开始得到什么的布莱恩,请看到我的编辑和增加进一步澄清什么,我想要做一个轻微的手柄。我觉得我很接近。 – shaddow

+0

对于关于'OnClickListener'的其他问题,在这种情况下,我建议让你的Fragment实现'OnClickListener'而不是尝试使用匿名内部类。这样你就不必担心你的变量是静态的还是最终的,这样它们就可以被内部类使用。 –

+0

的例子,会有帮助吗?我以前用Java编写过,但它可能已经8年了......而且我正在挖掘自己的方式,我花了很多时间放牧数据库(DBA)而不是编码,但是补充一点,我喜欢编码。 。 我对这些片段中的每一个都有putbuttons以允许我在另一个项目中实现的各种funcationality,并且我试图通过一个新的接口来复制功能(也就是为什么我甚至在做这个atm)。 – shaddow

0

这意味着ARG_SECTION_NUMBER应该被声明为public static。更好的,如果它宣布为public static final