listview所选项目不显示在对话框中

时间:2013-08-21 05:45:30

标签: android

   public class MainActivity extends Activity {

        private static final String[] lakes = { "Superior","Victoria","Huron","lakhani","sagar","surat","pune","gujarat",
        "baroda","jayesh","mahesh","suresh","chirag","laptop"};

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);


            ListView listView = (ListView)findViewById(R.id.listView);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, lakes);
            listView.setAdapter(adapter);

            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    AlertDialog.Builder abd = new AlertDialog.Builder(MainActivity.this);
                    abd.setTitle("selected item");
                    abd.setMessage("selected item is=" + listView.getItemAtPosition(View.SCROLLBAR_POSITION_DEFAULT));
                    abd.setPositiveButton("Ok", null);
                    abd.show();
                }
            });
        }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}




this error diaplay 
Gradle: local variable listView is accessed from within inner class; needs to be declared final

7 个答案:

答案 0 :(得分:0)

你有两个选择 - 在全球范围内制作列表视图 -

 ListView listView;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listView = (ListView)findViewById(R.id.listView);

或使其成为最终

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final ListView listView = (ListView)findViewById(R.id.listView);

答案 1 :(得分:0)

试试这段代码

final ListView listView = (ListView)findViewById(R.id.listView);

答案 2 :(得分:0)

您可以更改listview声明:

final ListView listView = (ListView)findViewById(R.id.listView);

或者在onCreate方法之外声明listview。

private ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = (ListView)findViewById(R.id.listView);

答案 3 :(得分:0)

试试这个..

private static final String[] lakes = { "Superior","Victoria","Huron","lakhani","sagar","surat","pune","gujarat",
    "baroda","jayesh","mahesh","suresh","chirag","laptop"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        final ListView listView = (ListView)findViewById(R.id.listView);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, lakes);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                AlertDialog.Builder abd = new AlertDialog.Builder(MainActivity.this);
                abd.setTitle("selected item");
                abd.setMessage("selected item is=" + listView.getItemAtPosition(i));
                abd.setPositiveButton("Ok", null);
                abd.show();
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

答案 4 :(得分:0)

您可以将listview声明为类成员,然后在itemclick

中声明
        String s = (String) adapterView.getItemAtPosition(i);

使用字符串在对话框中显示它。

    AlertDialog.Builder abd = new AlertDialog.Builder(MainActivity.this);
    abd.setTitle("selected item");
    abd.setMessage("selected item is=" + s);
    abd.setPositiveButton("Ok", null);
    abd.show();

如果您希望以吐司形式展示它。

 Toast.makeText(MainActivity.this,s,Toast.LENGTH_SHORT).show();

答案 5 :(得分:0)

  

ListView listView = (ListView)findViewById(R.id.listView);onCreate结尾的范围   功能

因此,每次调用onCreate(savedInstanceState)函数时,都会创建listView对象,并从函数(ListView)findViewById(R.id.listView)将值设置为它。

  

但是在功能的结尾 对象已解散,您将   在课堂上的任何其他地方都没有变量listView

当你在onCreate函数之外和类 MainActivity

中创建变量时,问题就解决了

请尝试阅读本文,以便更好地了解scope of a variable

答案 6 :(得分:0)

  

这应该有效

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    Object s=adapterView.getItemAtPosition(i);
                    final String selectedItem=s.toString();
                    AlertDialog.Builder abd = new AlertDialog.Builder(MainActivity.this);
                    abd.setTitle("selected item");
                    abd.setMessage("selected item is=" + selectedItem);
                    abd.setPositiveButton("Ok", null);
                    abd.show();
                }
            });
        }