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

时间:2017-09-30 07:58:44

标签: java android android-sqlite

这可能是一个简单的答案,但我花了太长时间才这样做,所以我真的可以使用一些帮助 我只是想将值存储在EditText中。会发布一些代码。

InvoiceCreator.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);

我的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" />

我的目标只是提取EditText费用的价值 我怀疑它与膨胀视图有关 非常感谢任何帮助。

3 个答案:

答案 0 :(得分:0)

您应该创建扩展ArrayAdapter的自定义适配器。看看this

答案 1 :(得分:0)

在将EditText绑定到列表视图的适配器中,您可以使用EditText.getText()。toString();

或者您可以使用自定义界面来处理用户输入的输入

答案 2 :(得分:0)

因此,只要我理解了您的问题,您就可以获得食物清单,并且需要从EditText中获取食物费用。 ListView

现在,如果您想在EditText中保留ListView数据,那么我建议您对数据结构进行微小更改。

你正在服用一系列字符串作为食物和另一个成本阵列。我想建议您创建一个包含两者的对象,并填充ListView中的对象数组。所以你的对象可能看起来像这样。

public class Food {
    public String name; 
    public String cost;
}

现在获取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;
}

现在将ArrayList传递给适配器并填充ListView中的项目。请参阅有关如何从ListView填充ArrayList的教程。

我想提出另一个使用RecyclerView而不是ListView的建议。