我是新來的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