How to Create Dynamic UI With String ID in Android? - android

I develop android application that in my application all layouts and widgets are created dynamically.
All data will be received from a JSON file and all ids for elements are also in the same file.
for example I want use this
String id=txt_site_id
TextView txt = new TextView(this);
txt.setID(id);
but it's can't work because setid just use int value for create dynamic id
Now,How I can Resolve my Problems
thanks

First thing came into my mind was using a Tag:
txt.setTag(id);
(...)
txt.getTag(id);
If this isn't enough, please provide more input, what you try to achieve.

Related

How to upload text view from android to a database in a website using a button

I'm currently doing a project from one of my subjects in my university. I'm about to do an attendance app that checks if the professors are present, late, or absent in the room based on the schedule. I made an app in an android studio which uses radio groups and radio buttons that will change the text in the text view which will show if the professors attendance status. I'm thinking of making a button that will upload the text views with the attendance statuses to a website which holds the data(database).
I'm not used to programming in the android studio that is why I would like to know if a way to upload the text views on a website. Thanks
It's quite simple, You have to create Weg api that you can call from the android mobile application and pass the current status of the professor or the whatever you want to pass on the server. That api will update the database as per whatever you have passed from the mobile application.
Also, One more thing that you can not pass the text view, you just need to pass the text of that view.
I hope, you will get answer from this.
Happy coding...
Assign value of textview to a string variable.
And use this string in your api to pass data.
For example if you have a textview with name teacher, you can get its value using this line.
String st_teacher = teacher.getText();
Now use that st_teacher value in your api to upload data.

Getting ImageButton Name

I am having some trouble with my code, and I am trying to figure out where each ImageButton is in my ArrayList to see if the problem is there. How do I retreive the name of an ImageButton from an ArrayList?
List<ImageButton> images = new ArrayList<ImageButton>();
images.add(imgbut1);
images.add(imgbut2);
images.add(imgbut3);
images.add(imgbut4);
images.add(imgbut5);
Here, I want to get imgbut1 for images.get(0), imgbut2 for images.get(1), etc. Something like images.get(0).getName(), images.get(1).getName(), etc
Thanks!
imgbut1, imgbut2 are names you gave to your instances. You cannot get the name.
Here are some good examples
Get name java instances

How do I create and read from arrays in Android?

I've used arrays in other languages (Python, MATLAB/Octave, C, Visual Basic, BASIC), but I haven't figured out how to use them in Android.
For example, how would I modify the Hello World program at World program at http://developer.android.com/resources/tutorials/hello-world.html to replace "Hello, Android" with the name of a TV show denoted by an index number (tvshow[0], tvshow[1], etc.)? Let's name the array tvshow, and the four values in the array are "Route 66", "The Twilight Zone", "Magnum P.I.", and "MASH".
What method(s) can I use to accomplish this?
There is also ArrayList . So all you have to do to add is call the add method and to read you use the get method.
ArrayList <String> myArrayList = new ArrayList<String>();
//this is how you add the elements you want to.
myArrayList.add("Route 66");
myArrayList.add("The Twitlight Zone");
myArrayList.add("Magnum");
myArrayList.add("P.I.");
myArrayList.add("Mash");
and to read the element you want:
String tvShow= myArrayList.get(2);
The advantage over a normal array (i.e. the one that android developer mentioned) is that you dont need to define an array size and you can add new elements in case the tv shows you have aument by just calling the add method.
well , you are talking about strings , so for an array of strings , you can simply use:
String[] tvshow={"Route 66","The Twilight Zone","Magnum", "P.I.", "MASH"} ;
for using it , use:
String currentTvShow=tvshow[3];
for working with strings , read this:
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html
strings on java (and on android, since it's based on it) are not mutable (for various reasons) , so they can't be modified. if you wish to modify such a thing , either use StringBuilder or an array of chars .

Conceptual help with Android ListView

I have a working android app using TextView, some formatting (line breaks, rows of dashes) and Linkify to generate a primitive "ListView-like" display with clickable URLs in each "row". I'd like to move up to a real ListView, but I'm just not finding the sample/explanation that I need to take that next step.
I have successfully reproduced the HelloListView sample, starting with the hardcoded string array, and moving to a string array defined in my res/values/strings.xml. I've taken one small step toward my goal by adding my HttpClient code to retrieve a set of data from a service, parse the results into a String Array and feed that into setListAdapter() such that my text and links show up as text-only in ListView items.
I want to move to the next step which is to make each "row" in my ListView launch the browser to the URL contained in the data, either by
(A) clicking anywhere in the row, or
(B) clicking a hyperlink displayed within the row data
For option (A), it appears that I need to have my onItemClick() method issue an intent that launches the browser. That's straightforward, but I don't get how to associate the URL with the item (currently its just one part of the string content for each "row" of text). How do I separate my URL from the rest of the text, such that I can launch a browser to the corresponding URL? Do I need to replace my String Array with an array of custom objects?
For option (B), can I use Linkify? It seems that my string array elements get converted to individual TextViews (inferring from the way the Toast text is generated in the HelloListView sample). Do I have access to that TextView to run Linkify against? Do I need to replace my String Array with a TextView Array and run Linkify myself? Am I completely off base?
Thanks to anyone who can help explain back to me what I'm trying to do, in a way that helps to find my way around the SDK, samples and other helps!
How do I separate my URL from the rest of the text, such that I can launch a browser to the corresponding URL?
Use a regular expression (java.util.regex) to find the URL.
For option (B), can I use Linkify?
Yes.
Do I have access to that TextView to run Linkify against?
Yes. Override getView() in your ArrayAdapter. Chain to the superclass and get your TextView from the result of super.getView().
Even better would be to use Linkify on your strings before putting them in the array in the first place.
Do I need to replace my String Array with a TextView Array and run Linkify myself?
No, and that is really not a good idea. Here is a free excerpt from one of my books that goes into more detail on tailoring the individual rows of a ListView, in case this helps.

Using text field with AdapterView

Is it possible to use AdapterView with text fields in android?
My query returns a set of values and for each I want to place that within a textfield, so that the user may edit the value.
Also, I want to click a button to create a new empty field, so that I may insert a new entry.
If you know of good example, then please let me know!
EDIT 1
I would prefer to use XML to define ui and I found this informations:
"In this case we create a new id called text1. The + after the # in the id string indicates that the id should be automatically created as a resource if it does not already exist, so we are defining text1 on the fly and then using it." Source http://developer.android.com/resources/tutorials/notepad/notepad-ex1.html
Will this + allow me to autocreate as many fields as needed? Is there a way I can generically specify the layout in XML and then create fields adhoc based on db response?
Many thanks,
Mith
Is it possible to use AdapterView with
text fields in android?
Yes. It will be complex for you (and possibly also for the user), so I would not recommend it unless you have a few months' Android programming experience, but it should work.
Also, I want to click a button to
create a new empty field, so that I
may insert a new entry.
That will get a little complicated.
Will this + allow me to autocreate as
many fields as needed?
No, that is not how you use ListAdapters for ListView rows.
Is there a way I can generically
specify the layout in XML and then
create fields adhoc based on db
response?
Use a CursorAdapter.
Here is a free excerpt from one of my books that describes creating custom adapter classes. Here is a sample project that shows creating a custom CursorAdapter to display the results of a database query.

Categories

Resources