在android中的自定义列表视图中从一个片段移动到另一个片段

时间:2014-08-02 05:02:52

标签: android android-intent android-fragments android-listview

我已从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);

由于我是碎片的新手,我不知道如何在片段中实现这一点。这就是我问这里的原因。有人可以指出这段代码中的错误。提前谢谢..

1 个答案:

答案 0 :(得分:1)

  • 您是否在所有扩展片段中使用Support package作为fragment 上课,检查!
  • 我自己使用了来自AndroidHive的代码,它完美无缺
  • 它没有使用Support package扩展片段类
  • 您在使用支持导入的代码中,请确保您 保持进口的一致性
  • Programatically Speaking for ex :: 如果您使用支持包,请使用以下代码

import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;

  • 如果您不使用支持包使用代码导入 ::

import android.app.ActionBarDrawerToggle;
import android.app.Fragment;
import android.app.FragmentActivity;
import android.app.FragmentTransaction;
import android.widget.DrawerLayout;

{编辑 - 关于如何执行片段事务的方式的示例}

<强> MainActivity.java

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;

public class MainActivity extends FragmentActivity {

    Fragment fragment;
    String className;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d("MainActivity", "onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Store the name of the class
        className=MainActivity.class.getSimpleName();

        //First fragment should be mounted on oncreate of main activity
        if (savedInstanceState == null) {  
            /*fragment=FragmentOne.newInstance();
            getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).addToBackStack(className).commit();
            */

            Fragment newFragment = FragmentOne.newInstance();  
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();  
            ft.replace(R.id.container, newFragment).addToBackStack(null).commit();  
            Log.d("FRAGMENT-A", "fragment added to backstack");
        }

    }
}

<强> FragmentOne.java

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.Spinner;

import com.sample.screennavigation.UtilRangeSeekBar.OnRangeSeekBarChangeListener;

    public class FragmentOne extends Fragment{

        //Declare variables that hold the data for configuration change

        public static FragmentOne newInstance(){
            Log.d("FragmentOne", "newInstance");
            FragmentOne fragment = new FragmentOne();
            return  fragment;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            Log.d("FragmentOne", "onCreateView");
            View view=inflater.inflate(R.layout.fragment_one, container, false);

            return view;
        }

    }

{EDIT-2}


  • PagesFragment ... 正在某处使用支持包,请检查 它
  • 检查pagefragment
  • 的导入

But the getSupportFragmentManager() is showing an error in eclipse like this: The method getSupportFragmentManager() is undefined for the type PagesFragment. 
  • 代码中的错误明确表示您正在使用supportpackage导入 并且它与执行相冲突
  • 记住你不能混合进口

希望它有所帮助!,如果您需要任何其他信息,请与我们联系