Getting text from a spinner in android studios - java

I have a spinner with 2 selections, "text1" and "text2". When I tried to get "text1" and log "text1" to see if I got it or not, I got this instead: "androidx.appcompat.widget.AppCompatSpinner{d963757 VFED..CL. ........ 389,1177-883,1230 #7f080094 app:id/spinner1}".
What am I doing wrong?
String getSpinnerName = "spinner" + "1";
Spinner spinner = (Spinner) findViewById(getResources().getIdentifier(getSpinnerName, "id", getPackageName()));
final String spinnerText = spinner.toString();
Log.d(String.valueOf(LOG), spinnerText);

you are converting an object into String that's why.
final String spinnerText = spinner.toString();
if you want to get the values from spinner use below one.
final String spinnerText = spinner.getSelectedItem().toString();

Related

Trying to pull plain text data to use in a service

I'm creating an app for an android mobile computer that can take the data scanned and convert a specified character to another. The way it currently works, I manually code in what characters to look for and convert to:
public String specialWorkFor(String originalData, String codeType ){
String[] targetStr = {"1", "2", "3"};
String[] replaceStr = {"a","b","c"};
String newData = "";
newData = originalData;
newData = ReplaceText(originalData,targetStr,replaceStr);
return newData;
}
private static String ReplaceText (String originalData, String[] targetStr, String[] replaceStr ){
String newData = "";
String newDataTmp = "";
newData = originalData;
for (int i = 0; i < targetStr.length; i++){
newDataTmp = newData.replace(targetStr[i], replaceStr[i]);
newData = newDataTmp;
}
return newData;
}
This works fine, but ideally I'd like to have an interface where I can just type into a plain text field and use the values from there to determine what characters get converted.
After creating the layout, I've tried doing this:
//Inputs
Context context1 = getApplicationContext();
Activity act1=(Activity)context1;
EditText codein = (EditText) act1.findViewById(R.id.input);
String in = codein.getText().toString();
//Outputs
Context context2 = getApplicationContext();
Activity act2=(Activity)context2;
EditText codeout = (EditText) act2.findViewById(R.id.output);
String out = codeout.getText().toString();
public String specialWorkFor(String originalData, String codeType ){
//String[] targetStr = {in};
//String[] replaceStr = {out};
String newData = "";
newData = originalData;
newData = ReplaceText(originalData,targetStr,replaceStr);
return newData;
}
Where I use:
Context context1 = getApplicationContext();
Activity act1=(Activity)context1;
EditText codein = (EditText) act1.findViewById(R.id.input);
String in = codein.getText().toString();
to pull values from the activity where I enter in my values. Issue is when I scan I get the error:
Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
I am unsure where to go from here. Any thoughts?
You cannot cast application context to an activity.
Create class Storage like this:
public class Storage {
public static final Storage instance = new Storage();
public String codeIn = "";
public String codeOut = "";
}
Add a textWatcher to both edittexts, and inside ontextchanged add :
Storage.instance.codeIn = whatever is in coresponding edittext
Inside the service you can get those values the same way:
String in = Storage.instance.codeIn
Also note that those values mast be set before the service uses them)))

Inserting a value into a URL in android studio?

I sent data from Activity A to Activity B through means of an intent.
I then extracted the results of the intent via the following.
String IdString = questionData.getString("id");
TextView mstudentId = (TextView) findViewById(R.id.ID);
mstudentId.setText(IdString);
I would now like to take the value of IdString which is equal to the value of the string "id" and place it inside a Url. For example,
final static String URL_ANSWER = "http//:myFake/(IdString)/Url"
How do I go about doing this in Android Studio?
Like this,
final static String URL_ANSWER = "http//:myFake/" + IdString + "/Url";
OR
UPDATE
String IdString = questionData.getString("id");
TextView mstudentId = (TextView) findViewById(R.id.ID);
mstudentId.setText(IdString);
final static String URL_ANSWER = "http//:myFake/" + mstudentId.getText() + "/Url";

Taking all items from listview to textview on click

im a beginner in android/java programming and im trying to retrieve the element from a listview and put them in a textview.I know you can set text to a textview using settext but I dont know how to retrieve element from my listview(they are all strings) and put them in my textview
thank you
StringBuffer allItems = new StringBuffer();
Adapter adapter = listView.getAdapter();
for (int i=0; i<adapter.getCount(); i++) {
Object item = adapter.getItem(i);
allItems.append(item.toString());
allItems.append(", ");
}
textView.setText(allItems.toString());

Use SharedPreference to define drawable resources

I have a few drawables:
R.drawable.pres01
R.drawable.pres02
R.drawable.pres03
I want to use SharedPreference to display the proper drawable:
private SharedPreferences prefs;
private SharedPreferences.Editor editor;
String someId;
ImageView ivPresident;
int inAdd;
prefs = this.getSharedPreferences("pNum", Context.MODE_PRIVATE);
someId = prefs.getString("presNum", "");
inAdd = Integer.parseInt(someId);
ivPresident = (ImageView) findViewById(R.id.imgViewPresident);
I converted the String to Integer, now how do I set the image source of ivPresident to the number based on the saved string.
So for example:
if someId is 01 the drawable for ivPresident is R.drawable.pres01
if someId is 02 the drawable for ivPresident is R.drawable.pres02
if someId is 03 the drawable for ivPresident is R.drawable.pres03
I tried the following but did not work:
ivPresident.setBackground(R.drawable.pres + inAdd);
How do I fix the code to achieve it?
What you want is:
Resources.getIdentifier(String name...)
See here
Build the string the way you are trying now.
EDIT:
String someId = prefs.getString("presNum", "");
int id = getResources().getIdentifier("pres0" + someId, "drawable", getPackageName())
ivPresident.setBackgroundResource(id);

Custom Autocomplete Adapter Android

I wish to have a auto-complete text-box which comes up with the users contact names. My code is as follows.
private void getContactNames()
{
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);
_contactAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line);
while (cursor.moveToNext())
{
int nameIdx = cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
String tmp = cursor.getString(nameIdx);
_contactAdapter.add(tmp);
}
}
Setting the adapter:
AutoCompleteTextView contactName = (AutoCompleteTextView) findViewById(R.id.contactName);
contactName.setAdapter(_contactAdapter);
When I do this, the adapter has all the contact names in there (238 contacts). However, when I start typing into the text box, the auto complete does not show.
Funny, as when I test it out doing this:
String[] ab = new String[] {"aaaaa", "bbbbb"};
_contactAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,ab);
it will show "aaaaa" and "bbbbb" when typing in to the text box.
Any ideas?
Thanks,
Tom
*EDIT
Just thought I would follow up. It does seem to be the sheer amount of contacts that is preventing it from appearing. Any way to get around this?
while (cursor.moveToNext())
{
int nameIdx = cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
String tmp = cursor.getString(nameIdx);
//_contactAdapter.add(tmp);
// get all names in a new arraylist and then assign it to
arrayList.add(tmp);
}
and then assign it to your adapter as
_contactAdapter = new ArrayAdapter<String> this,android.R.layout.simple_dropdown_item_1line, arrayList);

Categories

Resources