[英]Android arrayadapter doesn't work well
我不知道我做錯了什么。 我是新手。 我需要創建一個與兩個 EditText(number) 和一個按鈕交互的自定義列表。 當用戶單擊按鈕時,它會生成一個從第一個數字到最后一個數字的列表,並檢查列表中單擊的數字是否為素數...但我在這部分出現錯誤: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);
所以我不明白問題是什么。 我也在另一個活動中使用了此代碼。 我還將發布整個活動:包 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();
}
}
}
});
}
});
}
感謝您的任何幫助
好的,我已經修改了該行(真是失態),但現在控制台向我顯示了其他錯誤,例如:
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)
和
Error:(75, 22) error: cannot find symbol method setAdapter(ArrayAdapter<Integer>)
還
Error:(77, 22) error: cannot find symbol method setOnItemClickListener(<anonymous OnItemClickListener>)
這里:
for (int i = first; i <= last; i++) {
listMod.add(i);
}
listMod.add(i);
行導致問題,因為listMod
是ListView
對象,而您正試圖在其上調用add
方法..
使用lista
這是ArrayList
用於添加的物品中ArrayList
其傳遞作為數據源適配器:
for (int i = first; i <= last; i++) {
lista.add(i);
}
如更新日志:
錯誤:(77, 22) 錯誤:找不到符號方法 setOnItemClickListener()
問題是由於將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.