2010-09-25 97 views
0

我已經創建了四個帶有listview的標籤。我一直在嘗試使列表視圖可點擊,我用Here列表視圖教程創建列表視圖使用string.xml和R.array:佈局錯誤無法解決

問題是當我使用我的意圖和onItemClickListener我得到多個標記錯誤,如果我玩弄逗號括號和類身標記錯誤四處移動,那麼它的語法是問題,還是代碼的佈局或位置;

public class ll2 extends ListActivity { 

    static final String[] teams = new String[] {"Accrington Stanley", "Aldershot", "Barnet", "Bradford City", "Burton Albion", "Bury", "Cheltenham Town", "Chesterfield", "Crewe Alexandra"}; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     final String[] TEAMS = getResources().getStringArray(R.array.twoteams_array); 
     setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, TEAMS)); 

     ListView lv = getListView(); 
     lv.setTextFilterEnabled(true); 

     lv.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 

      public void onListItemClick(ListView, parent, View v, int position, long id); 
      } 

      if (position == "Braford City") { 
       Intent intent = new Intent(this, Bradford.class); 
       startActivity(intent); 
      } 

} 

我這裏得到這些錯誤:

static final String[] teams = new String[] {"Accrington Stanley", "Aldershot", "Barnet", "Bradford City", "Burton Albion", "Bury", "Cheltenham Town", "Chesterfield", "Crewe Alexandra"}; 

語法錯誤,插入 「}」 來完成 ClassBody

如果我完成添加類的身體我得到更多的誤差修改這裏和其他地方。

我這裏得到這些錯誤:

public void onListItemClick(ListView, parent, View v, int position, long id); } 
 
Multiple markers at this line 
    - Syntax error on token(s), misplaced construct(s) 
    - Syntax error, insert ";" to complete LocalVariableDeclarationStatement 
    - Syntax error on token ",", ; expected 
    - Syntax error on token "(", = expected 
    - Syntax error on token ",", ; expected 
    - Syntax error, insert "}" to complete MethodBody 
    - Syntax error, insert "}" to complete ClassBody 
    - Syntax error on token "}", delete this token 
這裏

同樣的問題我已經嘗試了不同的組合,它不斷給我的錯誤與此設置我有錯誤

任何幫助最少的非常感謝

回答

2

一切都很好,直到setOnItemClickListener,它變得一團糟。

1 lv.setOnItemClickListener(new OnItemClickListener() { 
2  public void onItemClick(AdapterView<?> parent, View view, 
           int position, long id) { 
3 
4  public void onListItemClick(ListView, parent, View v, 
            int position, long id); 
5  } 
6 
7  if (position == "Braford City") { 
8   Intent intent = new Intent(this, Bradford.class); 
9   startActivity(intent); 
10  } 
11 
12 
  • 2號線:你不關閉onItemClick方法定義,所以加一個括號}
  • 4行:不應該用分號;而是一個開放的括號結束{
  • 7號線:你不能比較的intposition)與String
  • 7-10行:這些措施必須一方法定義,例如5號線
  • 線11之前將其移動到:您需要關閉new OnItemClickListener()你在1號線和setOnItemClickListener電話開通,加入:});
  • 第12行:您需要關閉類ll2用括號}

此外,ListActivity已經自帶了一個onListItemClick方法,所以你不需要在onCreate —上面的代碼無需定義自己的聽衆。

就在你的類添加一個新方法,onCreate後:

public void onListItemClick(ListView l, View v, int position, long id) { 
    if (position == 3) { 
     Intent intent = new Intent(this, Bradford.class); 
     startActivity(intent); 
    } 
} 
+0

感謝ü這麼多,我想b是出第一個承認嵌入struggledwith這一點,但在這個問題上綁架些小與O問題後代碼你給我終於設法得到它的工作天才克里斯....天才 – JonniBravo 2010-09-25 16:11:20

+0

Ps拼寫是我的手機預測文字9個問題我的意思 – JonniBravo 2010-09-25 16:12:48