开发者

Errors in Layout Cant figure it out

开发者 https://www.devze.com 2023-01-17 08:25 出处:网络
I have created four tabs with listview in each.I have been attempting to make the list views clickable, I have used the listview tutorial from Here to create the list view using string.xml and R.array

I have created four tabs with listview in each. I have been attempting to make the list views clickable, I have used the listview tutorial from Here to create the list view using string.xml and R.array:

The problem is when I use my intent and onItemClickListener I get multiple marker errors, if I play around with the commas brackets and class body markers the errors move around, so is it the syntax that's the problem or is it the lay out or postion of the code;

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);
            }

}

I get these errors here:

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

Syntax error, insert "}" to complete ClassBody

If I add to complete class body I get more erros here开发者_如何学编程 and in other places.

I get these errors here:

  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

Same problem here I have tried different combinations and it gives me errors constantly with this setup I have the least amount of errors

Any help greatly appreciated


Everything is fine until setOnItemClickListener, where it becomes a mess.

 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
  • Line 2: You don't close the onItemClick method definition, so add a brace }.
  • Line 4: Should not end with a semicolon ; but a open brace {
  • Line 7: You cannot compare an int (position) with a String
  • Lines 7-10: These must be inside a method definition, e.g. move them to before line 5
  • Line 11: You need to close the new OnItemClickListener() you opened on line 1 and the setOnItemClickListener call, by adding: });
  • Line 12: You need to close the class ll2 with a brace }.

Furthermore, ListActivity already comes with an onListItemClick method, so you don't need the above code in onCreate — no need to define your own listener.

Just add a new method in your class, after 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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号