简体   繁体   English

如何使用我的自定义 ListView 访问 EditText

[英]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.任何帮助是极大的赞赏。

You should create custom adapter which extends ArrayAdapter.您应该创建扩展 ArrayAdapter 的自定义适配器。 Take a look at this .看看这个

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.

相关问题 如果我将一个类的对象绑定到脚本引擎,那么如何从脚本引擎中将其作为该类的对象进行访问? - If I bind an object of one of my classes to a scripting engine, how can I access it as an object of that class from withing the scripting engine? 我还如何使enter将edittext中的输入文本添加到列表视图? 在创建的添加按钮之上 - How can I also make enter add input text in edittext to my listview? On top of the created add button 如何将EditText中的值添加到自定义ListView中? - How do I add values from EditText to a custom ListView? 如何在ListView适配器中缓存自定义视图? - How can I cache custom views in my ListView Adapter? 如何在片段中使用我的edittext? - How I can use my edittext in a fragment? 当要搜索的Edittext为空时,如何显示Listview元素? - How can I display Listview elements when Edittext for searching is empty? 我怎样才能使列表视图中的edittext数据进入数据库? - How can i make data from an edittext in a listview go into the database? 如何获取自定义ListView中EditText的所有值 - How get to all value of EditText in Custom ListView 如何在自定义HandlerMethodArgumentResolver中访问路径变量 - How can I access path variables in my custom HandlerMethodArgumentResolver 如何将内容保存在列表视图的edittext中 - How do I save contents in an edittext that is in a listview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM