我有两个片段,一个带有对象列表视图,另一个带有对象详细信息。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?