我已經嘗試了很多的解決方案,我在網絡上找到的,但它們中的任何一個作品,我有這樣的代碼:我有一個ListFragment一些選項,但項目單擊不起作用
public class Cambiar_ciudad extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.lay_cambiar_ciudad, container,
false);
String[] values = new String[] {"Barcelona", "Madrid", "Sevilla", "Valencia" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
return rootView;
}
}
這是代碼向我展示List
與("Barcelona", "Madrid", "Sevilla", "Valencia")
我希望當我推動其中一個時,他們會給我一個新的佈局。
我做到了在不片段和它的工作原理與此代碼:
Ciudades = new String[]{"Badajoz","Barcelona", "Madrid", "Sevilla", "Valencia"};
ArrayAdapter<String> adapter;
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, Ciudades);
ListView lv = (ListView) findViewById(R.id.listView);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
if (position == 0) {
Intent myIntent = new Intent(view.getContext(), Menu_Principal.class);
startActivityForResult(myIntent, 0);
}
if (position == 1) {
Intent myIntent = new Intent(view.getContext(), Menu_Principal.class);
startActivityForResult(myIntent, 0);
}
if (position == 2) {
Intent myIntent = new Intent(view.getContext(), Menu_Principal.class);
startActivityForResult(myIntent, 0);
}
if (position == 3) {
Intent myIntent = new Intent(view.getContext(), Menu_Principal.class);
startActivityForResult(myIntent, 0);
}
if (position == 4) {
Intent myIntent = new Intent(view.getContext(), Menu_Principal.class);
startActivityForResult(myIntent, 0);
}
}
});
但在ListFragment我不知道如何選擇與Menu_Principal.class
鏈接我希望你們中的任何可以解決它,非常感謝!
在ListFragment'onListItemClick'中有一個方法可以覆蓋它,並且在實現中您可以啓動Menu_Principal活動 –
@RajeshBatth,但是我如何將它添加到我的代碼中以獲取幾個按鈕?我不知道如何在listFragment中使用'onListItemClick',我試過的時候我遇到了問題 – balex