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?