I'm making a project for university, I have a DialogFragment where I record a new animal for a database. In the form, there is a spinner that is loaded from a database. This is the "onCreate" code of DialogFragment:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
LayoutInflater inflater = getActivity().getLayoutInflater();
View v = inflater.inflate(R.layout.dialog_add_animal, null);
Spinner spEspecies = (Spinner) v.findViewById(R.id.spinner);
Cursor c = getActivity().getContentResolver().query(ContratoBBDD.Especies.URI_CONTENIDO,null,null,null,null);
SimpleCursorAdapter adaptador =
new SimpleCursorAdapter(getActivity().getApplicationContext() ,android.R.layout.simple_spinner_item,c,
new String[]{ContratoBBDD.Especies._id},
new int [] {android.R.id.text1},SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER );
Log.d("Tam", c.getCount() + "");
Log.d("Col", c.getColumnCount() + "");
adaptador.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spEspecies.setAdapter(adaptador);
ActionBar actionBar = ((AppCompatActivity) getActivity())
.getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDefaultDisplayHomeAsUpEnabled(false);
}
I can see in the log and debugging that the cursor has information, as does the spinner -- but when I click in the spinner, it's empty. Where do I lose the information?
You can look this link
Putting cursor data into an array
//the constructor
Spinner spEspecies = (Spinner) v.findViewById(R.id.spinner);
//set the value to show
//change this line with query, better use ArrayList<String>
String Especies[] = {"Homo Sapiens","Felis Domestica","Zea mays"};
//insert to spinner
ArrayAdapter<String> aaspEspecies = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, Especies);
// The drop down view
aaspEspecies.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spEspecies.setAdapter(aaspEspecies);
Related
i am working on spinner regarding medicines,when i select number of medicines i want to order i am using spinner and if i select 4 in the spinner then i get 4 fields which display each name of the medicine
but i got struck on the fields which will be depending on the spinner number(if i want 4 fields i give number 4 to the spinner and thus i will get 4 fields displayed,and if i want only 3 then i only get 3 fields ) the fields i want to display them so that user can enter the required data
Code:
public class SpecficTheMedicines extends ActionBarActivity {
Toolbar spcifytoolbar;
Spinner numberdrop;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.letmespecify);
spcifytoolbar = (Toolbar) findViewById(R.id.spcifytoolbar);
setSupportActionBar(spcifytoolbar);
assert getSupportActionBar() != null;
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
numberdrop = (Spinner) findViewById(R.id.numberdrop);
ArrayList<String> number = new ArrayList<String>();
number.add("1");
number.add("2");
number.add("3");
number.add("4");
number.add("5");
ArrayAdapter<String> spinneradapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, number);
spinneradapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
numberdrop.setAdapter(spinneradapter);
showInfo();
}
public void showInfo(){
LayoutInflater layoutInfralte=(LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout linearLayout=(LinearLayout)findViewById(R.id.infolayout);
List views=new ArrayList();
Iterator<latlng>ite=NavigationContext.getInstance().getArray().iterator();
latlng temp;
while(ite.hasNext()){
temp=ite.next();
View view=layoutInfralte.inflate(R.layout.infoitem, null);
// Edit
TextView tv=(TextView)view.findViewById(R.id.TextAddress);
tv.setText(temp.address);
view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
views.add(view);
}
for(int i = 0; i<views.size(); i++)
linearLayout.addView((View) views.get(i));
}
}
I am getting successfully previous activity value through intent. The get value store in TextView.
Only work else part of this code. 'If' part not working. I place this code in 'onCreate' method.
I want to do if text view contains "English" spinner should be run "English_array. if "Arabic" should be run "Arabic_array.
I want to work 'if' also depending on Textview's change.
public class TweetDetailActivity extends AppCompatActivity implements com.wdullaer.materialdatetimepicker.date.DatePickerDialog.OnDateSetListener,
DialogInterface.OnCancelListener{
Toolbar toolbar;
String[] subject;
String[] stages;
String[] via;
int position;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tweet_detail);
TextView Title = (TextView) findViewById(R.id.tweetTitle);
if (TitleName.toString().equals("English")){
Spinner staticSpinner = (Spinner) findViewById(R.id.static_spinner);
ArrayAdapter<CharSequence> staticAdapter = ArrayAdapter
.createFromResource(this, R.array.English_array,
android.R.layout.simple_spinner_item);
staticAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
staticSpinner.setAdapter(staticAdapter);
}else if(TitleName.toString().equals("Arabic")){
Spinner staticSpinner = (Spinner) findViewById(R.id.static_spinner);
ArrayAdapter<CharSequence> staticAdapter = ArrayAdapter
.createFromResource(this, R.array.Arabic_array,
android.R.layout.simple_spinner_item);
staticAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
staticSpinner.setAdapter(staticAdapter);
}else {
Spinner staticSpinner = (Spinner) findViewById(R.id.static_spinner);
ArrayAdapter<CharSequence> staticAdapter = ArrayAdapter
.createFromResource(this, R.array.Tamil_array,
android.R.layout.simple_spinner_item);
staticAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
staticSpinner.setAdapter(staticAdapter);
}
// Retrieve data from MainActivity on listview item click
Intent i = getIntent();
// Get the listview item click subject
subject= i.getExtras().getInt("subject");
// Get the list of stages
stages = i.getStringArrayExtra("stages");
// Get the list of via
via = i.getStringArrayExtra("via");
// Locate the TextViews in singleitemview.xml
Title = (TextView) findViewById(R.id.tweetTitle);
Body = (TextView) findViewById(R.id.tweetBody);
Date = (TextView) findViewById(R.id.tweetDate);
// Load the text into the TextViews followed by the position
Title.setText(subject[position]);
Body.setText(stages[position]);
Date.setText(via[position]);
}
}
Title(Transfer the listview value to textview) :
public class MainActivity_Third extends android.support.v4.app.ListFragment {
ListView list;
TweetAdapter adapter;
String[] subject;
String[] stages;
String[] via;
int i = 0;
View v;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from listview_main.xml
//getActivity().setContentView(R.layout.activity_main_activity__third);
v = inflater.inflate(R.layout.activity_main_activity__third, container, false);
i++;
subject= new String[]{"Arabic","English","Tamil"};
stages = new String[]{"Certificate" ,"| Diploma ","| Higher Diploma"};
via= new String[]{"Tamil | Distance Learning, Direct", "Tamil | Distance Learning, Direct",
"Tamil | Distance Learning, Direct", "Tamil | Distance Learning, Direct"};
list = (ListView) v.findViewById(android.R.id.list);
adapter = new TweetAdapter(MainActivity_Third.this.getActivity(), rank, country, population);
list.setAdapter(adapter);
return v;
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Intent i = new Intent(MainActivity_Third.this.getActivity(), TweetDetailActivity.class);
// Pass all data subject
i.putExtra("subject", subject);
// Pass all data stages
i.putExtra("stages", stages);
// Pass all data via
i.putExtra("via", via);
// Pass listview item click position
i.putExtra("position", position);
// Open SingleItemView.java Activity
startActivity(i);
}
}
My res XML code below:
<string-array name="English_array">
<item>Certificate in English</item>
<item>Diploma in English</item>
<item>Higher Diploma in English</item>
</string-array>
<string-array name="Arabic_array">
<item>Diploma in Arabic</item>
<item>Higher Diploma in Arabix</item>
</string-array>
<string-array name="Tamil_array">
<item>Diploma in Tamil</item>
<item>Higher Diploma in Tamil</item>
</string-array>
here is spinner XML :
<Spinner
android:id="#+id/static_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp" />
I am working on EditText and Spinner . When I set text for EditText it were fine but when I try to set text for Spinner it give error.
this is code for set text EditText
New_Name.setText(NewName);
here New_Name is EditText
this is code for set text Spinner
New_Quantity.setText(NewQuantity);
here New_Quantity is Spinner
this is the full code.
public class UpdateFoodActivity extends Activity {
EditText Name_Search,New_Name,New_Calorie,New_Fat,New_Protein,New_Sugar,New_carbohydrates;
Spinner New_Quantity;
TextView title_text;
String SearchName,NewName,NewQuantity,NewCalorie,NewFat,NewProtein,NewSugar,Newcarbohydrates;
FoodDbHelper foodDbHelper;
SQLiteDatabase sqLiteDatabase;
Button UpdateButton;
ArrayAdapter<CharSequence> adapter;
private Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.update_food_activity);
Name_Search = (EditText)findViewById(R.id.name_search);
New_Name = (EditText)findViewById(R.id.new_name);
New_Quantity = (Spinner)findViewById(R.id.new_quantity);
adapter = ArrayAdapter.createFromResource(this, R.array.quant, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
New_Quantity.setAdapter(adapter);
New_Quantity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
New_Calorie = (EditText)findViewById(R.id.new_calorie);
New_Fat = (EditText)findViewById(R.id.new_fat);
New_Protein = (EditText)findViewById(R.id.new_protein);
New_Sugar = (EditText)findViewById(R.id.new_sugar);
New_carbohydrates = (EditText)findViewById(R.id.new_vitamins);
UpdateButton = (Button)findViewById(R.id.update_button);
title_text = (TextView)findViewById(R.id.title_text);
New_Name.setVisibility(View.GONE);
New_Quantity.setVisibility(View.GONE);
New_Calorie.setVisibility(View.GONE);
New_Fat.setVisibility(View.GONE);
New_Protein.setVisibility(View.GONE);
New_Sugar.setVisibility(View.GONE);
New_carbohydrates.setVisibility(View.GONE);
UpdateButton.setVisibility(View.GONE);
title_text.setVisibility(View.GONE);
}
public void searchFood(View view){
SearchName = Name_Search.getText().toString();
foodDbHelper = new FoodDbHelper(getApplicationContext());
sqLiteDatabase = foodDbHelper.getReadableDatabase();
Cursor cursor = foodDbHelper.getFood(SearchName,sqLiteDatabase);
if (cursor.moveToFirst()) {
NewQuantity = cursor.getString(0);
NewCalorie = cursor.getString(1);
NewFat = cursor.getString(2);
NewProtein = cursor.getString(3);
NewSugar = cursor.getString(4);
Newcarbohydrates = cursor.getString(5);
NewName = SearchName;
New_Name.setText(NewName);
New_Quantity.setText(NewQuantity);
New_Calorie.setText(NewCalorie);
New_Fat.setText(NewFat);
New_Protein.setText(NewProtein);
New_Sugar.setText(NewSugar);
New_carbohydrates.setText(Newcarbohydrates);
New_Name.setVisibility(View.VISIBLE);
New_Quantity.setVisibility(View.VISIBLE);
New_Calorie.setVisibility(View.VISIBLE);
New_Fat.setVisibility(View.VISIBLE);
New_Protein.setVisibility(View.VISIBLE);
New_Sugar.setVisibility(View.VISIBLE);
New_carbohydrates.setVisibility(View.VISIBLE);
UpdateButton.setVisibility(View.VISIBLE);
title_text.setVisibility(View.VISIBLE);
}
else
{
Toast.makeText(getApplicationContext(),
"No Dish Found " ,
Toast.LENGTH_SHORT).show();
}
}
public void updatebutton(View view)
{
foodDbHelper = new FoodDbHelper(getApplicationContext());
sqLiteDatabase = foodDbHelper.getWritableDatabase();
String name,quantity,calorie,fat,protein,sugar,carbohydrates;
name = New_Name.getText().toString();
quantity = New_Quantity.getSelectedItem().toString();
calorie = New_Calorie.getText().toString();
fat = New_Fat.getText().toString();
protein = New_Protein.getText().toString();
sugar = New_Sugar.getText().toString();
carbohydrates = New_carbohydrates.getText().toString();
int count = foodDbHelper.updateInformation(SearchName,name,quantity,calorie,fat,protein,sugar,carbohydrates,sqLiteDatabase);
Toast.makeText(getApplicationContext(),count+" dish updated",Toast.LENGTH_LONG).show();
Intent intent = new Intent(this,UpdateFoodActivity.class);
startActivity(intent);
}
}
In order to add entries to the spinner you need to define an Adapter i give you an example:
spinner = (Spinner) findViewById(R.id.spinner2);
List<String> list = new ArrayList<String>();
list.add("list 1");
list.add("list 2");
list.add("list 3");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.spinner_item, list);
spinner.setAdapter(dataAdapter);
Spinner likes ListView, need an adapter for rendering data. Adapter, in simple term, is a collection of items. (As you see ListView always contains list of item and also Spinner). Nevertheless, Adapter of spinner often a list of string. (because spinner just represents user choice)
In your example, You can simply create ArrayAdapter and assign again to Spinner.
private void setTextForSpinner(Spinner spinner) {
String[] items = {"Stack", "Over", "Flow"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);
spinner.setAdapter(adapter);
}
If apply above code to your example, you can change again to:
private void setTextForSpinner(Spinner spinner, String NewName) {
String[] items = {NewName};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);
spinner.setAdapter(adapter);
}
Hope this help :)
I am trying to add data from two different sources to an array adapter. One source is from a spinner containing hard coded strings, the other is to allow the user to create their own string to pass to the array (via the adapter). Here is my code below. It appears to me that the array adapter can only except one data source according to the arguments that can be passed to it.......ie
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource
(this, R.array.alarmList, android.R.layout.simple_spinner_item );
Here I can only guess that the actual string input is gathered from......... android.R.layout.simple_spinner_item
Do I need to use another array adapter or is there a way to add my string variable to the adaoter as well as the item the user has chosen from the spinner? I've done some research here but drawing a blank!!Many thanks. Here's my code which tries to enter strings to adapter..........
public class NewAlarm extends Activity {
Spinner alarms;
//private Button b = (Button) findViewById(R.id.btnAddCustom);
final EditText et = (EditText) findViewById(R.id.edittext);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newalarm);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.alarmList, android.R.layout.simple_spinner_item );
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
//et.setText("name");
alarms = (Spinner) findViewById(R.id.cmbAlarms);
alarms.setAdapter(adapter);
}
public void addAlarm(View view) {
MainMenu.alarmList.add(new Alarm(alarms.getSelectedItem().toString()));
Toast.makeText(getApplicationContext(), "Added " + alarms.getSelectedItem().toString() + " alarm.", Toast.LENGTH_SHORT).show();
NewAlarm.this.finish();
}
public void addCustomAlarm (View view){
MainMenu.alarmList.add(new Alarm(et.getText().toString()));
}
}
And here's my array adapter code............
public View getView(final int position, View convertView, ViewGroup viewGroup) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.alarm_item, null);
}
Thanks guys!!
// Get a handle on the UI controls
TextView name = (TextView) convertView.findViewById(R.id.txtAlarmName);
final TextView timeStamp = (TextView) convertView.findViewById(R.id.txtTimeStamp);
// if the value of the timestamp from the alarm at the position selected is not a null value then set the text label to the alarm timestamp value
if (MainMenu.alarmList.get(position).getTimeStamp() != null)
{
timeStamp.setText(MainMenu.alarmList.get(position).getTimeStamp().toString());
}
// Set the alarm name
name.setText(listItems.get(position).getName());
// Get a handle on the button
Button btnCheckNow = (Button) convertView.findViewById(R.id.btnCheckNow);
btnCheckNow.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
// Set the timestamp of the alarm object at the selected position
MainMenu.alarmList.get(position).setTimeStamp();
// Set the timestamp label
timeStamp.setText(MainMenu.alarmList.get(position).getTimeStamp().toString());
}
});
return convertView;
}
Instead of calling ArrayAdapter.createFromResource(), import the array and add the string before creating the adapter :
CharSequence[] array = Arrays.asList(
getResources().getTextArray(R.array.alarmList));
List<CharSequence> list = new ArrayList<CharSequence>(array);
list.add("custom string");
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this,
android.R.layout.simple_spinner_item, list);
I'm getting the custom ringtone ID by caller and basically I want to do a setSelection(); to my spinner with that ringtone but I can't figure out how to match positions..
What I've tried is to match a string "Ringtone Name" to find it's position in a Spinner from it's cursor. Am I going about it the right way? Or is there an easier way to match ringtone ie:content://media/internal/audio/media/72 to the position in the spinner?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
mRingtoneManager2 = new RingtoneManager(this); //adds ringtonemanager
mRingtoneManager2.setType(RingtoneManager.TYPE_RINGTONE); //sets the type to ringtones
mRingtoneManager2.setIncludeDrm(true); //get list of ringtones to include DRM
mCursor2 = mRingtoneManager2.getCursor(); //appends my cursor to the ringtonemanager
startManagingCursor(mCursor2); //starts the cursor query
String[] from = {mCursor2.getColumnName(RingtoneManager.TITLE_COLUMN_INDEX)}; // get the list items for the listadapter could be TITLE or URI
int[] to = {android.R.id.text1}; //sets the items from above string to listview
// create simple cursor adapter
SimpleCursorAdapter adapter =
new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, mCursor2, from, to );
adapter.setDropDownViewResource( android.R.layout.simple_spinner_item);
// get reference to our spinner
s = (Spinner) findViewById( R.id.spinner1);
s.setAdapter(adapter);
getValue(mCursor2);
s.setSelection(posid);
s.setOnItemSelectedListener(this);
}
public String getValue(Cursor cursor) {
String posid = "Loopy Lounge";
while (cursor.moveToNext()) {
posid = cursor.getString(0);
}
return;
}