This question already has answers here:
How to get string from ListView?
(3 answers)
Closed 9 years ago.
I'm trying to get the string from my listView item that I click.
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, list);
I have tried accessing it using:
TextView text = (TextView) arg1.findViewById(R.id.text1);
The id of the textView of android.R.layout.simple_list_item_1 is android:id="#android:id/text1" but according to Eclipse text1 cannot be resolved or is not a field
I'm sure I'm making a mistake accessing the default android textView in the simple list item, but I'm not sure how to access it. Any ideas?
ArrayAdapter has getItem(int position), you can rely on it to retrieve the string your a looking for. How did you get arg1?
This ended up working as the solution for me. Thanks for giving me the though user2433059
TextView textView = (TextView) arg1.findViewById(android.R.id.text1);
String text = textView.getText().toString();
text1 cannot be resolved because it is an Android resource and you won't be able to access it through your R variable.
You will fix your issue using:
TextView text = (TextView) arg1.findViewById(android.R.id.text1);
String itemText = text.getText().toString();
Related
I have an Alert Dialog which consists of a ListView. In that list the first option is "Create WatchList". The remaining options in that list are all the other available Watchlist in the app. What I want is :
To Bolden the "Create Watchlist"
Change the Colour of "Create WatchList"
I have already tried 4 methods of achieving this but none of them worked. I put all of my methods in bullet form.
Storing and formatting "Create WatchList" in strings.xml and calling it from the adapter but it still doesn't work
<string name="create_watchlist"><b>Create Watchlist</b></string>
In Java
String createWatchList = context.getResources().getString(R.string.create_watchlist);
Then I add it to the List
alertList.add(0,createWatchList);
With this above method the text appears with no formatting.
Then I using fromHtml function but still I got the same results
CharSequence styledString = Html.fromHtml(createWatchList );
Then I add it to the list
alertList.add(0,styledString.toString());
Then I tried this in a text view but it seems that ListViews cannot accept
TextViews.
The Latest I have tried is this:
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
int start = 0;
spannableStringBuilder.append(createWatchlist);
spannableStringBuilder.setSpan(new ForegroundColorSpan(0xFFCC5500),start,start+createWatchlist.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableStringBuilder.setSpan(new StyleSpan(Typeface.BOLD),start,start+createWatchlist.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
alertList.add(0,spannableStringBuilder.toString());
I have tried this new method:
TextView textView = new TextView(context);
textView.setText(createWatchlist);
textView.setTypeface(textView.getTypeface(),Typeface.BOLD);
textView.setTextColor(context.getResources().getColor(R.color.lightRed));
alertList.add(0,textView);
After trying this new above method I get a message "Wrong 2nd argument type. Found: 'android.widget.TextView', required: 'java.lang.String'"
None of the above methods worked for me. Anyone out there who has any solutions to this please help!!!
You can set the style and color for the TextView inside the adapter. Just check if it is the first item and do whatever you want with the TextView.
For making it bold:
textView.setTypeface(textView.getTypeface(), Typeface.BOLD)
And for setting a color:
textView.setTextColor(getResources().getColor(R.color.yourColor));
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
In my program, I have 2 spinners: one shows a list of 3 numbers (as strings: one, two, and three), and another shows a list of countries: USA, Canada, Mexico, and Brazil. I also have a button that when tapped, goes to another activity/screen in android studio.
My question is, how should I use the variables returned from a spinner to go to another activity in android studio?
Here are the things I know:
The variables from the spinner 'exist' and can be used; I tried displaying them, and the TextView displays them. Here is the code that initializes the spinners:
//INIT SPINNER 1
Spinner spinner = findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.numbers, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
// INIT SPINNER 2
Spinner countries = findViewById(R.id.spinner2);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
countries.setAdapter(adapter2);
countries.setOnItemSelectedListener(this);
and here's the code that displays the two variables in one TextView:
TextView display = findViewById(R.id.display_spinner);
String text1 = spinner.getSelectedItem().toString();
String text2 = countries.getSelectedItem().toString();
display.setText(text1+text2);
The button, when used without any conditional statements relating to the spinners' variables, it opens a new activity. This is code for just opening a new activity by default:
//BUTTON INIT
button = (Button) findViewById(R.id.open_india);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openIndia();
}
});
Also, when using a simple conditional such as
if(4-3 == 1){
startActivity(intent);
}
the code works perfectly, and when I click the button on the emulator, I go to another activity/screen.
My question is, how do I use the variables returned from a spinner to determine whether or not to go to another activity/screen? I have the code for it, but for some reason, the button just decides not to go to the next activity.
The variables are set VARIABLE.toString(), so I know that the variable is a string, so it makes sense that it can be compared to another string.
String text2 = countries.getSelectedItem().toString();
if(text2 == "India"){
startActivity(intent);
}
Could anyone shed some light on this issue?
Change the condition to :
if(text2.equals("India")){
startActivity(intent);
}
You do not use spinner.getSelectedItem (). ToString ().
Instead of this
spinner.getSelectedItemPosition ();
spinner.getSelectedItemId ();
Use this.
public static final Int INDIA_CODE = 1;
int itemPos = countries.getSelectedItemPosition();
int itemId = countries.getSelectedItemId();
if(itemId == INDIA_CODE) {
startActivity(intent);
}
This is not a problem, but more of an efficiency question. I have multiple TextViews (2 of them) in my XML Layout of my Android App. My question is that can I select multiple TextViews, findViewById multiple TextViews on a single line?
Is this valid for my question?
TextView title, darkThemeTitle = findViewById(R.id.title); findViewById(R.id.darkThemeTitle);
When you use TextView title, darkThemeTitle = findViewById(R.id.title); findViewById(R.id.darkThemeTitle); in your code .
This line TextView title, darkThemeTitle = (TextView) findViewById(R.id.title); will show that Variable 'title' might not have been initialized .So title never initialized in the code .
And findViewById(R.id.tab_layout); will return View in your code .And it never return darkThemeTitle in your code .
And you can do like this .
TextView title = (TextView) findViewById(R.id.title); TextView darkThemeTitle = (TextView) findViewById(R.id.darkThemeTitle);
Another way
TextView title = null, darkThemeTitle = null;
TextView[] textViews = {title, darkThemeTitle};
Integer[] ids = {R.id.title, R.id.darkThemeTitle};
for (int i = 0; i < textViews.length; i++) {
textViews[i] = (TextView) findViewById(ids[i]);
}
The only recommendation is to use template ids to find views:
TextView[] themedViews = new int[NUMBER_OF_THEMES];
for (int k = 0; k < NUMBER_OF_THEMES; k++)
themedViews[k] = findViewById(context.getResources().getIdentifier("some_prefix" + String.valueOf(k), "id", packageName));
This will find all views for current activity.
Or you can use parent.findViewById to find subviews of a specified view.
I discourage you from doing this because it's more difficult for other programmers to read and doesn't save you much time typing. Use:
TextView title = findViewById(R.id.title), darkThemeTitle = findViewById(R.id.darkThemeTitle);
Have you tried using ButterKnife? This library help you with Dependency Injection so you don't need to worry about the findViewById. Just call the #BindView(view_id) and the type and name of the variable that you want to bind.
#BindView(R.id.title)
TextView title;
#BindView(R.id.darkThemeTitle)
TextView darkThemeTitle;
Remember that you need to add the dependency in your build.gradle file
compile 'com.jakewharton:butterknife:8.8.1'
And call the bind method in the onCreate of your activity
ButterKnife.bind(this);
I have a EditText, where the user will input something, a String.
And then after pressing the bottom, inverting the whole text
and showing it to the user, but, I don't know how to place the String(The inverted text) into
the TextView.
CODE:
final EditText e=(EditText)findViewById(R.id.escribiraqui); //INPUT
final TextView T=(TextView)findViewById(R.id.traduccion); //OUTPUT
Button TRAD=(Button) findViewById(R.id.traducir);//JUST THE BOTTOM
Forget about inverting the text, now I just want to output the same Text(From the EditText to the TextView).
Try this
TextView text = (TextView) findViewById(R.id.textview1);
String mText = text.getText();
text.setText(new StringBuffer(text).reverse().toString());
That's it...
If you want to invert (revert) the String and add it to a TextView you can make a call like this: myTextView.setText(new StringBuilder(enteredText).reverse().toString());
If you are newbie then first of all do Googling. This is basic Question, Just Search Like Inverting string in android:
There is solution:
return new StringBuffer(s).reverse().toString();
Place in TextView from EditText in one line:
textView.setText(new StringBuffer(editText.getText().toString()).reverse().toString());
When i display one textview it works, but when i display the second, the first textview disappears. Please help.
Here is my code
Intent intent = getIntent();
String[] data = intent.getStringArrayExtra(MainActivity.EXTRA_MESSAGE);
TextView name = new TextView(this);
name.setTextSize(25);
name.setText(data[0]+"\n");
name.setText("\n"+data[1]);
// Show text view
setContentView(name);
/* AlertDialog dialog = new AlertDialog.Builder(DisplayMessageActivity.this).create();
dialog.setTitle(name);
dialog.setMessage(message);
*/
// Show the Up button in the action bar.
setupActionBar();
Its overwrite the value on TextView.
So Use:
name.setText(data[0]+"\n"+data[1]);
Instead Of:
name.setText(data[0]+"\n");
name.setText("\n"+data[1]);
U can also use :
String dataStr="";
for(int i=0;i<data.length();i++)
{
dataStr=dataStr+"\n"+data[i];
}
name.setText(dataStr);
you must set text in one step
name.setText(data[0]+"\n"+data[1])
You are not appending text to your textview. You are actually replacing the text in textview. Before you do name.setText(); just add name.getText().toString();
For example: name.setText(name.getText().toString()+" My New data ");
Though it would be a good practice if you can use a StringBuilder for this. But above code should do the trick.
If you need to display whole array to textview,Just convert the array to string and set as text
name .setText(Arrays.toString(array));