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);
}
}
});
创建ListView并为其设置适配器后,我在ListAdapter中收到错误。如何修复它或者它是什么? 我希望我用我编写的代码清楚我的moto。
所有建议都深受赞赏。
答案 0 :(得分:2)
您需要在匿名类中使用MainActivity.this
而不是this
来将此实际的Activity上下文称为
ListAdapter myAdapter = new ArrayAdapter<Double>(MainActivity.this, android.R.layout.simple_list_item_1, myyList);
// ^^^^^^^^^^^^^^^^^^
并提及类型并使用ArrayAdapter
而非ListAdapter
ArrayAdapter<Double> myAdapter = new ArrayAdapter<>(MainActivity.this...);
// ^^^^^^^
答案 1 :(得分:0)
你应该使用这样的东西
ArrayAdapter<Double> myAdapter = new ArrayAdapter<Double>(this, android.R.layout.simple_list_item_1, myyList);
答案 2 :(得分:0)
您可以使用
ListAdapter myAdapter = new ArrayAdapter<>(MainActivity.this,
android.R.layout.simple_list_item_1, myyList);
mListView.setAdapter(myAdapter);
或
ListAdapter myAdapter = new ArrayAdapter<Double>(MainActivity.this,
android.R.layout.simple_list_item_1, myyList);
mListView.setAdapter(myAdapter);
但请确保使用MainActivity.this
而不是这个