问题
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...
回答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
回答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);
回答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);
回答4:
Maybe this will help you:
listAdapter = new ArrayAdapter(MainActivity.this,
android.R.layout.simple_list_item_1,
android.R.id.text1,
Collections.singletonList(value));
来源:https://stackoverflow.com/questions/32546236/cannot-resolve-constructor-arrayadapter-when-using-listview-in-android-studio