无法获取变量以在edittext表单中显示值

时间:2014-03-13 19:23:45

标签: android

我正在处理一个应用程序,该应用程序的主要布局包含一行按钮,该按钮位于LinearLayout顶部,其中包含一个由edittexts组成的表单。我创建了一个Form对象来保存要在每个表单的edittexts中显示的所有值。单击每个按钮可更改表单的数据,以显示每个Form对象中保存的值。我将Form对象保存在List中,以便在按下相应按钮时可以访问每个对象。

编辑:我意识到我并没有很好地解释自己。当我单击其中一个按钮时,我希望EditText值更改为与该按钮对应的另一组值。但是当我单击这些按钮时,先前输入的值不会显示。我在EditTexts列中提到了一个表单,但它只是EditTexts的一列。

当我单击Next Drink时,它会动态创建一个按钮以及一个Form对象,并清除EditTexts,使其看起来像是为新按钮创建了一个新表单。然后我应该能够在EditTexts中添加字符串,并且字符串应该存储在Form对象中,这样如果我再次单击该按钮,Form对象将使用存储在该对象中的字符串填充EditTexts。

因此,如果在单击按钮2后单击按钮3,则应将每个EditText的文本设置为第三个Form对象中包含的值,其中包含Name,Volume,Percentage,Price和Quantity的字符串。

我的问题是单击按钮后,EditTexts中没有显示值。因此,如果我单击按钮3,则表单3中的值不会显示。我已经在这里呆了很长时间,无法弄清楚原因,所以我非常感谢任何帮助。这是活动:

package com.example.DrinkApp;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import java.util.ArrayList;
import java.util.List;

public class MyActivity extends Activity {
/**
 * Called when the activity is first created.
 */




//view variables
final Button newDrink[] = new Button[100];
LinearLayout drinkSelectionContainer;
EditText name;
EditText vol;
EditText perc;
EditText pri;
AutoCompleteTextView quant;

//counters
int numOfDrinks = 0;
int selectedForm = 0;


//array to hold all Form objects.
List<Form> formList = new ArrayList<Form>();



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    //get views
    drinkSelectionContainer = (LinearLayout) findViewById(R.id.drinkSelectContainer);
    name = (EditText) findViewById(R.id.etName);
    vol = (EditText) findViewById(R.id.etVol);
    perc = (EditText) findViewById(R.id.etPerc);
    pri = (EditText) findViewById(R.id.etPrice);
    quant = (AutoCompleteTextView) findViewById(R.id.etQuantity);
    Button saveDrink = (Button) findViewById(R.id.bSaveForm);

    Button nextDrink = (Button) findViewById(R.id.bNextDrink);
    Button crunkOut = (Button) findViewById(R.id.bCalc);

    ///////////////////////// next drink method
    addNewDrinkButton();



    name.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            formAdd();

        }

        @Override
        public void afterTextChanged(Editable editable) {

//                mNames.add(selectedForm, name.getText().toString());
//                newDrink[selectedForm].setText(name.getText().toString());

        }
    });
    nextDrink.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            addNewDrinkButton();
        }

    });

}






//listener for drink select buttons. when button is clicked, it finds out which one it was,
//and then displays the correct values.
public View.OnClickListener drinkSelectListener = new View.OnClickListener() {
    public void onClick(View v) {

        selectedForm = v.getId();
        Form form = formList.get(selectedForm);
        Button v1 = (Button) findViewById(selectedForm);
        v1.setText(" " + selectedForm);
        // Do something depending on the value of the tag
        Log.v("My Logs:", "Select Drink " + selectedForm + " button pressed.");
        Log.v("My Logs:", "Drink " + selectedForm + " name: " + form.getName());


        formDisplay(form);


    }
};


private void addNewDrinkButton() {
    formClear();
    formAdd();
    numOfDrinks++;

    selectedForm = numOfDrinks;
    Log.v("My Logs:", "Next Drink button pressed.");


    newDrink[numOfDrinks] = new Button(getBaseContext());
    newDrink[numOfDrinks].setOnClickListener(drinkSelectListener);
    newDrink[numOfDrinks].setId(numOfDrinks);


    newDrink[numOfDrinks].setText("" + selectedForm);
    newDrink[numOfDrinks].setLayoutParams(new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));

    // name.setText("Drink " + (numOfDrinks+1));

    drinkSelectionContainer.addView(newDrink[numOfDrinks]);



    //TODO: save form values, clear edit texts, and create new variables for edittexts.    
}


void formAdd(){

    Form tempForm = new Form();

    tempForm.setId(selectedForm);
    tempForm.setName(name.getText().toString());
    tempForm.setVolume(vol.getText().toString());
    tempForm.setPercentage(perc.getText().toString());
    tempForm.setPrice(pri.getText().toString());
    tempForm.setQuantity(quant.getText().toString());

    formList.add(selectedForm, tempForm);

}

void formDisplay(Form form){

    name.setText(form.getName());
    vol.setText(form.getVolume());
    perc.setText(form.getPercentage());
    pri.setText(form.getPrice());
    quant.setText(form.getQuantity());


}


void formClear(){

    name.setText("");
    vol.setText("");
    perc.setText("");
    pri.setText("");
    quant.setText("");


    }
} 

Here's the main layout. So if I click button 4, I want to have the EditTexts display the values I'd entered the last time I'd clicked on it. It's really very simple, I'm having trouble explaining myself today.

1 个答案:

答案 0 :(得分:1)

提供的代码似乎与您的问题不符。您希望将值放入EditText视图,然后点击一个按钮,该按钮从EditText视图中获取值并将其放在&#34;表单中#34;?如果是这样,那么在onCreate()内部和setContentView()之后尝试:

EditText name = (EditText) findViewById(R.id."edit_text_id");  // where "edit_text_id" is your your EditText view id set in your xml
String text = name.getText().toSting();

TextView textView = (TextView) findViewById(R.id."forms_text_field"); // where "forms_text_field" is your TextView id set in your form's xml
textView.setText(text);

对每个EditText视图重复此操作(将表单中的相应TextView设置为所需的EditText字段值)。这应该可以正常工作。