Search code examples
javaandroidandroid-listview

Cannot resolve constructor ArrayAdapter when using ListView in Android Studio


I have an activity Mainactivity, in this when a button is pressed then it will show a listview. But in

listAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1, value);

I am getting "Cannot resolve constructor ArrayAdapter (anonymous android view.View.OnClickListener, int, int, java.lang.String)"

My outer class is "Mainactivity" I tried "Mainactivity.this" instead of "this". But It is showing "cannot resolve constructor" error.

MainActivity class extends Actionbaractivity implements onItemelectedListner

My code is:

ok.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if(cnt.getText().toString().length() > 0 &&
                    number.getText().toString().length() > 0 &&
                    Integer.parseInt(number.getText().toString()) > 0 &&
                    Integer.parseInt(cnt.getText().toString()) > 0) {
                    number.requestFocus();
                    String[] value = new String{"hello","world"};

                    try {
                        temp_count = temp_count + Integer.parseInt(cnt.getText().toString());
                        count.setText(String.valueOf(temp_count));
                        temp_amt = temp_amt + (Integer.parseInt(cnt.getText().toString()) * tkt_rate);
                        amount.setText(String.valueOf(temp_amt));

                        ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this,
                         android.R.layout.simple_list_item_1, android.R.id.text1, value);

                        lstView.setAdapter(listAdapter);
                        lstView.setOnItemClickListener(new OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view,
                                                    int position, long id) {

                                SparseBooleanArray checked = lstView.getCheckedItemPositions();
                                int size = checked.size(); // number of name-value pairs in the array
                                for (int i = 0; i < size; i++) {
                                    int key = checked.keyAt(i);
                                    boolean value = checked.get(key);
                                    if (value) {
                                        row = lstView.getChildAt(i);
                                        row.setBackgroundColor(Color.parseColor("#33B5E5"));
                                    } else {
                                        row = lstView.getChildAt(i);
                                        row.setBackgroundColor(Color.parseColor("#F0F0F0"));
                                    }
                                }
                            }
                        });

Please help...


Solution

  • The error shows that you are putting android view.View.OnClickListener as the first argument.

    I know you said you have tried, but you really need to use Mainactivity.this. If it is not working please post the code of the start of your java file.

    Also is your activity named as Mainactivity? Remember it is case sensitive, should it be MainActivity? If so, you have to use MainActivity.this