我想问一下如何从Listview BaseAdapter导航到Fragment?我单击任何项目并从中获取位置和信息并发送到 Fragment。我该怎么做?
我的基本适配器:
public class ListViewAdapter extends BaseAdapter {
// Declare Variables
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
ImageLoader imageLoader;
HashMap<String, String> resultp = new HashMap<String, String>();
public ListViewAdapter(Context context,
ArrayList<HashMap<String, String>> arraylist) {
this.context = context;
data = arraylist;
imageLoader = new ImageLoader(context);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// Declare Variables
TextView country, rank;
// TextView population;
ImageView flag;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.listview_item, parent, false);
// Get the position
resultp = data.get(position);
// Locate the TextViews in listview_item.xml
rank = (TextView) itemView.findViewById(R.id.rank);
country = (TextView) itemView.findViewById(R.id.country);
// Locate the ImageView in listview_item.xml
flag = (ImageView) itemView.findViewById(R.id.flag);
// Capture position and set results to the TextViews
distance.setText(resultp.get(FragmentPlaces.RANK));
bar_name.setText(resultp.get(FragmentPlaces.COUNTRY));
// population.setText(resultp.get(MainActivity.POPULATION));
// Capture position and set results to the ImageView
// Passes flag images URL into ImageLoader.class
imageLoader.DisplayImage(resultp.get(FragmentPlaces.FLAG), logo_image);
// Capture ListView item click
itemView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// // Get the position
resultp = data.get(position);
Bundle bundle = new Bundle();
bundle.putString("rank", resultp.get(FragmentPlaces.RANK));
bundle.putString("country",
resultp.get(FragmentPlaces.COUNTRY));
bundle.putString("flag", resultp.get(FragmentPlaces.FLAG));
// set Fragmentclass Arguments
FragmentSubVenues fragSubVenues = new FragmentSubVenues();
fragSubVenues.setArguments(bundle);
// FragmentTransaction ft =
// getFragmentManager().beginTransaction();
// ft.replace(R.id.activity_main_content_fragment,
// fragSubVenues);
// ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
// ft.addToBackStack(null);
// ft.commit();
// FragmentManager fragmentManager =
// activity.getFragmentManager();
}
});
return itemView;
}
}
我想在 Fragment 下方导航:
public FragmentSubVenues() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_subview, null);
Bundle bundle = this.getArguments();
if(bundle != null){
bar_name = bundle.getString("country");
bar_name = bundle.getString("rank");
bar_name = bundle.getString("flag");
}
TextView txtrank = (TextView) view.findViewById(R.id.rank);
TextView txtcountry = (TextView) view.findViewById(R.id.country);
ImageView imgflag = (ImageView) view.findViewById(R.id.flag);
txtrank.setText(country);
txtcountry.setText(rank);
imageLoader.DisplayImage(flag, imgflag);
return view;
}
}