使用 Bundle 将数组数据传递给 listview click 上的另一个活动
Posted
技术标签:
【中文标题】使用 Bundle 将数组数据传递给 listview click 上的另一个活动【英文标题】:Passing Array Data Using Bundle to another activity on listview click 【发布时间】:2016-10-04 06:49:41 【问题描述】:您好,我一直在尝试将数组数据从一个 Activity 传递到另一个没有正面响应,因为我不知道如何通过捆绑传递它已在列表视图上单击的数据并将其提取到另一个 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;
【问题讨论】:
有什么问题?您似乎正确地传递了数据。 “数据”是您创建的自定义类吗? 你有使用可序列化的数据类吗? 可序列化或可打包? Have a look on this example 【参考方案1】:我觉得应该是的
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);
【讨论】:
谢谢 那我将如何提取 anotheractivity.class 中的数据以上是关于使用 Bundle 将数组数据传递给 listview click 上的另一个活动的主要内容,如果未能解决你的问题,请参考以下文章