Spinner from database - get value - java

I am learning Android and I use this tutorial: http://www.androidhive.info/2012/06/android-populating-spinner-data-from-sqlite-database/ to generate Spinner with data from database.
This working ok, but i can't get selected item.
final Spinner spinner = (Spinner) findViewById(R.id.spinner); //this is ok
and next i would like get selected item:
Log.i("test", spinner.getSelectedItem().toString());
this return me:
+++ LOG: entry corrupt or truncated
EDIT:
I try also:
Log.i("test", spinner.getSelectedView().toString());
but this return:
android.widget.TextView#410a2ae8

Try the following:
TextView textView = (TextView)spinner.getSelectedView();
String result = textView.getText().toString();

Related

How to overwrite a text in TextView using a button?

I'm working on an app that generates passwords randomly using the array. The password is in TextView. Everything is good unless I want to generate a new password second time. How can I "delete" the old text (password) from the TextView and replace it with the new one using the same button?
Here are the variables that I'm using:
EditText dlugosc;
String haslo = "";
String pustak = "";
TextView haslo0;
And this is a code that I use to generate a password:
(znaki is the name of array)
dlugosc = findViewById(R.id.password_len);
haslo0 = findViewById(R.id.password);
String yui = dlugosc.getText().toString();
int x = Integer.parseInt(yui);
for(int i = 0; i < x; i++){
int Index = generator.nextInt(znaki.length);
haslo = znaki[Index] + haslo;
}
I have already tried doing an if structure:
if (haslo0 != null){
haslo0.setText(pustak);
haslo0.setText(haslo);
}
else
haslo0.setText(haslo);
But it doesn't help :(
When I want to have 7 chars in the password and click the button first time, the result is correct e.g. PKAjzQL. But when I click the button second time, the result is nBzcRjQPKAjzQL instead of nBzcRjQ.
Why are you appending the old string haslo behind the newly generated one in haslo = znaki[Index] + haslo;
Probably that's why you are getting output like that. Can you please try just setting newly generated password into the textview like
haslo = znaki[Index];
And then try to set text in the text view using haslo0.setText(haslo);
How can I "delete" the old text (password) from the TextView and replace it with the new one using the same button?
The problem is not to "delete" the old text, the problem is that you have to clear the list for example, every time user clicks on the Button you clear the list doing : znaki.clear(), then it will only show the new password generated.
If you see your output :
First output :
PKAjzQL --> This is correct
Second output :
nBzcRjQPKAjzQL --> this is the new output + the old one
Can you give the code of the OnClickButton? And why are you setting the same TextView with diferents Strings when you click?
haslo0.setText(pustak);
haslo0.setText(haslo);
?

How to add the text in String passed from another activity and show the resulting text in Textview

I am passing value from Activity A to Activity B. It works fine.
For Example, if I pass String "Harvey Brown", Then in next activity it also shows "Harvey Brown".
But I would also like to add "Author: " text before "Harvey Brown" and show the sum result in TextView such that now instead of only
"Harvey Brown".
It will show
Author:Harvey Brown
for every intent.
Here is the code, I'm using to pass String value:
intent.putExtra("Title",mData.get(position).getTitle());
and in next activity I received value by
Intent intent = getIntent();
String author_name = intent.getExtras().getString("author_name");
and I set the value accordingly as
author_name = (TextView) findViewById(R.id.author_nameid);
author_name.setText(author_name);
Solution:
Just change this line:
intent.putExtra("Title",mData.get(position).getTitle());
To
intent.putExtra("Title", "Author: " + mData.get(position).getTitle());
You will get your desired result.
You can add a 'Author:' Prefix like below -
author_name = (TextView) findViewById(R.id.author_nameid);
author_name.setText("Author: " + author_name);

Working with NavigationView Xamarin

Hi I have problem with NavigationView . I use this code :
nav = FindViewById<NavigationView>(Resource.Id.nav_view);
View vie = nav.GetHeaderView(0);
TextView title1 = vie.FindViewById<TextView>(Resource.Id.title1);
TextView title2 = vie.FindViewById<TextView>(Resource.Id.title2);
ImageView logo_menu = vie.FindViewById<ImageView>(Resource.Id.imageView1);
title1.Text = MenuLayout.text1;
title1.TextSize = MenuLayout.text1_size;
title1.SetTextColor(MenuLayout.text1_color);
title2.Text = MenuLayout.text2;
title2.TextSize = MenuLayout.text2_size;
title2.SetTextColor(MenuLayout.text2_color);
logo_menu.SetImageBitmap(MenuLayout.menu_Image);
vie.SetBackgroundColor(Android.Graphics.Color.Black);
The last line not working , nothing change ... How Can I set background of header Programmatically ? My secend problem is change text and icon items of Navmenu (Can I change when program work ? How override this)

how to make textView clickable and collect information about clicked data?

i had data set which are retrieve from sqlite database and the data will display by using textView.
this is code
ContactDatabase onbOfContactDatabase=new ContactDatabase(getBaseContext());
Cursor allcontact= onbOfContactDatabase.showData();
TextView t=(TextView)findViewById(R.id.textView);
String dbString="";
allcontact.moveToFirst();
do {
dbString+=allcontact.getString(allcontact.getColumnIndex("name"));
dbString+="\n";
t.setText(dbString);
} while(allcontact.moveToNext());
The output of this code like
new text
data1
data2
data3
data4
data5
i want all data make clickable and how to know who data is clicked?
plese help this is argent.
Change the TextView to a ListView then set onClickListener for the ListView, then with that set
String output = listview.getselecteditem(position);

findViewById with getIdentifier not working

I'm looping through an array, in that for-loop I'm also using this array to get IDs for a couple of spinners:
Integer[] ids;
ids = new Integer[8];
ids[0] = R.id.nr_area;
ids[1] = R.id.nr_municipality;
ids[2] = R.id.nr_method;
ids[3] = R.id.nr_bait;
ids[4] = R.id.nr_sessionhours;
ids[5] = R.id.nr_bifangst_art;
ids[6] = R.id.nr_bifangst_released;
ids[7] = R.id.nr_name;
In the for loop:
for (int i = 0; i < fields.length; i++) {
if(fields[i].contains("*") || fields[i].matches("")){
String test = ids[i].toString();
int resID = getResources().getIdentifier(test, "id", getPackageName());
Spinner spnr = (Spinner) findViewById(resID); //error is on this row
spnr.setBackgroundColor(Color.GREEN);
}
}
Now the error I get is as follows:
android.support.v7.internal.widget.TintEditText cannot be cast to android.widget.Spinner
So I guess this can't be used with a spinner as I found the example code for a EditText.. but how can I do what I'm trying to do with a spinner?
I would appreciate any help! Been fighting with this for hours. Thanks alot..
I think the problem is that either one of the ids is of EditText or id was of EditText and you have changed it to Spinner in your xml lately. Make sure that ids are correct, relate them from xml, if still problem persists, try cleaning your project for indexing again, that should do it.
Hope this helps ...

Categories