0

Hello I have been trying to pass array Data from one Activity to another with no positive response because i don't know how to pass the data that it has been clicked on the listview through the bundle and extract it in the other Activity.

 public class Hospitals extends Fragment {
View hospitalZetu;
Context context;




public Hospitals() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view =  inflater.inflate(R.layout.fragment_hospitals, container, false);

    final ArrayList<Data> hospitalsDetails = new ArrayList<Data>();

    HospitalAdaptor adapter = new HospitalAdaptor(getContext(), hospitalsDetails);

    ListView listView;
    listView = (ListView) view.findViewById(R.id.edis_hospitals);


    hospitalsDetails.add(new Data(12.122,12.0,R.drawable.h1,"Muhimbili Hospital","12","23","12","90"));
    hospitalsDetails.add(new Data(12.122,12.0,R.drawable.h2,"Aghakan Hospital","10","43","76","90"));

    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Data data = hospitalsDetails.get(position);
            Intent intent = new Intent(getActivity(),Hopital.class);
            Bundle bundle = new Bundle();
            bundle.putString("Hname", String.valueOf(data));
            intent.putExtras(bundle);
            startActivity(intent);

        }
    });

    return view;
}

}

user3337660
  • 35
  • 1
  • 7

1 Answers1

0

I think it should be

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

        Data data = hospitalsDetails.get(position);
        Intent intent = new Intent(Hospital.this,"anotherActivity.class");
        intent.putExtra("Hname", String.valueOf(data));
        startActivity(intent);

    }
Rahul
  • 36
  • 5