我的代码是当我在列表中选择一个项目时,项目名称应作为下一个屏幕的工具栏传递。
所以当我通过调用函数putExtra()传递所选项时,我在getItemAtPosition(i)上得到错误就像"无法解析方法getItemAtPosition(int)
这是我的代码......
package 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);
}
}
}
}
答案 0 :(得分:1)
getItemAtPosition()
上没有Intent
方法。 是 ListView
上的一个。
比较
intent1.putExtra("squareName", intent1.getItemAtPosition(i).toString());
使用:
i1.putExtra("rectangleName",l.getItemAtPosition(i).toString());
在第一种情况下,您在getItemAtPosition()
上呼叫intent1
,这是Intent
。这不会编译。
在第二种情况下,您在getItemAtPosition()
上呼叫l
,这是ListView
。这将编译。