简体   繁体   中英

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. Will post some code.

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

And my 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.
I suspect it has something to do with inflating the View.
Any help is greatly appreciated.

You should create custom adapter which extends ArrayAdapter. Take a look at this .

In your adapter where you bind the EditText to list view you can use 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 .

Now, if you want to persist your EditText data in your ListView then I would suggest a minor change in your data structure.

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 . 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.

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 . See for tutorials on how to populate a ListView from an ArrayList .

I would like to give another suggestion of using RecyclerView instead of ListView .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM