2010-09-25 74 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