如何解决错误“无法解析方法getItemAtPosition(int)”。

问题描述 投票:-2回答:1

我的代码是当我在列表中选择一个项目时,项目名称应作为下一个屏幕的工具栏传递。

因此,当我通过调用函数putExtra()传递所选项时,我在getItemAtPosition(i)上遇到错误,就像“无法解析方法getItemAtPosition(int)”

这是我的代码......

包com.example.hp.shapes;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class secondActivity extends AppCompatActivity {

    Toolbar mtoolbar;
    ListView l;

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

        mtoolbar=(Toolbar) findViewById(R.id.toolbar2);

        Bundle bundle=getIntent().getExtras();
        if(bundle!=null)
        {
            mtoolbar.setTitle(bundle.getString("shapeName"));
            if(mtoolbar.getTitle().toString().equalsIgnoreCase("Square"))
            {
                l=(ListView) findViewById(R.id.listView2);
                ArrayAdapter<String> m1=new ArrayAdapter<String>(secondActivity.this,
                        android.R.layout.simple_list_item_1,
                        getResources().getStringArray(R.array.square));

               /* l.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)*/


 l.setOnItemClickListener(new AdapterView.OnItemClickListener() {
               @Override
               public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                   Intent intent1=new Intent(secondActivity.this,ThirdActivity.class);
                   intent1.putExtra("squareName", intent1.getItemAtPosition(i).toString());
                   startActivity(intent1);

               }
           });
            l.setAdapter(m1);


        }
            else if(mtoolbar.getTitle().toString().equalsIgnoreCase("rectangle"))
            {
                l=(ListView) findViewById(R.id.listView2);
                ArrayAdapter<String> m1=new ArrayAdapter<String>(secondActivity.this,
                        android.R.layout.simple_list_item_1,
                        getResources().getStringArray(R.array.rectangle));

                l.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        Intent i1=new Intent(secondActivity.this,ThirdActivity.class);
                        i1.putExtra("rectangleName",l.getItemAtPosition(i).toString());
                        startActivity(i1);
                    }
                });
                l.setAdapter(m1);

            }
            else if(mtoolbar.getTitle().toString().equalsIgnoreCase("parallelogram"))
            {
                l=(ListView) findViewById(R.id.listView2);
                ArrayAdapter<String> m1=new ArrayAdapter<String>(secondActivity.this,
                        android.R.layout.simple_list_item_1,
                        getResources().getStringArray(R.array.parallelogram));

                l.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        Intent i1=new Intent(secondActivity.this,ThirdActivity.class);
                        i1.putExtra("Name1",l.getItemAtPosition(i).toString());
                        startActivity(i1);
                    }
                });
                l.setAdapter(m1);

            }
            else if(mtoolbar.getTitle().toString().equalsIgnoreCase("hexagon"))
            {
                l=(ListView) findViewById(R.id.listView2);
                ArrayAdapter<String> m1=new ArrayAdapter<String>(secondActivity.this,
                        android.R.layout.simple_list_item_1,
                        getResources().getStringArray(R.array.hexagon));

                l.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        Intent i1=new Intent(secondActivity.this,ThirdActivity.class);
                        i1.putExtra("Name1",l.getItemAtPosition(i).toString());
                        startActivity(i1);
                    }
                });
                l.setAdapter(m1);

            }
            else if(mtoolbar.getTitle().toString().equalsIgnoreCase("octagon"))
            {
                l=(ListView) findViewById(R.id.listView2);
                ArrayAdapter<String> m1=new ArrayAdapter<String>(secondActivity.this,
                        android.R.layout.simple_list_item_1,
                        getResources().getStringArray(R.array.octagon));

                l.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        Intent i1=new Intent(secondActivity.this,ThirdActivity.class);
                        i1.putExtra("Name1",l.getItemAtPosition(i).toString());
                        startActivity(i1);
                    }
                });
                l.setAdapter(m1);

            }
            else if(mtoolbar.getTitle().toString().equalsIgnoreCase("pentagon"))
            {
                l=(ListView) findViewById(R.id.listView2);
                ArrayAdapter<String> m1=new ArrayAdapter<String>(secondActivity.this,
                        android.R.layout.simple_list_item_1,
                        getResources().getStringArray(R.array.pentagon));

                l.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        Intent i1=new Intent(secondActivity.this,ThirdActivity.class);
                        i1.putExtra("Name1",l.getItemAtPosition(i).toString());
                        startActivity(i1);
                    }
                });
                l.setAdapter(m1);

            }
            else if(mtoolbar.getTitle().toString().equalsIgnoreCase("cube"))
            {
                l=(ListView) findViewById(R.id.listView2);
                ArrayAdapter<String> m1=new ArrayAdapter<String>(secondActivity.this,
                        android.R.layout.simple_list_item_1,
                        getResources().getStringArray(R.array.cube));

                l.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        Intent i1=new Intent(secondActivity.this,ThirdActivity.class);
                        i1.putExtra("Name1",l.getItemAtPosition(i).toString());
                        startActivity(i1);
                    }
                });
                l.setAdapter(m1);

            }
        }
    }
}
android android-arrayadapter
1个回答
1
投票

getItemAtPosition()上没有Intent方法。 ListView上有一个。

相比:

intent1.putExtra("squareName", intent1.getItemAtPosition(i).toString());

有:

i1.putExtra("rectangleName",l.getItemAtPosition(i).toString());

在第一种情况下,你在getItemAtPosition()上调用intent1,这是一个Intent。这不会编译。

在第二种情况下,你在getItemAtPosition()上调用l,这是一个ListView。这将编译。

© www.soinside.com 2019 - 2024. All rights reserved.