我是新来的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发生...
有东西我不是很到这里,一旦我通过这个障碍,我会有这个由球,并将能够使它做我想做的事情。
好吧,我开始得到什么的布莱恩,请看到我的编辑和增加进一步澄清什么,我想要做一个轻微的手柄。我觉得我很接近。 – shaddow
对于关于'OnClickListener'的其他问题,在这种情况下,我建议让你的Fragment实现'OnClickListener'而不是尝试使用匿名内部类。这样你就不必担心你的变量是静态的还是最终的,这样它们就可以被内部类使用。 –
的例子,会有帮助吗?我以前用Java编写过,但它可能已经8年了......而且我正在挖掘自己的方式,我花了很多时间放牧数据库(DBA)而不是编码,但是补充一点,我喜欢编码。 。 我对这些片段中的每一个都有putbuttons以允许我在另一个项目中实现的各种funcationality,并且我试图通过一个新的接口来复制功能(也就是为什么我甚至在做这个atm)。 – shaddow