0

我有两个片段,一个带有对象列表视图,另一个带有对象详细信息。Listview 由 BaseAdapter 填充,我试图从 listview onclick 切换到其他片段(对象详细信息)。我之前在活动中工作得很好的代码:

public class MyAdapter extends BaseAdapter  {

    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 
    ArrayList<HashMap<String, String>> mDataShown;
    ArrayList<HashMap<String, String>> mAllData;

    public MyAdapter(Activity actt, ArrayList<HashMap<String, String>> d) {
        activity = actt;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
        mDataShown= (ArrayList<HashMap<String, String>>) d;
        mAllData = (ArrayList<HashMap<String, String>>) mDataShown.clone();
    }

    public int getCount() {
        return mDataShown.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.songs_row, null);

        TextView tvsong = (TextView)vi.findViewById(R.id.tvSong); 
        TextView tvsinger = (TextView)vi.findViewById(R.id.tvSinger); 

        HashMap<String, String> song = new HashMap<String, String>();
        song = data.get(position);

        final Context ct;
        ct = vi.getContext();

        final String stsong = song.get(Songs.KEY_NAME);
        final String stsinger = song.get(Songs.KEY_SINGER);

        tvsong.setText(stsong);
        tvsinger.setText(stsinger);

        vi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent i = new Intent(ct, SongDetails.class);               
                Bundle bandl = new Bundle();
                bandl.putString("song", stsong);
                bandl.putString("singer", stsinger);
                i.putExtras(bandl);
                ct.startActivity(i);
            }
        });

        return vi;
    }

但是现在我用片段更改了活动,我需要替换片段而不是开始活动。尝试使用此代码

    vi.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Fragment SongDetailsFragment = SongDetails.newInstance();
            FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
            transaction.addToBackStack(null);
            transaction.replace(R.id.SongContainer, SongDetailsFragment);
            transaction.commit();
        }
    });

但它不会工作。有谁知道如何使用 BaseAdapter 中的 FragmentTransaction?

4

2 回答 2

2

我通过在 Fragment 中实现 setOnItemClickListener 解决了这个问题:

songList = new ArrayList<HashMap<String, String>>();
.
.
.
adapter = new MyAdapter(getActivity(), songList);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {


public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {

                    HashMap<String, String> o = (HashMap<String, String>) list.getItemAtPosition(position);

                    Bundle bundle = new Bundle();
                    bundle.putString("song", o.get("song"));
                    bundle.putString("singer", o.get("singer"));

                    Fragment SongDetailsFragment = SongDetails.newInstance();
                    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
                    SongDetailsFragment .setArguments(bundle);
                    transaction.addToBackStack(null);
                    transaction.replace(R.id.SongContainer, SongDetailsFragment );
                    transaction.commit();
                }
              });  

此外,BaseAdapter 代码的小改动:

代替:

public Object getItem(int position) {
    return (position);

和:

public Object getItem(int position) {
    return data.get(position);

就是这样!

于 2013-08-14T20:32:31.513 回答
0

您已经通过适配器的构造函数中的 getActivity 传输 FragmentActivity 实例。

您可以使用它来获取片段管理器。经过

片段活动 mActivity;

公共 xyz(FragmentActivity a, args)

{

  this.mActivity = a;

}

和 onClickListener

mActivity.getFragmentManager().beginTransaction();

你可以做你想做的事。

于 2014-07-22T14:44:32.887 回答