2015-06-14 45 views
0

我已經嘗試了很多的解決方案,我在網絡上找到的,但它們中的任何一個作品,我有這樣的代碼:我有一個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

鏈接我希望你們中的任何可以解決它,非常感謝!

+0

在ListFragment'onListItemClick'中有一個方法可以覆蓋它,並且在實現中您可以啓動Menu_Principal活動 –

+0

@RajeshBatth,但是我如何將它添加到我的代碼中以獲取幾個按鈕?我不知道如何在listFragment中使用'onListItemClick',我試過的時候我遇到了問題 – balex

回答

0

把這個代碼在你

@Override 
public void onListItemClick(ListView l, View v, int position, long id){ 
    Context context = getActivity(); 
    // When clicked, show a toast with the TextView text 
    if (position == 0) { 
     Intent myIntent = new Intent(context, Menu_Principal.class); 
     context.startActivityForResult(myIntent, 0); 
    } 

    if (position == 1) { 
     Intent myIntent = new Intent(context, Menu_Principal.class); 
     context.startActivityForResult(myIntent, 0); 
    } 

    if (position == 2) { 
     Intent myIntent = new Intent(context, Menu_Principal.class); 
     context.startActivityForResult(myIntent, 0); 
    } 
    if (position == 3) { 
     Intent myIntent = new Intent(context, Menu_Principal.class); 
     context.startActivityForResult(myIntent, 0); 
    } 
    if (position == 4) { 
     Intent myIntent = new Intent(context, Menu_Principal.class); 
     context.startActivityForResult(myIntent, 0); 
    } 
} 
+0

現在我得到了這段代碼: – balex

0

現在我得到這個代碼:

@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[] { "Badajoz","Barcelona", "Madrid", "Sevilla", "Valencia" }; 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), 
      android.R.layout.simple_list_item_1, values); 
    setListAdapter(adapter); 

    return rootView; 

    @Override 
    public void onListItemClick(ListView l, View v, int position, long id){ 
     Context context = getActivity(); 
     // When clicked, show a toast with the TextView text 
     if (position == 0) { 
      Intent myIntent = new Intent(context, Menu_Principal.class); 
      startActivityForResult(myIntent, 0); 
     } 

     if (position == 1) { 
      Intent myIntent = new Intent(context, Menu_Principal.class); 
      startActivityForResult(myIntent, 0); 
     } 

     if (position == 2) { 
      Intent myIntent = new Intent(context, Menu_Principal.class); 
      startActivityForResult(myIntent, 0); 
     } 
     if (position == 3) { 
      Intent myIntent = new Intent(context, Menu_Principal.class); 
      startActivityForResult(myIntent, 0); 
     } 
     if (position == 4) { 
      Intent myIntent = new Intent(context, Menu_Principal.class); 
      startActivityForResult(myIntent, 0); 
     } 
    } 


} 
} 

但它給我一些錯誤的位置:

(ListView l, View v, int position, long id){

的ListView其上紅色

v其紅色

還有一些「;」預計

錯誤:(35,36)錯誤:';'預計

錯誤:(35,53)錯誤:';'預期

錯誤:(35,54)錯誤:不是一個語句

錯誤:(35,55)錯誤: ';'預期

錯誤:(35,70)錯誤:預期

錯誤:(35,76)錯誤:不是一個語句

錯誤:(35,78)錯誤: ';'預計

相關問題