I'm trying to pass the value of a spinner into a different fragment so that it can be used in a different fragment to convert it to a different unit. I have the code here. I have an onitemselected method which takes the value of the spinner and displays it at the bottom of the application.
Any help would be appreciated.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_1, container, false);
String[] values =
{"Select Units:", "Miles", "Kilometers", "Meters", "Yards",};
final Spinner spinner = (Spinner) view.findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, values);
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
String[] values1=
{"Select Units:", "Miles", "Kilometers", "Meters", "Yards",};
Spinner spinner1 = (Spinner) view.findViewById(R.id.spinner1);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, values1);
adapter1.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner1.setAdapter(adapter1);
spinner1.setOnItemSelectedListener(this);
button1 =view.findViewById(R.id.button_confirm);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CharSequence input = spinner.
}
});
return view;
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String text = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(),text,Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
How to transfer some data to another Fragment?
In your fragment that creates the data, store it in a bundle and send it to the next fragment via arguments, then in a method which takes a bundle (like onCreateView), use the value that is found in the bundle.
Related
Hello!
I have tried to add a listview in a fragment but it doesn´t accept getApplicationContext:
adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, arrayList);
and this:
Toast.makeText(Todo.this, "" + parent.getItemAtPosition(position), Toast.LENGTH_SHORT).show();
How do I make it work for fragment instead of activity?
Code:
public class Todo extends Fragment {
EditText editText;
Button button;
ListView listView;
ArrayList<String> arrayList;
ArrayAdapter<String> adapter;
public Todo() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.todo, container, false);
editText = editText.findViewById(R.id.editText);
button = button.findViewById(R.id.button2);
listView = listView.findViewById(R.id.list);
arrayList = new ArrayList<>();
adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, arrayList);
listView.setAdapter(adapter);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String YeniVeri = editText.getText() .toString();
arrayList.add(YeniVeri);
adapter.notifyDataSetChanged();
editText.setText("");
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(Todo.this, "" + parent.getItemAtPosition(position), Toast.LENGTH_SHORT).show();
}
});
// Inflate the layout for this fragment
return view;
}
}
You can use requireActivity() or container.context in onCreatView. So you can also access "contex" via container.
Exp(Kotlin):
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
var mContext:Context? = container.context
use requireActivity() instead of getApplicationContext and "ActivityName.this"
am trying to use a spinner within a fragment to categorize the output of my recycler view
when I call the recycler view function from the onCreateView it works perfectly. however, when I call it from the spinner function, it requires 2 clicks to display.
here is the spinner XML code:
<Spinner
android:id="#+id/med_spinner"
android:layout_width="150dp"
android:layout_height="30dp"
android:layout_below="#+id/tv_med2"
android:layout_marginLeft="15dp"
android:layout_marginTop="20dp"
android:layout_toRightOf="#id/tv_spinner"
android:dropDownWidth="match_parent"
android:focusable="true"
android:gravity="center"
android:entries="#array/medicine"
/>
and here is the fragment:
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_medicine, container, false);
RecyclerView rv_med = view.findViewById(R.id.rv_medicine);
db = new UserDB(getActivity());
med_name = new ArrayList<>();
med_type = new ArrayList<>();
med_type2 = new ArrayList<>();
//spinner declartion
spinner = view.findViewById(R.id.med_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.medicine, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
//recycler view adapter and layout manger
recyclerViewAdapter = new RecyclerViewAdapterMedicine(getContext(), med_name, med_type, med_type2);
rv_med.setAdapter(recyclerViewAdapter);
rv_med.setLayoutManager(new LinearLayoutManager(getContext()));
return view;
}
and here is the spinner functions
#Override
public void onItemSelected(AdapterView<?> parent, View view, int i, long l) {
med_name.clear();
med_type.clear();
med_type2.clear();
cata = spinner.getSelectedItem().toString();
display(cata);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
cata = "All";
display(cata);
}
here's the display function:
void display(String type) {
db.open();
Cursor cursor = db.listMedicine();
if (cursor.getCount() == 0) {
Toast.makeText(getActivity(), "an error occurred", Toast.LENGTH_LONG).show();
} else {
if (type.equals("All")){
while (cursor.moveToNext()) {
med_name.add(cursor.getString(1));
med_type.add(cursor.getString(2));
med_type2.add(cursor.getString(3));
}
}else{
while (cursor.moveToNext()) {
if(cursor.getString(2).equals(type) || cursor.getString(3).equals(type)) {
med_name.add(cursor.getString(1));
med_type.add(cursor.getString(2));
med_type2.add(cursor.getString(3));
}
}
}
db.close();
}
}
I've tried to put the spinner functions within a spinner.onItemSelectedListener inside and outside the fragment, I tried to enable and disable focus and focusableTochMode, I also tried to call the recycler view before the spinner in the fragment.
You need to notify your RecyclerViewAdapter that data has changed. Add
recyclerViewAdapter.notifyDataSetChanged()
at the end of your display.
I want to remove spinner specific item using other spinner , both spinner are get data from string-array string.xml , such as if i choose kg from one spinner so kg is remove from other spinner
Here is a code to update you spinners :
List<String> data = new ArrayList<>();
List<String> data2 = new ArrayList<>();
//populate you lists in onCreate
String tempRemovedItem ;
Spinner spinner1 = (Spinner)findViewById(R.id.spinner1);
Spinner spinner2 = (Spinner)findViewById(R.id.spinner2);
ArrayAdapter<String> adapter1 = new ArrayAdapter<>(this,
android.R.layout.simple_dropdown_item_1line, data);
ArrayAdapter<String> adapter2 = new ArrayAdapter<>(this,
android.R.layout.simple_dropdown_item_1line, data2);
spinner1.setAdapter(adapter1);
spinner2.setAdapter(adapter2);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//when item is selected remove it from data2 and update the adapter of the second spinner
data2.remove(spinner1.getSelectedItem().toString());
if(tempRemovedItem != null){
data2.add(tempRemovedItem);
}
tempRemovedItem = spinner1.getSelectedItem().toString();
adapter2.notifyDataSetChanged();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
I have multiple spinners with a string array that has 3 items called "70","80" and "90". After the user selects these different spinner items I want to be able to sum up all of the items which the user has selected and display the total in a textview.For example a user selects "70" from one spinner and "70" from another, a textview will show the total 140. How can I do this?
public class FragmentCalc3 extends Fragment{
Context context;
TextView Results;
public static FragmentCalc3 newInstance()
{
FragmentCalc3 fragment = new FragmentCalc3();
return fragment;
}
public FragmentCalc3() {}
// Required empty public constructor
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_calculator3, container, false);
Results = (TextView) rootView.findViewById(R.id.Results);
context = container.getContext();
final Spinner spinner1 = (Spinner) rootView.findViewById(R.id.TestSpinner2);
ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(context, R.array.GradeTest, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // Specify the layout to use when the list of choices appears
spinner1.setAdapter(adapter1); // Apply the adapter to the spinner
final Spinner spinner2 = (Spinner) rootView.findViewById(R.id.TestSpinner1);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(context, R.array.GradeTest, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // Specify the layout to use when the list of choices appears
spinner2.setAdapter(adapter2); // Apply the adapter to the spinner
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int spinner2Value = Integer.parseInt(spinner2.getSelectedItem().toString());
int spinner1Value = Integer.parseInt(spinner1.getSelectedItem().toString());
int Results = spinner2Value + spinner1Value;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
return rootView;
}
}
If you have two spinners spinner2 and spinner1, And you want to get result when item of spinner2 is selected then simply do like this,
spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
int spinner2Value = Integer.parseInt(spinner2.getSelectedItem().toString()); //Add try catch block if necessary
int spinner1Value = Integer.parseInt(spinner1.getSelectedItem().toString());
int yourResult = spinner2Value + spinner1Value; //Result
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
//Do nothing
}
});
I have two java classes below that make a customListView. I got the code from somebody I'm working on an app with but I don't really understand her code. I want to add buttons that will bring you to an new activity when they are clicked, and change the text of "Description of "+itemname[positionof] to be different for each section(she has them all grouped so I can't change each individually).
I know that you can do something like
public void onLeaderboardClick(View view)
{
Intent intent = new Intent(MainActivity.this , Leaderboard.class);
startActivity(intent);
}
to start the new activites but as I've said above I'm just not sure how to split them up indivually so each can have a diffeernt onClickValue
public class ListMainActivity extends Activity {
ListView list;
String[] itemname ={
"Capture The Flag",
"Leaderboard",
"Log out"};
Integer[] imgId={
R.drawable.capturetheflag,
R.drawable.leaderboard,
R.drawable.logout,
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_main);
CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgId);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
String Selecteditem= itemname[+position];
Toast.makeText(getApplicationContext(), Selecteditem, Toast.LENGTH_SHORT).show();
}
});
}
}
and
public class CustomListAdapter extends ArrayAdapter<String>{
private final Activity context;
private final String[] itemname;
private final Integer[] imgid;
public CustomListAdapter(Activity context, String[] itemname, Integer[] imgid) {
super(context, R.layout.mylist, itemname);
this.context=context;
this.itemname=itemname;
this.imgid=imgid;
}
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.mylist, null,true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.item);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
TextView extratxt = (TextView) rowView.findViewById(R.id.textView1);
txtTitle.setText(itemname[position]);
imageView.setImageResource(imgid[position]);
extratxt.setText("Description of "+itemname[position]);
return rowView;
};
}
You can add individual on click listener to every element in the list item by specifying them in the custom adapter
How do i call onclick listener of a button which resides in listview item
View this question it might be helpful to you
I think you should add a button into row layut layout.
Then in getView() method of Adapter class :
Button btnClick= (Button) rowView.findViewById(R.id.btnClick);
btnClick.setOnClickListener(#Override
public void onClick(View v) {
//todo something with each button
});
For anyone looking back at this question, I ended up making a switch statement
# In your Adapter Class while inflating xml add a buttons as required
# and get the references for that buttons then write onClick on #that button in your getView method of Adapter
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.mylist, null,true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.item);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
TextView extratxt = (TextView) rowView.findViewById(R.id.textView1);
Button button =(Button) rowView.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Do your Work
Launch the Required Intent
}
});
txtTitle.setText(itemname[position]);
imageView.setImageResource(imgid[position]);
extratxt.setText("Description of "+itemname[position]);
return rowView;
};
You will have to use rowView.setOnClickListener() in the getViewMethod of the class custom adapter and inside that you can then specify which activity you wanna go based on the position of the view.