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));
}
}
Related
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);
Hi guys I'm having difficulty figuring out why my buttons wont display the right text and display junk code. I am running a serverRequest then creating the buttons after the details are gotten and I surely did test I am getting the right string back from the server.
public void getCourses(User user) {
ServerRequest serverRequest = new ServerRequest(this);
serverRequest.fetchUserCoursesDataInBackground(user, new getUserCallback() {
#Override
public void doneString(String[] returnedString) {
if (returnedString == null) {
System.out.println("DONE EMPTY");
} else {
userLocalStore.storeUserCourses(returnedString);
final ListView listView = (ListView) findViewById(R.id.viewCourseList);
final ArrayList<Button> list = new ArrayList<>();
View v = getWindow().getDecorView();
for (int i = 0; i < returnedString.length; i++) {
System.out.println("This is in for loop:" +returnedString[i]);
Button button = new Button(v.getContext());
button.setText(returnedString[i]);
button.setId(i);
button.setHeight(40);
button.setWidth(100);
list.add(button);
}
if (list.isEmpty()) {
System.out.println("List is empty bro");
} else {
final ArrayAdapter adapter = new ArrayAdapter(v.getContext(), android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter);
System.out.println("Adding adapter");
}
}
}
this is the whole code. I will show you what it is displaying on the application
this is the bug
I see 3 problems in this code:
1st i assume java can mess this 1 up:
replace> final ArrayList list = new ArrayList<>();
with> final ArrayList list = new ArrayList();
2nd you are using a layout "android.R.layout.simple_list_item_1" for the ArrayAdapter which is an xml layout including a text view thus you screenshot shows 2 text views and not buttons.
3rd new ArrayAdapter() constructor takes a list of Strings. so if you...
replace> final ArrayAdapter adapter = new ArrayAdapter(v.getContext(), android.R.layout.simple_list_item_1, list);
with> final ArrayAdapter adapter = new ArrayAdapter(v.getContext(), android.R.layout.simple_list_item_1, returnedString);
you will see buttons with the text that you are printing in your loop.
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 have a ListView with multiple checkboxes (one for each list item). What would be the best way to create an array containing information from the selected checkboxes.
For Example, here's a list.
Item 1 "Ham" Checked
Item 2 "Turkey" NotChecked
Item 3 "Bread" Checked
I would like to create an array containing "ham" and "turkey"
If you used ListView's built-in checkbox method with setChoiceMode() then you only need to call getCheckedItemIds() or getCheckedItemPositions().
public class Example extends Activity implements OnClickListener {
ListView mListView;
String[] array = new String[] {"Ham", "Turkey", "Bread"};
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, array);
mListView = (ListView) findViewById(R.id.list);
mListView.setAdapter(adapter);
mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
}
public void onClick(View view) {
SparseBooleanArray positions = mListView.getCheckedItemPositions();
int size = positions.size();
for(int index = 0; index < size; index++) {
Log.v("Example", "Checked: " + array[positions.keyAt(index)]);
}
}
}
If the Adapter is bound to a Cursor, then this becomes much more practical.
I create checkboxes dynamically & i want to bind that checkboxes to listview.
How can I do that?
Here i give my code--
public class HomeActivity extends ListActivity{
CheckBox[] chk;
ListView lv1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv1=(ListView)findViewById(R.id.listTasks);
tv1=(TextView) findViewById(R.id.tvMsg);
The database section:
db = new DBAdapter(HomeActivity.this);
db.open();//int[] id=new int[]{Integer.parseInt(DBAdapter.ID)};
Cursor cr=db.getUncompletedTask();//my database function to retrieve values to create checkboxes
if (cr.moveToFirst()) {
do {
String[] str=new String[2];
str[0]=cr.getString(0);
str[1]=cr.getString(1);
al.add(str);
} while (cr.moveToNext());
}
startManagingCursor(cr);
String[] tasks = new String[] { DBAdapter.KEY_TODO };
Creation of checkbox:
int[] idchk=new int[al.size()];//here i am creating checkbox dynamicaly
if (al.size() != 0) {
chk = new CheckBox[al.size()];
System.out.println(al.size());
for (int i = 0; i < al.size(); i++) {
String[] s = (String[]) al.get(i);
System.out.println("ID: "+s[0]);
Task_Id = Integer.parseInt(s[0]);
Task_Nm = s[1];
chk[i] = new CheckBox(HomeActivity.this);
System.out.println(i +"task id"+Task_Id +"parseint"+Integer.parseInt(s[0]+chk[i].getText().toString()));
chk[i].setId(Task_Id);
idchk[i]=Task_Id;
chk[i].setText(Task_Nm);
//lv1.addView(chk[i]);
//setContentView(lv1);
}}}
Here what can i write here so that this dynamically created checkboxes will be bind to listview
What if you use the default listview of android with checkbox.
By using :
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, COUNTRIES));
where COUNTRIES is static final string array that contains the item to show..
You can use customized ListView whose rows contains CheckBox. Create your own adapter extending ArrayAdapter and it's overridden method getView create your checkboxes.