0
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.

3
  • Not a mind reader - what error? Commented Dec 13, 2017 at 5:57
  • Can you give a logcat of the error? Commented Dec 13, 2017 at 5:57
  • Getting red line in android studio in line- ListAdapter myAdapter = new ArrayAdapter<Double>(this, android.R.layout.simple_list_item_1, myyList); mListView.setAdapter(myAdapter); Commented Dec 13, 2017 at 5:58

3 Answers 3

2

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...);
 //         ^^^^^^^ 
Sign up to request clarification or add additional context in comments.

2 Comments

Yes I did.. I guess I need to post a screenshot of the error.. I'm attaching an screenshot too..
@user8747695 check the update , ListAdapter is not generic
0

you should use something like this

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

1 Comment

You are correct. An ArrayAdapter cannot be cast to a ListAdapter, but as ListView adapter only requires a BaseAdapter then you can pass in an ArrayAdapter. [developer.android.com/guide/topics/ui/… [developer.android.com/reference/android/widget/ListView.html]
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.