user8747695
user8747695

Reputation: 11

Cannot resolve constructor in ArrayAdapter

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

EditText myeditText1;
EditText myeditText2;
Button myButton;

Double myfirstET1;
Double myfirstET2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    myeditText1 = (EditText) findViewById(R.id.editText1);
    myeditText2 = (EditText) findViewById(R.id.editText2);
    myButton = (Button) findViewById(R.id.buttonID);
    final TextView myTextView = (TextView) findViewById(R.id.textView2);


    myButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            myfirstET1 = Double.parseDouble(myeditText1.getText().toString());
            myfirstET2 = Double.parseDouble(myeditText2.getText().toString());
            final ListView mListView = (ListView) findViewById(R.id.mList);

            if ((myeditText1.getText().length() > 0) && (myeditText2.getText().length() > 0)) {
                double oper1 = Double.parseDouble(myeditText1.getText().toString());
                double oper2 = Double.parseDouble(myeditText2.getText().toString());
                double result = oper1 / oper2;
                myTextView.setText(Double.toString(result));


                Double myyList[] = {oper1, oper2 + 1, oper2 + 1};

                ListAdapter myAdapter = new ArrayAdapter<Double>(this, android.R.layout.simple_list_item_1, myyList);
                mListView.setAdapter(myAdapter);

            }

        }
    });

Upon creating the ListView and setting adapter to it, I'm getting error in ListAdapter.. How do I fix it OR whats the easy alternative for it? I hope my moto is clear with the code I've written.

All suggestions are heartly appreciated.

Upvotes: 0

Views: 167

Answers (3)

Nikhil Lotke
Nikhil Lotke

Reputation: 665

You can either use

ListAdapter myAdapter = new ArrayAdapter<>(MainActivity.this, 
android.R.layout.simple_list_item_1, myyList);
mListView.setAdapter(myAdapter);

or

ListAdapter myAdapter = new ArrayAdapter<Double>(MainActivity.this, 
android.R.layout.simple_list_item_1, myyList);
                mListView.setAdapter(myAdapter);

But make sure instead of this use MainActivity.this

Upvotes: 0

Pavneet_Singh
Pavneet_Singh

Reputation: 37404

You need to use MainActivity.this instead of this in anonymous class to refer to this actual Activity context as

 ListAdapter myAdapter = new ArrayAdapter<Double>(MainActivity.this, android.R.layout.simple_list_item_1, myyList);
 //                                             ^^^^^^^^^^^^^^^^^^

and also mention the type and use ArrayAdapter not ListAdapter

 ArrayAdapter<Double> myAdapter = new ArrayAdapter<>(MainActivity.this...);
 //         ^^^^^^^ 

Upvotes: 2

Tejas Pandya
Tejas Pandya

Reputation: 4087

you should use something like this

ArrayAdapter<Double> myAdapter = new ArrayAdapter<Double>(this, android.R.layout.simple_list_item_1, myyList);

Upvotes: 0

Related Questions