0

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

3
  • You need to pass array or arraylist. Post your log. Commented Sep 13, 2015 at 4:26
  • i need to pass array, question updated. i am getting compile time errors Commented Sep 13, 2015 at 4:35
  • copy the full sentence of the error and paste here Commented Sep 13, 2015 at 6:05

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

8 Comments

public class MainActivity extends ActionBarActivity implements OnItemSelectedListener { Button add; Button ok; Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); add = (Button) findViewById(R.id.btn_new); radioGroup = (RadioGroup) findViewById(R.id.radioGroup); spinner = (Spinner) findViewById(R.id.spinner); spinner.setOnItemSelectedListener(this); ok.setOnClickListener(new View.OnClickListener() { Override public void onClick(View v) {
//above comment continues ArrayAdapter<String> listAdapter = new ArrayAdapter<String>((Activity)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) {
then obviously you should use new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, android.R.id.text1, value);
I tried: ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, android.R.id.text1, value); but getting "cannot resolve constructor 'ArrayAdapter(app.mysqlapp.MainActivity,int,int,java.lang.String)'" error
your line String[] value = new String{"hello","world"}; is looking strange, are you sure you post it right? it should not compile
|
0

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);

1 Comment

i tried this. still getting error as "inConvertible Types, cannot cast Anonymos android.view.View.onClickListner to Android.app.Activity"..
0

ArrayAdapter needs Context as the first argument. What you can do is to have a field reference to your Context, like the followings.

  1. Added a field to your Activity, private Context mContext;
  2. Inside the onCreate() of your Activity, mContext = this;
  3. Use the mContext to construct ArrayAdapter, listAdapter = new ArrayAdapter(mContext, android.R.layout.simple_list_item_1, android.R.id.text1, value);

Comments

0

Maybe this will help you:

listAdapter = new ArrayAdapter(MainActivity.this,
                               android.R.layout.simple_list_item_1,
                               android.R.id.text1,
                               Collections.singletonList(value));

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.