英文:
How do I add multiple values from a Spinner to a TextView in Android?
问题
我正在构建一个使用Firebase的简单杂货订购应用程序,其中我想要两个下拉框和一个按钮。
- 一个下拉框用于选择商品名称和商品价格标签(例如 - '薯片 -5卢比-')。
- 第二个下拉框用于选择数量(例如 - '5包')。
- 一个按钮用于从下拉框获取值并将其放入下方的TextView中(一个接一个地添加商品)。
例如,从下拉框添加了几个商品后,下方的TextView应如下所示:
- 薯片 -5卢比- 5包
- 饼干 -20卢比- 20包
- 面条 -15卢比- 2包
等等。。
我应该如何实际实现它?如果有人能帮助我提供实际的Java代码,我将不胜感激。我对Android开发还很新,对此非常感兴趣.. 任何帮助将不胜感激..
谢谢..
英文:
I am building a simple grocery ordering App with firebase, where I want two spinners and a button.
- One Spinner for the item name and item price tag. (For example - 'Chips -5rs-')
- Second spinner for the quantity. (for Example - '5 packet')
- A button for taking the values from the spinners and putting it to the TextView below. (One item after the other.)
For Example, after adding several items from the spinners, the textview below should look like this. -
- Chips -5rs- 5 packet
- Biscuits -20rs- 20 packet
- Noodles -15rs- 2 packet
and so on..
How do I actually implement it? and if anyone could help me with actual java codes, I would be very thankful. I've also attached a picture below of how I am trying to Implement it. I am new to Android Development and I have keen interest in it.. any help would be deeply appreciated..
Thankyou ..
答案1
得分: 2
定义你的全局字符串变量
String priceTag, quantityTag;
然后从下拉列表框中获取值,像这样:
yourSpinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
priceTag = yourSpinner1.spinnerDropDownView.getSelectedItem().toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
与从第二个下拉列表框中获取值相同:
yourSpinner2.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
priceTag = yourSpinner1.spinnerDropDownView.getSelectedItem().toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
然后在按钮点击时显示数据,如下所示:
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
yourtextViewId.setText(priceTag + " - " + quantityTag);
}
});
英文:
Define your string global
String priceTag,quantityTag;
then get value from spinner like this
yourSpinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
priceTag= yourSpinner1.spinnerDropDownView.getSelectedItem().toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
same as getting value from your second spinner
yourSpinner2.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
priceTag= yourSpinner1.spinnerDropDownView.getSelectedItem().toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
and show data on button click like this
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
yourtextViewId.setText(priceTag " - " quantityTag);
}
});
答案2
得分: -1
你可以这样做,
首先获取两个微调器的值
//从微调器1获取值
String s1_val = spinner1.getSelectedItemsAsString();
//从微调器2获取值
String s2_val = spinner2.getSelectedItemsAsString();
将这些值连接起来并设置到文本视图中。
//连接并设置到textView
textView.setText(s1_val.concat(s2_val));
在“添加项目”按钮的点击事件中执行这个操作。
英文:
You can do it this way,
Firstly grab the values from both the spinners
//From spinner 1
String s1_val = spinner1.getSelectedItemsAsString();
//From spinner 2
String s2_val = spinner2.getSelectedItemsAsString();
Concat the values and set it to the text view.
//concat and set to textView
textView.setText(s1_val.concat(s2.val()));
Perform this on the onClick of Add Items button.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论