2

I'm new to android and Java and I faced this problem and I search for answer but I don't find any thing.

I'm trying to get some data from database and put it in spinner but I faced this problem:

The constructor ArrayAdapter(AddNewUnit, int, String) is undefined

this is my code:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

public class AddNewUnit extends Activity implements OnItemSelectedListener {

EditText UnitName;
Spinner BookName;
DataBase dataBase;
String[][] Books;
String BookId;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_new_unit);
    UnitName = (EditText) findViewById(R.id.ANU_EtUnitName);
    BookName = (Spinner) findViewById(R.id.ANU_SBookName);
    dataBase.open();
    Books = dataBase.Get_Books();
    dataBase.close();
    ArrayAdapter<String> adapter =new ArrayAdapter<String>(AddNewUnit.this, android.R.layout.simple_spinner_item, Books[1][]);
    BookName.setAdapter(adapter);
    BookName.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    // TODO Auto-generated method stub
    BookId=Books[0][BookName.getSelectedItemPosition()];
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}


}

My problem is with this line

ArrayAdapter<String> adapter =new ArrayAdapter<String>(AddNewUnit.this, android.R.layout.simple_spinner_item, Books[1][]);

I searched and found that most mistakes is with context when they write this but I write AddNewUnit.this which is my activity.

what's the mistake I do it?


More details:

I'm sorry this is the first time to use java I used c++ before and in c++ I use Books[1][] to transform array from multidimensional to single-dimension it just get the row number 1 from the array.

This string it has two rows one for Book ID and one for Book name I want to pass just the Book name which is the row 1.

Is there any way to do that or I must use custom ArrayAdapter?

2
  • Will you please try with just passing String[]. I mean your problem can be at Books[1][]; Will you just create an array constant and pass and see it works or not Commented Jul 15, 2013 at 4:25
  • going good check your DB query .. getting data or not :) Commented Jul 15, 2013 at 4:50

4 Answers 4

1

The constructor used is undefined.

public ArrayAdapter (Context context, int resource, int textViewResourceId, T[] objects)

Added in API level 1

Constructor

Parameters

context The current context.

resource    The resource ID for a layout file containing a layout to use when instantiating views.

textViewResourceId  The id of the TextView within the layout resource to be populated

objects The objects to represent in the ListView.

http://developer.android.com/reference/android/widget/ArrayAdapter.html#ArrayAdapter(android.content.Context, int, T[])

You have this

 ArrayAdapter<String> adapter =new ArrayAdapter<String>(AddNewUnit.this, android.R.layout.simple_spinner_item, Books[1][]);  // params wrong

Use a Custom Listview with custom adapter along with a view holder.

Override getCount and getView

How to Display two dimensional Array in ListView?

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

Comments

0

i think you will have to use custom adapter for Books[1][] because its multi dimensional ! so use custom Adapter and override getView()

Comments

0
ArrayAdapter<String> adapter =new ArrayAdapter<String>(AddNewUnit.this, android.R.layout.simple_spinner_item, Books[1][]);

ArrayAdapter doesn't have any constructor that receives 2D array. Try extending BaseAdapter and create your own custom adapter.

public class YourCustomAdapter extends BaseAdapter{

String[][] your2Darray;

//Here's your constructor
public YourCustomAdapter(Context c,int layoutId,int resourceId, String[][] you2Darray){
        this.your2Darray=your2Darray;
....
      }
}....

Override getCount() and getView().

Comments

-1

You have Set the T type to be string and then you pass set of Book type objects, you need to change the String to "your data class"

2 Comments

This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post.
Sorry boss rich formatting removed the text between "<>"

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.