2012-12-27 7 views
5

मेरे पास में EditText है जिसमें मैं चाहता हूं कि उपयोगकर्ता टेक्स्ट दर्ज करे, मैं डीबी स्टोर करूंगा। लेकिन, यहां मैं EditText से मान प्राप्त करने में सक्षम नहीं हूं।मैं एंड्रॉइड में EditText से मूल्य कैसे प्राप्त कर सकता हूं?

यहाँ मेरी कोड है,

EditText etUserInfoNewValue = (EditText)findViewById(R.id.etUserInfoNewVal);  
String newValue = etUserInfoNewValue.getText().toString().trim(); 

मैं इस EditText से मूल्य मिल सकता है?

+4

एक्सएमएल फ़ाइल को पहले रखें –

+3

सुनिश्चित करें कि यह etUserInfoNewVal आईडी जेनरेट हो रही है। – paritosh

+4

आपने यह कोड कहाँ लिखा था ?? – Deepzz

उत्तर

8

रखो

String newValue = etUserInfoNewValue.getText().toString().trim(); 

बटन के onClicklistener() के अंदर।

आपने इस कोड को इसे घोषित करने के बाद ऑनक्रेट() में रखा है, इसका कोई मूल्य नहीं होगा। तो आपको शून्य मूल्य मिलेगा।

0

जब आप संपादन टेक्स्ट स्ट्रिंग को डेटाबेस में संग्रहीत करेंगे। अपने कोड को देखकर ऐसा लगता है कि आप टेक्स्ट को स्ट्रिंग न्यूव्यू में तुरंत संग्रहीत कर रहे हैं और फिर इसे डेटाबेस में संग्रहीत कर रहे हैं। स्ट्रिंग घोषणा का कोड अपने बटन या किसी अन्य चीज़ पर ऑनक्लिक() पर बदलें। editText से

0

प्राप्त मूल्य, मेरे लिए

String getValueEditText = "null"; 
if (etUserInfoNewValue.getText().toString().trim().length() > 0) { 
    getValueEditText = etUserInfoNewValue.getText().toString().trim(); 
} 
4

वर्क्स ठीक, अपने मामले पर जाँच करें ..

Button buttonTest; 
EditText editText; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    buttonTest = (Button)findViewById(R.id.button); 
    editText = (EditText)findViewById(R.id.edittext); //check this point carefully on your program 

    buttonTest.setOnClickListener(
     new View.OnClickListener() 
     { 
      public void onClick(View view) 
      { 
       Log.v("EditText..", editText.getText().toString().trim()); 
      } 
     }); 
} 
2

हो सकता है आप TextView.OnEditorActionListener की जरूरत है। ब्लूटूथकैट http://developer.android.com/tools/samples/index.html

//BluetoothChat.java: 

// Initialize the compose field with a listener for the return key 
EditText mOutEditText = (EditText) findViewById(R.id.edit_text_out); 
mOutEditText.setOnEditorActionListener(mWriteListener); 

// The action listener for the EditText widget, to listen for the return key 
private TextView.OnEditorActionListener mWriteListener = 
new TextView.OnEditorActionListener() { 
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) { 
    // If the action is a key-up event on the return key, send the message 
    if (actionId == EditorInfo.IME_NULL && event.getAction() == KeyEvent.ACTION_UP) { 
     String message = view.getText().toString(); 
     sendMessage(message); 
    } 
    if(D) Log.i(TAG, "END onEditorAction"); 
    return true; 
} 

};

संबंधित मुद्दे