使用Bundle将数组数据传递给listview上的另一个活动单击

时间:2016-06-04 06:53:02

标签: android

您好我一直在尝试将数据从一个Activity传递到另一个Activity但没有正面响应,因为我不知道如何通过捆绑包传递它在listview上单击的数据并将其提取到其他活动。

 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;
}

}

1 个答案:

答案 0 :(得分:0)

我认为应该是

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);

    }