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.l
Solution 1:
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
Solution 2:
I think it's happening because your class implements onclicklistener. So can you try cast this
to Activity
? Like the code below:
listAdapter = new ArrayAdapter((Activity)this, android.R.layout.simple_list_item_1, android.R.id.text1, value);
Solution 3:
ArrayAdapter needs Context
as the first argument. What you can do is to have a field reference to your Context
, like the followings.
- Added a field to your Activity,
private Context mContext;
- Inside the onCreate() of your Activity,
mContext = this;
- Use the mContext to construct ArrayAdapter,
listAdapter = new ArrayAdapter(mContext, android.R.layout.simple_list_item_1, android.R.id.text1, value);
Solution 4:
Maybe this will help you:
listAdapter = newArrayAdapter(MainActivity.this,
android.R.layout.simple_list_item_1,
android.R.id.text1,
Collections.singletonList(value));
Post a Comment for "Cannot Resolve Constructor Arrayadapter When Using Listview In Android Studio"