我有一个从导航栏运行的片段。这个ListView包含4个元素,我想做多个选择,重定向到另一个活动。 我是否应该为所有这些选择进行活动,使其成为if else if语句,或者是否有更简单的方法来做到这一点?新活动将包含有关每个类别的数据。 这是我的片段
public class Caffes extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
final String[] items=new String[]{"Urban Style","Unic concept","Small and cosy","Coffe to go"};
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public Caffes() {
// Required empty public constructor
}
// TODO: Rename and change types and number of parameters
public static Caffes newInstance(String param1, String param2) {
Caffes fragment = new Caffes();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fcaffes,container,false);
ListView list=(ListView)view.findViewById(R.id.listView1);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,items);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { //arg2-position,
if(arg2==1) {
Intent intent = new Intent(this, Liked.class);
intent.putExtra("position", arg2);
startActivity(intent);
}
Toast.makeText(getActivity().getApplicationContext(), "Yea!!! click called", Toast.LENGTH_SHORT).show();
}
});
return view;}
答案 0 :(得分:0)
我会创建一个使用Intent创建新活动的方法,并在intent中放置您想要添加的变量。然后我会在每个if中调用方法。
您可以使用切换功能,而不是使用if else!
希望有所帮助