【发布时间】:2014-08-02 05:02:51
【问题描述】:
我从androidhive.info 为我的应用程序创建了一个导航抽屉。在那个导航抽屉的一个片段中,我创建了一个自定义列表视图,它显示带有名称和图像的元素。现在单击自定义列表视图的项目后,我需要显示单击的列表元素的详细信息。为此,我创建了另一个片段。现在单击 listview 元素后,我应该转到所选元素的详细信息。我用来从一个片段移动到另一个片段的代码是这样的:
Fragment fragment=new ImportantNumbersFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
但getSupportFragmentManager() 在 Eclipse 中显示如下错误:The method getSupportFragmentManager() is undefined for the type PagesFragment。这是我的自定义列表视图片段的代码。
PagesFragment.class
public class PagesFragment extends ListFragment implements OnItemClickListener{
Typeface tf;
ListView lv;
List<SimpleRow>rowItems;
public PagesFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_pages, container, false);
Helper help=new Helper(getActivity().getApplicationContext());
String choice1="സര്ക്കാര് സ്ഥാപനങ്ങള്";
String choice1new=help.changemalayalam(new Data(choice1));
String choice2="പത്രം";
String choice2new=help.changemalayalam(new Data(choice2));
String choice3="ന്യൂസ് ചാനല്";
String choice3new=help.changemalayalam(new Data(choice3));
String choice4="യൂണിവേഴ്സിറ്റികള്";
String choice4new=help.changemalayalam(new Data(choice4));
String choice5="പഞ്ചായത്ത് ഓഫീസ്";
String choice5new=help.changemalayalam(new Data(choice5));
String choice6="ഇന്ഷുറന്സ്";
String choice6new=help.changemalayalam(new Data(choice6));
String choice7="പോലീസ് സ്റ്റേഷന്";
String choice7new=help.changemalayalam(new Data(choice7));
String choice8="ഫയര് സ്റ്റേഷന്";
String choice8new=help.changemalayalam(new Data(choice8));
ArrayList<String>choice=new ArrayList<String>();
choice.add(choice1new);
choice.add(choice2new);
choice.add(choice3new);
choice.add(choice4new);
choice.add(choice5new);
choice.add(choice6new);
choice.add(choice7new);
choice.add(choice8new);
lv=(ListView)rootView.findViewById(R.id.list1);
rowItems=new ArrayList<SimpleRow>();
for (int i = 0; i < choice.size(); i++) {
SimpleRow item = new SimpleRow(choice.get(i));
rowItems.add(item);
CustomSimpleListAdapter adapter = new CustomSimpleListAdapter(getActivity().getApplicationContext(),R.layout.list_single, rowItems);
lv.setAdapter(adapter);
lv.setOnItemClickListener(this);
}
return rootView;
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Toast.makeText(getActivity().getApplicationContext(), "You clicked on position "+position,Toast.LENGTH_LONG).show();
//Here upon clicking the list element, I need to go to ImportantNubersFragment
Fragment fragment=new ImportantNumbersFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Toast.makeText(getActivity().getApplicationContext(), "You clicked on position "+arg2,Toast.LENGTH_LONG).show();
}
}
ImportantNumbersFragment.class
public class ImportantNumbersFragment extends Fragment {
public ImportantNumbersFragment() {}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_container, container, false);
return rootView;
}
}
fragment_important_numbers.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout android:name="fragments.YourInitialFragment"
android:id="@+id/fragment_container"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dip" />
</LinearLayout>
单击自定义列表视图中的列表元素后,我想从 PagesFragment 移动到 ImportantNumbersFragment。所以在 OnListItemClick 中,我给出了从片段移动到另一个片段的代码。我必须以与使用以下意图从一个活动移动到另一个活动相同的方式执行此操作:
Intent i=new Intent(First.this,Second.class);
startActivity(i);.
由于我是 Fragment 的新手,我不知道如何在 Fragment 中实现这一点。这就是我在这里问的原因。有人可以指出此代码中的错误。提前谢谢..
【问题讨论】:
标签: android android-intent android-fragments android-listview