[英]Adapter & ListView
I want to get list. 我想得到清单。 Each line - mas[i]. 每行-mas [i]。 Numb - it's int. 麻木-它是int。 I got an eclipse error: 我有一个月食错误:
The constructor ArrayAdapter(new View.OnClickListener(){}, int, String[]) is undefined 构造函数ArrayAdapter(new View.OnClickListener(){},int,String [])未定义
public class FirstActivity extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
TextView NumbZK = (TextView)findViewById(R.id.editText2);
final int Numb = Integer.parseInt(NumbZK.getText().toString());
TextView modN = (TextView)findViewById(R.id.editText1);
final int N = Integer.parseInt(modN.getText().toString());
String[] mas = new String[N];
for (int i=0; i<N;i++){
mas[i]=Integer.toString(Numb%(i+1));
}
ListView lView = (ListView)findViewById(R.id.list);
lView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, mas));
}
});
}
}
XML: XML:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false"
/>
help 救命
If your Activity
is extending from ListActivity
you need to declare a ListView
with android:id="@android:id/list"
in your layout. 如果您的Activity
是从ListActivity
扩展的, ListActivity
需要在布局中声明一个android:id="@android:id/list"
的ListView
。 Then you can get the ListView
by calling getListView()
in your Activity
然后,您可以通过在Activity
调用getListView()
来获取ListView
Reference: http://developer.android.com/reference/android/app/ListActivity.html 参考: http : //developer.android.com/reference/android/app/ListActivity.html
android:id="@+id/lView"
因为这就是您在活动中寻找的东西。
mas[i]=""+(Numb%(i+1));
consider replacing the above line with 考虑将上述行替换为
mas[i] = "" + Integer.toString(Numb%(i+1));
after looking at your xml, 查看您的xml之后,
ListView lView = (ListView)findViewById(R.id.lView);
replace this with 替换为
ListView lView = (ListView)findViewById(R.id.list);
and also 并且
lView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, mas));
with 与
lView.setAdapter(new ArrayAdapter<String>(youractivity.this,android.R.layout.simple_list_item_1, mas));
this should be replaced by context , this method will not work for onclicklistener. 应该将其替换为context,此方法不适用于onclicklistener。
HTH. HTH。
Try adding: 尝试添加:
<ListView android:id="@android:id/lView/>
In the layout file 在布局文件中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.