0

现在我正在尝试创建一个 android onListItemClick 方法。但是每次我这样做时都会出现大量语法错误,说预期的分号。通常,当我遇到这样的语法错误时,我知道只需添加一个分号,但在这种情况下,我不知道应该如何将分号应用于这样的事情。首先,在这样的声明中是否应该有分号?这是代码

package net.androidbootcamp.coffeefinder;

import java.util.Calendar;

import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Menu Select Array
        String[] CoffeeShop = {"Creation","Starbucks","Caribou","Mo'Joe" };
        //Set list adapter for list
        setListAdapter(new ArrayAdapter<String>(this,R.layout.activity_main,R.id.Coffee,CoffeeShop));
        //Create actions of list

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

        } 
4

2 回答 2

2

它应该在您的 onCreate 之外:

public class MainActivity extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Menu Select Array
        String[] CoffeeShop = {"Creation","Starbucks","Caribou","Mo'Joe" };
        //Set list adapter for list
        setListAdapter(new ArrayAdapter<String>(this,R.layout.activity_main,R.id.Coffee,CoffeeShop));
        //Create actions of list
    }

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

    } 

}
于 2012-12-06T02:54:11.770 回答
0

该方法是这样的,应该在 onCreate() 中:

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

              // selected item will be in position
              //do what ever you want to do.

          }
        });
于 2012-12-06T02:55:00.430 回答