Android arrayadapter运行良好

时间:2015-11-06 19:33:08

标签: java android android-listview android-arrayadapter

我不知道自己做错了什么。我是新手。 我需要创建一个与两个EditText(数字)和一个按钮交互的自定义​​列表。当用户单击该按钮时,它会生成从第一个数字到最后一个数字的列表,并检查列表中单击的数字是否为素数...但是我在这部分中收到错误:     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);

所以我不知道问题是什么。我在另一个活动中也使用了此代码。 我还会发布整个活动: package 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>)

1 个答案:

答案 0 :(得分:1)

下面:

for (int i = first; i <= last; i++) { 
   listMod.add(i);
}

listMod.add(i);行导致问题,因为listModListView的对象,而您正试图在其上调用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);