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

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);

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 make a table to display records from a SQLite database?

I got a simple question. I am trying to create some kind of layout that would display my records from SQLite Database in separate cells. I tried to make it using TextViews, but it is not what I expect. Do you know any ideas to create 'excel like' table with headers etc? What is more i need each row to be able to select and open (My goal is to have something like catalogue with products).
final TextView dTable= (TextView) findViewById(R.id.displayTable);
final ManageDatabase md = new ManageDatabase(this);
dTable.setText("");
for(Product p: md.takeAllProducts()){
dTable.setText(dTable.getText()+"\n"+" "+p.getProductCode()+" "+p.getProductName()+" "+p.getQuantity()+" "+p.getExpireDate());
}
You can use TableLayout for it and dynamically add your content there.
First get the reference of table layout from XML file.
TableLayout myTable = (TableLayout) findViewById(R.id.mTable);
Then you can add retrieved data to the table dynamically like below
for(Product p: md.takeAllProducts()){
//create new row
TableRow tableRow = new TableRow(this);
// create new text view
TextView textView1 = new TextView(this);
textView1.setText(p.getProductCode());
//add to row
tableRow.addView(textView1);
//create another text view and add to same row
TextView textView2 = new TextView(this);
textView2.setText(p.getProductName());
tableRow.addView(textView2);
//
TextView textView3 = new TextView(this);
textView3.setText(p.getQuantity());
tableRow.addView(textView3);
//
TextView textView4 = new TextView(this);
textView4.setText(p.getExpireDate());
tableRow.addView(textView4);
}

Two extra lines added to edit text when retrieving html String

I save my formatted edit text in sqlite database in html then after retrieving it two extra lines are added to the edit text
So I started like so:-
edt.setText(Html.fromHtml("<b>"+myString+"</b>"));//making it bold
Editable e = edt.getText();//convert to Editable
String text = Html.toHtml(e);//convert to String
Then after I inserted text into the database .I retrieved it back to edt this way:-
Spanned sp = Html.fromHtml(text);//convert text to spanned
edt.setText(sp);//setting to the edittext
It was retrieved successfully however 2 extra lines where added to edt at the end of text each time why you think that occurs?.
try this
edt.setText(Html.fromHtml("<b>"+myString+"</b>"));//making it bold
Editable e = edt.getText();//convert to Editable
/* String text = Html.toHtml(e);//convert to String
Spanned sp = Html.fromHtml(text); //convert text to spanned */
edt.setText(e);//setting to the edittext
I finally found a solution using code from an answer in this question Remove extra line breaks after Html.fromHtml()
.Its all about removing extra html whitspaces but using it for spannable
String like so :-
Spanned sp = Html.fromHtml(subnote);
int h = sp.length();
// loop back to the first non-whitespace character
while(--h >= 0 && Character.isWhitespace(sp.charAt(h))) {
}
sp= (Spanned) sp.subSequence(0, h+1);
edt.setText(sp);

Spinner from database - get value

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();

Android html.form html

I'd like to read html tags to a TextView, so I have done this:
titolo = (TextView) this.findViewById(R.articolo.titolo);
testo = (TextView) this.findViewById(R.articolo.testo);
titolo.setText(db.getTitolo());
testo.setText(db.getTesto());
testo.setText(Html.fromHtml(testo));
But i have an errore here: testo.setText(Html.fromHtml(testo)); Why?
This application retrieves data from a database so I hope that if I write into the database, for example hello this is formatted as bold using html.fromhtml
public static Spanned fromHtml (String source)
Returns displayable styled text from the provided HTML string. Any tags in the HTML will display as a generic replacement image which your program can then go through and replace with real images.
This uses TagSoup to handle real HTML, including all of the brokenness found in the wild.
More info #
http://developer.android.com/reference/android/text/Html.html
This
testo = (TextView) this.findViewById(R.articolo.testo); // textview initialized
This
testo.setText(Html.fromHtml(testo)); // wrong
fromHtml takes a string as a argument
It should be
testo.setText(Html.fromHtml("myhtmlString"));
Example :
String s ="<b>"+"Hello"+"</b>";
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(Html.fromHtml(s));
In your example you're sending TextView to fromHtml and you should deliver String variable. That String could contain HTML tags.
TextView testo = (TextView) findViewById(R.articolo.testo);
String formattedText = "This is <b>bold</b>";
testo.setText(Html.fromHtml(formattedText));
Of course you could get String from DB. I don't know how works your getTesto() method, but if it returns String you could write:
TextView testo = (TextView) findViewById(R.articolo.testo);
String formattedText = db.getTesto();
testo.setText(Html.fromHtml(formattedText));

Categories