2016-04-10 270 views
0

我有一個從導航欄運行的片段。這個ListView包含4個元素,我想做出多重選擇,並重定向到另一個活動。 我是否應該爲所有這些選擇制定活動,讓它像是其他語句一樣,還是有更簡單的方法來實現?新活動將包含有關每個分類的數據。 這裏是我的片段listView選擇多個項目

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

我想創建創建意圖的新活動的方法,並在意向將使您要添加的變量。然後我會在每個方面都調用這個方法。

而不是使用if else,你可以使用開關功能!

希望它有幫助