[英]How can I access an EditText withing my custom ListView
This is probably an easy answer, but I've spent far too long on this so I could really use some help.这可能是一个简单的答案,但我在这方面花了太长时间,所以我真的可以使用一些帮助。
I am simply trying to get the value stored in my EditText.我只是想获取存储在我的 EditText 中的值。 Will post some code.将发布一些代码。
InvoiceCreator.java发票创建器.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_invoice_creator);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.inflateMenu(R.menu.menu_invoice_creator);
myDb = new DatabaseHelper(this);
EditText ItemCost;
Bundle NameIntentData1 = getIntent().getExtras();
if (NameIntentData1==null){
return;
}
String IntentDataID1 = NameIntentData1.getString("Client ID1");
String IntentDataName1 = NameIntentData1.getString("Client Name");
String IntentDataAddress1 = NameIntentData1.getString("Client Address");
final TextView IDBar1 = (TextView) findViewById(R.id.InvoiceCreatorIDTV);
final EditText Namebar1 = (EditText) findViewById(R.id.InvoiceCreatorNameET);
final EditText AddressBar1 = (EditText) findViewById(R.id.InvoiceCreatorAddressET);
IDBar1.setText(IntentDataID1);
//IDBar1.setText(IntentDataName1);
//AddressBar1.setText(IntentDataAddress1);
Spinner spinMonths1 = (Spinner) findViewById(R.id.InvoiceCreatorMonthSpinner);
ArrayAdapter<CharSequence> months = ArrayAdapter.createFromResource(this, R.array.Months, android.R.layout.simple_spinner_dropdown_item);
months.setDropDownViewResource(android.R.layout.simple_list_item_1);
spinMonths1.setAdapter(months);
Spinner spinYear1 = (Spinner) findViewById(R.id.InvoiceCreatorYearSpinner);
ArrayAdapter<CharSequence> years = ArrayAdapter.createFromResource(this, R.array.Years, android.R.layout.simple_spinner_dropdown_item);
years.setDropDownViewResource(android.R.layout.simple_list_item_1);
spinYear1.setAdapter(years);
String[] foods = {"Service: Bag \'n\' Cut", "Service: Mulch Thrown", "Service: Grass Seed",
"Service: Spring Decoration", "Service: Snow Plow" };
//S:Bag//S.Mul//S.Gra//S.Spr//S.Snw
String[] costs = {"150", "150", "150", "150", "150"};
ListAdapter adapterservices = new custom_ServiceAdapter(this, foods);
ListView servicelist = (ListView) findViewById(R.id.InvoiceCreatorMonthHistoryListView);
servicelist.setAdapter(adapterservices);
And my custom_row_layout
还有我的custom_row_layout
<TextView
android:id="@+id/customName"
android:layout_width="210dp"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
android:clickable="false"
android:text="TextView"
android:textAlignment="center"
android:textColor="@android:color/black"
android:textSize="24sp"
android:textStyle="bold" />
<EditText
android:id="@+id/Cost"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/CheckService"
android:layout_alignParentEnd="true"
android:layout_weight="1"
android:editable="false"
android:ems="10"
android:hint="Cost"
android:inputType="none"
android:textSize="24sp" />
<CheckBox
android:id="@+id/CheckService"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/customName"
android:checked="false"
android:clickable="false"
/>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toStartOf="@+id/Cost"
android:text="$"
android:textColor="@android:color/background_dark"
android:textSize="40sp" />
I am simply aiming to extract the value of EditText
cost.我只是想提取EditText
成本的价值。
I suspect it has something to do with inflating the View.我怀疑这与膨胀视图有关。
Any help is greatly appreciated.任何帮助是极大的赞赏。
In your adapter where you bind the EditText to list view you can use EditText.getText().toString();在您将 EditText 绑定到列表视图的适配器中,您可以使用 EditText.getText().toString();
Or you can use custom interface to handle input enter by user或者您可以使用自定义界面来处理用户输入的输入
So as far as I've understood your question, you've a list of foods and you need to get the food costs from the EditText
s which are in each of your row of the ListView
.因此,据我了解您的问题,您有一份食物清单,您需要从ListView
每一行中的EditText
获取食物成本。
Now, if you want to persist your EditText
data in your ListView
then I would suggest a minor change in your data structure.现在,如果您想将EditText
数据保留在ListView
那么我建议您对数据结构进行细微更改。
You're taking an array of Strings which is foods and another array for costs.您正在获取一组字符串,它是食物,另一个数组是成本。 I would like to propose you make an object containing both and populate the array of objects in your ListView
.我想建议您制作一个包含两者的对象并填充ListView
的对象数组。 So your object might look like this.所以你的对象可能看起来像这样。
public class Food {
public String name;
public String cost;
}
Now get an ArrayList
of your foods and populate the ArrayList
as you want.现在得到ArrayList
你的食物和填充ArrayList
,只要你想。
public ArrayList<Food> generateFoods() {
ArrayList<Food> foodList = new ArrayList<Food>();
String[] foods = {"Service: Bag \'n\' Cut", "Service: Mulch Thrown", "Service: Grass Seed", "Service: Spring Decoration", "Service: Snow Plow" };
String[] costs = {"150", "150", "150", "150", "150"};
for (int i = 0; i < foods.length; i++) {
Food food = new Food();
food.name = foods[i];
food.cost = costs[i];
foodList.add(food);
}
return foodList;
}
Now pass the ArrayList
to your adapter and populate the items in your ListView
.现在将ArrayList
传递给您的适配器并填充ListView
的项目。 See for tutorials on how to populate a ListView
from an ArrayList
.有关如何从ArrayList
填充ListView
教程,请参阅 。
I would like to give another suggestion of using RecyclerView
instead of ListView
.我想给出另一个使用RecyclerView
而不是ListView
建议。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.