I have the following MainActivity.java file currently running on Eclipse.
My intention is to get the user input and the spinner value and form a url like www.ex.com?title=(user_inpu)&title_type=(spinner_input).
As of now I am hung on this part. How do I get the text value from EditText and Spinner?
I have tried writing this code:
package com.example.myfirstapp;
import org.apache.commons.logging.Log;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
public class MainActivity extends Activity {
Button btn;
EditText edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
edit = (EditText) findViewById(R.id.edit_message);
btn.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
Log.v("EditText", edit.getText().toString());
}
});
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.title_type,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Object item = parent.getItemAtPosition(pos);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
I am getting a small error:
The method v(string, String) is undefined for MainActivity.java.
I have tried replacing the Log.v("EditText", edit.getText().toString()); to Log.v("EditText", MainActivity.this.edit.getText().toString());, but the error still persists.
How do I get rid of that? And in general, will this code work?
You’re not importing the right
Logclass.Just replace
by
And you probably want to make your
spinnera member of the activity (just like the button and the text field) so that you can access it from any listener.I mean: