我创建了四个带有listview的标签。我一直在尝试使列表视图可单击,我使用了Here的listview教程,使用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
同样的问题在这里我尝试了不同的组合,它通过这种设置不断给我错误我有最少的错误
任何帮助非常感谢
答案 0 :(得分: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
onItemClick
方法定义,因此请添加大括号}
。;
结尾,而应以开放式大括号{
int
(position
)与String
new OnItemClickListener()
和setOnItemClickListener
来电,方法是添加:});
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);
}
}