[英]Android arrayadapter doesn't work well
I don't know what i'm doing wrong.我不知道我做错了什么。 I'm a newbie.我是新手。 I need to create a custom list that interact with two EditText(number) and a button.我需要创建一个与两个 EditText(number) 和一个按钮交互的自定义列表。 When the user click the button, it generates a list from the first number to the last number and check if the clicked number on list was prime or not... But i get an error in this part: ListView listMod;当用户单击按钮时,它会生成一个从第一个数字到最后一个数字的列表,并检查列表中单击的数字是否为素数...但我在这部分出现错误:ListView listMod;
listMod = (ListView) findViewById(R.id.listMod);
List<Integer> lista = new ArrayList<>();
for (int i = first; i <= last; i++) {
listMod.add(i);
}
ArrayAdapter<Integer> arrayAdapter = new ArrayAdapter<Integer>(
this,
android.R.layout.simple_list_item_2,
lista);
listMod.setAdapter(arrayAdapter);
so i don't see what the problem is.所以我不明白问题是什么。 I used this code also in another activity.我也在另一个活动中使用了此代码。 I'll post also the Whole activity: package com.example.andre.thelist;我还将发布整个活动:包 com.example.andre.thelist;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class TheList extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_the_list);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Maybe I can use this", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
Button btn = (Button)findViewById(R.id.gen);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText firstn = (EditText) findViewById(R.id.firstn);
EditText lastn = (EditText) findViewById(R.id.lastn);
String firstnString = firstn.getText().toString();
String lastnString = lastn.getText().toString();
if(firstnString.isEmpty() == true || lastnString.isEmpty() == true ) {
Integer first = 1;
Integer last = 100;
Toast.makeText(getApplicationContext(),"Verranno usate le cifre di esempio 1 e 100", Toast.LENGTH_SHORT).show();
}
final Integer first = Integer.parseInt(firstnString);
final Integer last = Integer.parseInt(lastnString);
ListView listMod;
listMod = (ListView) findViewById(R.id.listMod);
List<Integer> lista = new ArrayList<>();
for (int i = first; i <= last; i++) {
lista.add(i);
}
ArrayAdapter<Integer> arrayAdapter = new ArrayAdapter<Integer>(
this,
android.R.layout.simple_list_item_2,
lista);
listMod.setAdapter(arrayAdapter);
listMod.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adattatore, final View componente, int pos, long id) {
final Integer numero = (Integer) adattatore.getItemAtPosition(pos);
//creo il ciclo di controllo valori
if (numero > 0) {
boolean isPrime = true;
for (int i = 2; i <= numero / 2; i++) {
if (numero % i == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
AlertDialog alertDialog = new AlertDialog.Builder(TheList.this).create();
alertDialog.setTitle("Yeah numero primo");
alertDialog.setMessage("Il numero " + numero + " è un numero primo!");
alertDialog.show();
} else {
Toast.makeText(getApplicationContext(), "Il numero " + numero + " non è un numero primo", Toast.LENGTH_SHORT).show();
}
}
}
});
}
});
}
Thank you for any help感谢您的任何帮助
OK, I've modified the line (What a gaffe), but now the console show me other error like:好的,我已经修改了该行(真是失态),但现在控制台向我显示了其他错误,例如:
Error:(70, 54) error: no suitable constructor found for ArrayAdapter(<anonymous OnClickListener>,int,List<Integer>)
constructor ArrayAdapter.ArrayAdapter(Context,int,int,List<Integer>) is not applicable
(actual and formal argument lists differ in length)
constructor ArrayAdapter.ArrayAdapter(Context,int,List<Integer>) is not applicable
(actual argument <anonymous OnClickListener> cannot be converted to Context by method invocation conversion)
constructor ArrayAdapter.ArrayAdapter(Context,int,int,Integer[]) is not applicable
(actual and formal argument lists differ in length)
constructor ArrayAdapter.ArrayAdapter(Context,int,Integer[]) is not applicable
(actual argument <anonymous OnClickListener> cannot be converted to Context by method invocation conversion)
constructor ArrayAdapter.ArrayAdapter(Context,int,int) is not applicable
(actual argument <anonymous OnClickListener> cannot be converted to Context by method invocation conversion)
constructor ArrayAdapter.ArrayAdapter(Context,int) is not applicable
(actual and formal argument lists differ in length)
And和
Error:(75, 22) error: cannot find symbol method setAdapter(ArrayAdapter<Integer>)
Also还
Error:(77, 22) error: cannot find symbol method setOnItemClickListener(<anonymous OnItemClickListener>)
Here:这里:
for (int i = first; i <= last; i++) {
listMod.add(i);
}
listMod.add(i);
line causing issue because listMod
is object of ListView
and you are trying to call add
method on it..行导致问题,因为listMod
是ListView
对象,而您正试图在其上调用add
方法..
Use lista
which is ArrayList
for adding items in ArrayList
which passing as data-source to Adapter:使用lista
这是ArrayList
用于添加的物品中ArrayList
其传递作为数据源适配器:
for (int i = first; i <= last; i++) {
lista.add(i);
}
As in update log:如更新日志:
Error:(77, 22) error: cannot find symbol method setOnItemClickListener()错误:(77, 22) 错误:找不到符号方法 setOnItemClickListener()
problem is due to passing this
as first parameter in ArrayAdapter.create adapter object as:问题是由于将this
作为 ArrayAdapter.create 适配器对象中的第一个参数传递为:
ArrayAdapter<Integer> arrayAdapter = new ArrayAdapter<Integer>(
TheList.this,
android.R.layout.simple_list_item_2,
lista);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.