setText() into a textview from a database - java

I am new to Android App Development. I am try to view my database using a Textview in my activity.
Here is my setText() java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viewlogs);
TextView tv = (TextView) findViewById(R.id.tvSqlinfo);
LogsDB info = new LogsDB(this);
info.open();
ArrayList<String> data = info.getData();
info.close();
tv.setText(data);
}
I seem to be getting and error at tv.setText(data);. Stating "The method setText(CharSequence) in the type TextView is not applicable for the arguments (ArrayList)" Then when i do the recommended fix it changes
tv.setText(data)
to
tv.setText((CharSequence) data);
Then when I test the application I get an error stating that it cannot be cast.
What do I need to change to be able to view my database in the textview?
Any advice and help would be greatly appreciated.
Thanks

If you want to keep it simple you can use
tv.setText(data.toString());
instead of
tv.setText(data);
It will show something like this:
([field1],[field2],...)

You probably want to take each String out of the ArrayList and add them to a single String object then add that to your TextView. Something like
String text = "These are my database Strings ";
for (int i=0; i<data.size(), i++)
{
text = text.concat(data.get(i)); // might want to add a space, ",", or some other separator
}
tv.setText(text);
and you can separate the Strings however you want them to be displayed.

Related

how to feed input node (which is a sentence) in android studio using TensorFlowInferenceInterface

If my question is unclear, please let me know, I am quite new in this area.
I did the following:
I freezed and optimized tensorflow model
It is a RNN lstm model , getting a sentence then saying if that positive or negative
I successfully added all the things in android studio
Now I have a editbox to enter my sentence , then touch Run button, then this run button should fill the label if this sentence was positive or negative,
Would you please give me some instructions that if there is a predefined function I should use,
or I have to write down that by myself, if this is the case, how,
Thanks in advance for helping.
Update
This is something I have tried but it seems quite incorrect:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inferenceInterface = new TensorFlowInferenceInterface();
inferenceInterface.initializeTensorFlow(getAssets(), MODEL_FILE);
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final EditText editNum1 = (EditText) findViewById(R.id.editNum1);
inferenceInterface.fillNodeInt(INPUT_NODE, INPUT_SIZE, editNum1);
inferenceInterface.runInference(new String[] {OUTPUT_NODE});
int[] resu = {0, 0};
inferenceInterface.readNodeInt(OUTPUT_NODE, resu);
final TextView textViewR = (TextView) findViewById(R.id.txtViewResult);
textViewR.setText(Float.toString(resu[0]) + ", " + Float.toString(resu[1]));
}
});
}
Edit2
there is FillNodeInt in TensorFlowInferenceInterface,
what I thought is that, it is going to tie my input in the editbox to the input node in my model,
so the first parameter is input node, the second is the size, and the third should be the input for example I fill in the text box,
but there is no function to have the third parameter as String,
they are int or float or byte(like example above which is Int), or something like buffer
can someone please explain which method I can use

How to give Format to a text in a String

I am working on a Android App on Android Studio. The app will show several phrases randomly when a button is pressed.
I already have a simple script for that, using a String variable which contains all the possible texts.
However, I want each text to appear as follows:
"text 1
- Text1 a"
But I am not able to add the break on the string variable I created.
I currently have it as:
final TextView textOne = (TextView) findViewById(R.id.textView);
Button motivateYou = (Button) findViewById(R.id.button);
final String[] misFrases = {"Texto 1", "texto 2", "Texto 3", "Texto 4"};
motivateYou.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Random randGen = new Random();
final int rando = randGen.nextInt(4);
textOne.setText(misFrases[rando]);
}
});
Is this a good way to create the list to add the format?
Or should I add the format on the String file on the res folder and then call the string from there?
As a side comment, I am planning to have lists of 100+ texts
Let me know your comments
What you want to do is this:
myTextView.setText(Html.fromHtml("Text1<br/>next line"));
Use Html formatted strings inside of textviews. This should cover most of usecases.
If you want a lot of strings inside of your app, I'd recommnd using an array inside arrays.xml
If you want to port the app to another language, it makes it a lot easier, if you have all strings in one place.
Furthermore you shorten your code inside the Class, since you will have over hundred strings in those lists.

How Can I setText in the EditText without knowing id?

I'd like use the setText() function for an EditText without knowing the id,
so if there are three EditText in an application, the command setText() should change the text on all three and I won't know the ids.
I was planning to use this command:
EditText ed = (EditText) findView (something);
I don't think it is that simple. Is there a way to accomplish this?
You could put the TextViews alone inside a ViewGroup (i.e. LinearLayout) and iterate along their children like this.-
for (int i = 0; i < containerView.getChildCount(); i ++) {
View view = containerView.getChildAt(i);
if (view instanceof TextView) {
TextView textView = (TextView) view;
textView.setText(yourText);
}
}

unable to get text of other textview in android?

I am having a dynamic table in which I have created lots of textview dynamically... actually I having one row in which I have 3 textview... in this one of them contains unique id but other's textview value repeats around the table... therefore on the click of other textview I need text from unique textview.... so please suggest some thing like getting text from the neighbor textview... line through which I am able to get text from the textview is following.
((TextView)v).getText().toString();
When creating dynamic TextViews assign a reference to the unique TextView as a tag using setTag to each of them.
TextView uniqueTextView = (TextView)findViewById(R.id.unique_id);
TextView neighbourView = new TextView();
neighbourView.setTag(uniqueTextView);
You can later get the reference back using getTag
public void onTextViewClick(TextView view) {
TextView uniqueTextView = (TextView)view.getTag();
String text = uniqueTextView.getText().toString();
}

Clone textview to append it to a ViewGroup

I have a ViewGroup defined in XML with a view inside, at onCreate time I'd like to have a variable of those.
I don't want to go through the hassle of using a listview+adapter cause its clearly overkill as I know the list won't change since onCreate()
This is more or less the code I'd like to have.
TextView mytextview = myViewGroup.findViewById(R.id.mytext);
for(String test : strings){
mytextview = mytextview.clone();
mytextview.setText(test);
myViewGroup.addView(mytextview);
}
But it is not working.
Maybe use an inflater, and put the textview in an external layout file:
View v = LayoutInflater.from(this).inflate(R.layout.textview_include, null);
viewGroup.addView(v);
Using code of Mathias Lin and using the hint from javahead76:
LinearLayout x = (LinearLayout) findViewById(R.id.container);
for (int i = 0; i < 5; i++) {
View c = LayoutInflater.from(this).inflate(R.layout.singlerow, x);
TextView t = ((TextView)findViewById(R.id.textView1));
t.setId(i+10000);
t.setText("text"+i);
}
TextView b = (TextView) findViewById(10003);
b.setText("10003");
If you do this, you will most likely get the exact same id for every view created this way. This means doing things like ((TextView)v).setText("some text"); will be called on every TextView previously inflated from the same layout. You can still do it this way, but you should call setId() and have some reasonable method for ensuring you do not get the same id twice in a row - incrementation or universal time, etc.
Also, I think Android reserves a certain range of id's for dynamically created id's. You might avoid ID's in this range; but honestly, I don't know the id system works so I could be wrong on this point.

Categories

Resources