I am trying to make app with one text view, one button and one spinner. I make button work showing random values from string array, but i have a lot different string arrays(list1, list2 etc). Now my question is when change spinner position, how to get button get another string array(from spinner) and show it to textview.
How to pass value from spinner when something is selected in spinner to button.
Any help is appreciated.
Here is my code:
public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener {
Button btn;
public String[] myString,myString1;
public static final Random rgenerator = new Random();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//textview
final TextView tv = (TextView) findViewById(R.id.textureView);
final Resources res = getResources();
//string
myString = res.getStringArray(R.array.World_War_I);
myString1 = res.getStringArray(R.array.World_War_II);
//button
btn = (Button) findViewById(R.id.buttonxx);
btn.setOnClickListener(new View.OnClickListener(){
public void onClick (View v){
//i am missing code here, spinner position, and pass spinner position to if statement.
if (==0){
myString = res.getStringArray(R.array.list1);
String q = myString[rgenerator.nextInt(myString.length)];
tv.setText(q);
}
if (==1){
myString1 = res.getStringArray(R.array.list2);
String q1 = myString1[rgenerator.nextInt(myString.length)];
tv.setText(q1);
}
}
});
//drop list
Spinner spinner = (Spinner) findViewById(R.id.spinnerrrr);
spinner.setOnItemSelectedListener(this);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.kategorije, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
// when some cat selected
public void onItemSelected(AdapterView<?> parent, View view,
final int pos, long id) {
// An item was selected. You can retrieve the selected item using
parent.getItemAtPosition(pos);
parent.setSelection(0);
parent.getSelectedItemPosition();
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
Use this
// when some cat selected
public void onItemSelected(AdapterView<?> parent, View view,
final int pos, long id) {
// An item was selected. You can retrieve the selected item using
btn.setTag(pos+""); // Passing as string
parent.getItemAtPosition(pos);
parent.setSelection(0);
parent.getSelectedItemPosition();
}
and then
btn.setOnClickListener(new View.OnClickListener(){
public void onClick (View v){
//i am missing code here, spinner position, and pass spinner position to if statement.
int index = Integer.parseInt(btn.getTag().toString());
if (index==0){
myString = res.getStringArray(R.array.list1);
String q = myString[rgenerator.nextInt(myString.length)];
tv.setText(q);
}
if (index==1){
myString1 = res.getStringArray(R.array.list2);
String q1 = myString1[rgenerator.nextInt(myString.length)];
tv.setText(q1);
}
}
});
Related
I want to get object from ListView. I put my data from a csv file.
I have two column and around 100 rows. First column is Name, second is number.
I want get number after clicking on a row.
So I have in onCreate:
listView.setClickable(true);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Button btn2 = (Button) findViewById(R.id.button3);
btn2.setEnabled(true);
Object listItem = listView.getItemAtPosition(position);
String item = ( listView.getItemAtPosition(position).toString());
btn2.setText(item);
}
});
And after that when I click on a row (first and second column= the same score), my Button text shows [LJAVA.LANG.STRING;#42791450, every row have other numbers.
EDIT:
listView = (ListView) findViewById(R.id.list_view2);
itemArrayAdapter = new ItemArrayAdapter(getApplicationContext(), R.layout.single_list_item);
Parcelable state = listView.onSaveInstanceState();
listView.setAdapter(itemArrayAdapter);
listView.onRestoreInstanceState(state);
InputStream inputStream = getResources().openRawResource(R.raw.manager_number);
CSVReader csv = new CSVReader(inputStream);
List<String[]> scoreList = csv.read();
for (String[] scoreData : scoreList) {
itemArrayAdapter.add(scoreData);
}
If you have a way to retrieve the object from your ItemArrayAdapter, for example itemArrayAdapter.get(index), you could declare it as an instance attribute and use the position as index. Like so:
public class myActivity extends Activity {
private ItemArrayAdapter itemArrayAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
this.itemArrayAdapter = new ItemArrayAdapter(getApplicationContext(), R.layout.single_list_item);
InputStream inputStream = getResources().openRawResource(R.raw.manager_number);
CSVReader csv = new CSVReader(inputStream);
List<String[]> scoreList = csv.read();
for (String[] scoreData : scoreList) {
itemArrayAdapter.add(scoreData);
}
ListView listView = (ListView) findViewById(R.id.list_view2);
listView.setClickable(true);
Parcelable state = listView.onSaveInstanceState();
listView.setAdapter(itemArrayAdapter);
listView.onRestoreInstanceState(state);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Button btn2 = (Button) findViewById(R.id.button3);
btn2.setEnabled(true);
String item = this.itemArrayAdapter.get(position);;
btn2.setText(item);
}
});
spinner a have 2 item : poison and non_poison
spinner b have 2 item : dart frog and viper
spinner c have 2 item : chicken and eggs
long story short, i want to make
"IF" selected item on spinner a = poison
"THEN" spinner b = visible, spinner c = gone
heres my code, tried using setOnItemSelectedListener, no error shown, but its not working the way it supposed to be, am i missing something in here ? please help me....
public class eatable extends Fragment {
public String spinner1x;
#Nullable
#Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
final FrameLayout mRelative = (FrameLayout) inflater.inflate(R.layout.berita_layout, container, false);
final TextView text1 = (TextView) mRelative.findViewById(R.id.text_test1);
final TextView text2 = (TextView) mRelative.findViewById(R.id.text_test2);
final TextView text3 = (TextView) mRelative.findViewById(R.id.text_test3);
final Spinner spinner1 = (Spinner) mRelative.findViewById(R.id.spinner_test1);
final Spinner spinner2 = (Spinner) mRelative.findViewById(R.id.spinner_test2);
final Spinner spinner3 = (Spinner) mRelative.findViewById(R.id.spinner_test3);
spinner1x = spinner1.getSelectedItem().toString();
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> mRelative, View selectedItemView, int position, long id) {
// your code here
if (spinner1x.equals("poison")){
spinner2.setVisibility(View.VISIBLE);
spinner3.setVisibility(View.GONE);
}
}
});
return mRelative;
}
}
You forgot to retrieve the currently selected item of your spinner & save it to your spinner1x string, & to implement the onNothingSelected method:
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> mRelative, View selectedItemView, int position, long id) {
spinner1x = mRelative.getItemAtPosition(position).toString();
// your code here
if (spinner1x.equals("poison")){
spinner2.setVisibility(View.VISIBLE);
spinner3.setVisibility(View.GONE);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
});
The AdapterView.OnItemSelectedListener requires the onItemSelected()
and onNothingSelected() callback methods.
Android Spinners
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> mRelative, View selectedItemView, int position, long id) {
// Use spinner item position for this.
if (position == 0){
spinner2.setVisibility(View.VISIBLE);
spinner3.setVisibility(View.GONE);
}
}
});
I am just using your code with corrections..
public class eatable extends Fragment {
public String spinner1x;
#Nullable
#Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
final FrameLayout mRelative = (FrameLayout) inflater.inflate(R.layout.berita_layout, container, false);
final TextView text1 = (TextView) mRelative.findViewById(R.id.text_test1);
final TextView text2 = (TextView) mRelative.findViewById(R.id.text_test2);
final TextView text3 = (TextView) mRelative.findViewById(R.id.text_test3);
final Spinner spinner1 = (Spinner) mRelative.findViewById(R.id.spinner_test1);
final Spinner spinner2 = (Spinner) mRelative.findViewById(R.id.spinner_test2);
final Spinner spinner3 = (Spinner) mRelative.findViewById(R.id.spinner_test3);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> mRelative, View selectedItemView, int position, long id) {
// your code here
spinner1x = spinner1.getSelectedItem().toString();
if (spinner1x.equals("poison")){
spinner2.setVisibility(View.VISIBLE);
spinner3.setVisibility(View.GONE);
}else {
spinner2.setVisibility(View.GONE);
spinner3.setVisibility(View.VISIBLE);
}
}
});
return mRelative;
}
}
my idea is to have a text field where I can input a name manually.
Additionally I do have a list of names. If there is at east 1 name in that list an icon should appear on the right of the EditText in order to open a Spinner to give a selection.
If no item is selected the EditText stays the same and if an item has been selected the EditText will have that item.
I'mworking with visibilities so far - but my problem is that the onNothingSelected seems not to get fired at all.
public class MainActivity extends ActionBarActivity {
private Spinner spinner;
private EditText text2;
private ImageButton button2;
private String spinner_value = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner)findViewById(R.id.spinner);
String[] str = {"","good", "dislike", "like", "hate", "moderate"};
spinner.setPrompt("Set Text");
ArrayAdapter<String> list = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, str);
spinner.setAdapter(list);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
TextView tv = (TextView)arg1;
spinner_value = tv.getText().toString();
if(spinner_value.length() == 0)
{
spinner_value = "Nothing";
}
Toast.makeText(getApplicationContext(), spinner_value, Toast.LENGTH_SHORT).show();
spinner = (Spinner) findViewById(R.id.spinner);
text2 = (EditText) findViewById(R.id.editText);
button2 = (ImageButton) findViewById(R.id.imageButton);
text2.setVisibility(View.VISIBLE);
button2.setVisibility(View.VISIBLE);
spinner.setVisibility(View.INVISIBLE);
String myStr = spinner.getSelectedItem().toString();
text2.setText(myStr);
text2.requestFocus();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
Toast.makeText(getApplicationContext(), "NothingSelected", Toast.LENGTH_SHORT).show();
spinner = (Spinner) findViewById(R.id.spinner);
text2 = (EditText) findViewById(R.id.editText);
button2 = (ImageButton) findViewById(R.id.imageButton);
text2.setVisibility(View.VISIBLE);
button2.setVisibility(View.VISIBLE);
spinner.setVisibility(View.INVISIBLE);
text2.requestFocus();
}
});
}
}
By default, the item at index 0 is selected when you spinner is created. So, even though the user doesn't actually do anything, the first item is selected. You can see this happen by setting a breakpoint (or some other notification) in onItemSelected() and seeing it get triggered on startup without the user doing anything.
For the onNothingSelected() callback to get triggered, whatever item is selected has to disappear.
I have an OnItemClick(), after that I wanted to get the ItemClicked name's and display it in a TextView.I do it normally, but the problem is that I get the ItemClicked name's inside a bracket! How do I avoid this?Here is the output:
{titre=hello}
I want only in my TextView:
hello.
Here is the code:
public void onItemClick(AdapterView<?> parent, View view, int position, long id ) {
displayTextViewTitle = (TextView)findViewById(R.id.text_view_title);
Object item = parent.getItemAtPosition(position);
String value = item.toString();
displayTextViewTitle.setText(String.valueOf(value));
}
Try this :
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
TextView displayTextViewTitle = (TextView) findViewById(R.id.textView1);
TextView item = (TextView) parent.getItemAtPosition(position);
String value = item.getText();
displayTextViewTitle.setText(value);
}
or even better try :
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
TextView displayTextViewTitle = (TextView) findViewById(R.id.textView1);
ListView lv = (ListView) findViewById(yourListViewName);
String value = lv.get(position).tostring();
displayTextViewTitle.setText(value);
}
If you are getting your String {titre=hello} like this after click on ListView. then you should use this:
String[] no = item.split("=");
String m = no[0];
String k = no[1];
String name = k.substring(0, k.length() - 1);
name is your final result String .
value is already a String, you don't have to use String.valueOf(value).
The listener method works fine, check this:
public class SplashActivity extends Activity implements OnItemClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
ListView lv = (ListView)findViewById(R.id.lv);
ArrayList<String> members = new ArrayList<String>();
members.add("titre=hello");
members.add("world");
ArrayAdapter s = new ArrayAdapter(this, android.R.layout.simple_list_item_1, members);
lv.setAdapter(s);
lv.setOnItemClickListener(this);
}
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
TextView displayTextViewTitle = (TextView) findViewById(R.id.textView1);
Object item = parent.getItemAtPosition(position);
String value = item.toString();
displayTextViewTitle.setText(String.valueOf(value));
}
}
Recheck the values inside your listview, the problem might be there
I have problem with my spinners,
in my app are two spinners which gets data from database,
when i choose one option from first spinner, i want to remove this option from second spinner,
and add option which i removed from second spinner to first spinner and vice versa.
How can i do that ?
This is my present code:
public void showScoresCalculator() {
setContentView(R.layout.scores_calculator);
Typeface font = Typeface.createFromAsset(getAssets(),"fonts/HelveticaProCn.ttf");
TextView txt = (TextView) findViewById(R.id.txt);
txt.setTypeface(font);
TextView team = (TextView) findViewById(R.id.choose_team);
team.setTypeface(font);
TextView group = (TextView) findViewById(R.id.choose_group);
group.setTypeface(font);
TextView title = (TextView) findViewById(R.id.textView1);
title.setTypeface(font);
TextView title2 = (TextView) findViewById(R.id.textView2);
title2.setTypeface(font);
final Spinner sp = (Spinner) findViewById(R.id.spinner3);
final Spinner sp1 = (Spinner) findViewById(R.id.spinner4);
final DataBaseHelper.MyData[] items = this.dh.getTeamsNameAndIDs("38");
final DataBaseHelper.MyData d = items[(int) sp.getSelectedItemId()];
final DataBaseHelper.MyData[] items2 = this.dh
.getTeamsNameAndIDs("47");
final DataBaseHelper.MyData d1 = items2[(int) sp1.getSelectedItemId()];
final ArrayAdapter<DataBaseHelper.MyData> adapter = new ArrayAdapter<DataBaseHelper.MyData>(
this, android.R.layout.simple_spinner_item,
this.dh.getTeamsNameAndIDs("38"));
final ArrayAdapter<DataBaseHelper.MyData> adapter1 = new ArrayAdapter<DataBaseHelper.MyData>(
this, android.R.layout.simple_spinner_item,
this.dh.getTeamsNameAndIDs("47"));
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
DataBaseHelper.MyData d = items2[(int) sp1.getSelectedItemId()];
DataBaseHelper.MyData d1 = items[position];
home = d.getValue();
away = d1.getValue();
if(Integer.valueOf(home.toString()) == Integer.valueOf(away.toString())) {
Toast.makeText(getApplicationContext(), "Nie możesz wybrać dwóch tych samych drużyn.", Toast.LENGTH_SHORT).show();
}else {
showChart(d1.getValue(), d.getValue(), d1.getSpinnerText(), d.getSpinnerText(), "REMIS");
}
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(adapter1);
sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
DataBaseHelper.MyData d = items[(int) sp.getSelectedItemId()];
DataBaseHelper.MyData d1 = items2[position];
home = d.getValue();
away = d1.getValue();
if(Integer.valueOf(home.toString()) == Integer.valueOf(away.toString())) {
Toast.makeText(getApplicationContext(), "Nie możesz wybrać dwóch tych samych drużyn.", Toast.LENGTH_SHORT).show();
}else {
showChart(d.getValue(), d1.getValue(), d.getSpinnerText(), d1.getSpinnerText(), "REMIS");
}
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
final Button next = (Button) findViewById(R.id.next);
final Button prev = (Button) findViewById(R.id.prev);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showNewsPage();
}
});
prev.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showGroupCalculator();
}
});
Page = true;
}
Please, help me ! :)
Update the adapter by removing the selected data from the list. Use Handler concept to update adapter.
Set a new adapter on the Spinner via setAdapter(). Or, if it is an ArrayAdapter, use add(), insert(), and remove() to modify the data. Or, if it is a CursorAdapter, requery() it to get fresh data.