AlertDialog vs Spinner vs ListView - android

I've got what I thought was a simple android UI design problem but I've been going around in circles for a couple of days. I have a REST service that I'm downloading XML from and displaying the XML in a form in an android app. I have a web page built and am mimicking this with android, same options, same URLs being sent to the REST service whether from android or the web pages. With HTML I can easily create checkbox groups and radiobutton/dropdowns for various id/display items, so for instance, I can display a planet option as:
<select name="planet"><option value="0">Mercury</option></select>
I wanted to do something similar in android where I had a pair of values, one an id and the other the user-friendly text to display. So I decided to create an adapter using android.util.Pair:
public class PairView extends Pair<String, String> {
public PairView(String first, String second) {
super(first, second);
}
public String toString() {
return second;
}
}
public class PairAdapter extends ArrayAdapter<PairView> {
}
So now I can put my id in pair.first and what to display to the user in pair.second.
My problem comes in that some of these options will be single-selects and some will be multi-selects. In html, that's not an issue, just use a checkbox group for multi, and radio buttons/dropdowns for single selects. In android however, it seems it's not so straight forward. I tried using Spinners for the adapters, but Spinner seems to only allow single selection. AlertDialog.Builder allows for single and multi-selections, but curiously I don't see an option for using an adapter for the multi-selection, just for single selections.
I guess what I really want is a consistent look for all my options, with radio buttons displayed for single selections and checkboxes displayed for multi selections, via an adapter so I can get the id's from the Pair for the items selected.
What approach should I use? A custom spinner with code added for multi-selections? AlertDialog.Builder and somehow make it use an adapter for multi-selections? Just create a plain Alert and wrap a ListView in it? Another option that is (hopefully) simpler?
I feel like I'm missing something very basic here.

I had a similar situation in an app I was making so would share what I opted for. I had different type of questions and depending on that I removed and added things in my activity. For radio buttons I used with elements in it. For multiple choice questions I wanted a checkbox based view so I added an empty within my layout and in code added CheckBox(s) to it.
As for the caption and value, for radio buttons and checkboxes you can set display text by setText and add any object/value as a tag. So what I used to do was something like this:
CheckBox option = new CheckBox(MyActivity.this);
option.setText("Option 1");
option.setTag(10);
Later on when you get the selected option, you can simply get its tag and use its value.
This is just one way of doing it which I found simple. Hope this helps

Related

How to modify a ListView item in Android?

I'm starting learning Android and I want to know if there is some option in Android that let you modify each item or view (I don't know how it's named exactly, I mean each of the items from an ArrayList that you show in a ListView).
Well, I made a ListView that is going to show some books that were located in an ArrayList named "books".
I made a custom adapter that I associate to the ListView to show each item with the corresponding layout in the application. I also have a class "book" for each item that is going to be shown in the ListView.
Further, I made an Intent that I call from MainActivity with startForActivityResult(), that I process in Book class and that I return to MainActivity with all data of a book with the method setResult and got the information with onActivityResult() and the requestCode.
So I don't have any problem to add items to the ListView, just I have the problems if I want to modify some of the items (or views) that are located in the ListView (for example if I have title and author of a book, if I put some wrong information, I want have the option to change it).
I have that, in the same moment that you click on some of the items of the ListView, a new layout will be show to modify the information that it's wrong so I use the method setOnItemClickListener with onItemClick event on the custom adapter that I created before. Here it's where I call the new Intent to modify the wrong information with the method startActivityForResult().
I made the same as before to add a new item but, instead of add a new item with custom_adapter.addBook(title,author) I want to know if there is some option to made something like this: custom_adapter.modify(title,author) or custom_adapter.update(title,author), I mean, when you have modify all the items that were wrong of a book (for example an EditText that were "title") and you have all the information in the MainActivity class (because you returned it with setResult), how to put it again in the same item updating it in the custom_adapter and also in the ArrayList.
I searched it on the Internet but I didn't find anything.
I'm sorry if I have a poor English, but I expect that it can be understand.
Thank you very much!
If I am understanding your problem correctly - you could simply modify the ArrayList of type Book that is backing your ArrayAdapter.
So if you know what Book object you want to modify then you can simply make your changes to the Book object itself. As long as this Book is a reference to the same object that you originally added to the ArrayList you instantiated your ArrayAdapter with then you can then call custom_adapter.notifyDataSetChanged() to tell the adapter to redraw its childviews with the new data.
There are some good code samples on the Internet but you have to understand the code for your purposes. So...here is a start, look at Using an ArrayAdapter with ListView. The code shows the use of ArrayAdapter with getView() method. And I hope it shows how to define the listeners, which you need.
How about that for a start? Have fun...
Thank you very much for all help you gave to me. I'm very pleased with you. :)
Finally, I just send the info with a Bundle when I started the Intent, also with the position. And after, I just used this position to set the new info to my items (in my case, books).
Again, thank you very much ;)

Using text files and XML defined strings to populate lists

I am writing an application which contains a few simple views; first is just two buttons. each button leads to a map, and from this map is a button which opens a list of buildings on the site. From here the user will be able to select a building and view detailed information on it (much like a contacts list actually).
So far, I am able to populate the listview by creating the string array to populate it it directly within the activity, like so:
public class BuildingsActivity extends ListActivity {
static final String[] buildings=new String[]{
"Building 1",
"Building 2",
"Building 3",
"Building 4",
"Building 5"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buildings);
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,buildings));
}
This works all well and good for a 5 item list, but in it's final form, the list may contain up to a hundred buildings, and I have 2 concerns:
1) A one-hundred item long string array is going to look really gross defined in code like this
2) I am worried about how long it will take the application to open the activity if it has to generate such a large list of string values beforehand
To avoid this, I had the idea to define this string array in the strings.xml file and assign the strings to the list layout in the corresponding layout XML for the list activity. However, this so far doesn't seem to work; no errors but the android:entries command does not see to affect the layout at all. This is the XML. If anybody has knowledge of populating a list this way, I would appreciate your answers.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BuildingsActivity" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/buildings">
</ListView>
</RelativeLayout>
My other alternative is to retrieve it from a text file, which I will have to do for building details (as the volume of text is too large to bother saving as strings) but wanted to avoid for the list. Admittedly, I have not heavily researched this route (both because I had not really wanted to use it yet and based on what I have seen on other forums it looks difficult, but if there is a lot of support for this method then I will certainly appreciate any advice in using it.
Forgive me if any of this has already been covered in other threads. I DID search for similar topics, but nobody seemed to want to populate a list this way and a lot of the language from their questions went over my head (I am very new...this is my first real application, besides having worked through some of the tutorials on the developer website.
Java's ArrayList implements the Serializable interface, which allows you to save and load an array to and from a pure binary format using the functions writeObject and readObject. This page should be enough to get you started.
If you want to populate the listView with from a resource file you just have to use this in your code:
String[] values = getResources().getStringArray(R.array.<input_file>);
Once you have the values loaded you just have to assign it to the ArrayAdapter like this:
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
The second parameter is a predefined layout of android SDKs, you could use your own layout.
If your String[]'s ceiling is 100, I don't see it as a problem to just generate the array and pass it to the adapter. As for your concern about the code looking gross, have you considered storing this information in a database and then requesting it? For instance, you could have a Building Entity, with all of its information and persist it to MySQL using OrmLite. http://ormlite.com/javadoc/ormlite-core/doc-files/ormlite_1.html#SEC1 You could then write a getter than returns an array representation to avoid the ugly code.

Displaying an array of objects, one at a time through a single dialog... instead of several dialogs

In my application I have a list of questions stored in an ArrayList, and I want to display a dialog that shows one question, and then continues to the next one after the question is answered. The way that I'm currently doing it (iterating through a loop) hasn't been working because it just layers all of the dialogs on top of one another all at once which causes a host of other issues. What I'm looking for is a way to still iterate through the questions, but just change the layout of the dialog each time until it has finished each question in the list. Can anyone give me a good pointer for how to get this going?
You can make a function that takes title and message as parameters and shows a dialog.
showDialog(String title, String message){ // Show dialog code here}
Within that dialog's answer button's listener call another function (showQuestion(currentQuestion)) that iterates the arrayList till it is over
int currentQuestion=0;
ArrayList<QuestionObject> questionList;
showQuestion(int i){
if(i<questionList.size()){
showDialog(questionList.get(i).getTitle,questionList.get(i).getMessage);
currentQuestion++;
}else{
//quiz is over
}
}
I assume you mean that you just want to change 1 single layout(created within XML i.e main.xml). In order to do this, make sure that the class your working on is pointing to that layout. From there (assuming your using an Event listener for when the user submits an answer) you can change do as you want by the following:
TextView txt = (TextView) findViewById(R.id.textView); // references the txt XML element
and in your Event listener, if the answer is correct then change(Have i be a global variable thats initially set to 0).
if(i<arrayList.size()){
txt.setText(arrayList.get(++i));
}else{
txt.setText("You Finished");
}
From there, in the else statement, you can change arrayLists and reset i to 0;
If you are trying to use the positive, neutral, and negative buttons; then you may have problems with multiple dialogs. Try defining a customized layout with your own TextViews, ListViews, and Buttons. You can implement listeners and everything else like a regular layout. Then just pass your customized layout to the dialog through AlertDialog.Builder.setView().
PS If you include code examples of what you are currently doing we can provided answers that are less vague.

Displaying a list with different types of information in Android

I want to create a page with X questions (the questions are stored in a database and I read them), headline and buttons at the end.
When the user clicks on one question than the question should transform into a dialog where the user can modify the question, however the other questions above and beneath it should still display the same way.
The way ListActivity is used in the sample notepad application in the android documentation it seems like the class can only display multiple items of the same type.
Is there a straightforward way to go about this problem?
I should tell you that I don't like your solution as an user.
I would prefer to chose from a list and having an edit activity after a click.
That's the default approach I've seen in every android app and it will be also easier for you.
If you still want to do what you explained I would try do this:
Create a ListView
Create a class QuestionOrDialog
Create an Adapter that extends from ArrayAdapter
Override getView doing something like:
QuestionOrDialog aQuestionOrDialog = getItem(position);
if ( aQuestionOrDialog.showDialog() ) {
return mInflater.inflate(R.layout.dialog, parent, false);
} else {
view = mInflater.inflate(R.layout.question, parent, false);
TextView question = (TextView) view.findViewById(R.id.question);
question.setText(aQuestionOrDialog.getQuestion());
}
On the OnClick you will have to do a getItem() and set that it was clicked.
Tell the listView that it's item have changed.
Hope it works.

How do I display consecutive AlertDialogs in Android?

I'm trying to port one of my iPhone apps over to the Android. It was all going along swimmingly until I came to AlertDialogs. In the iPhone app, sometimes there will be more than one alert to pass to the user. When this happens, the first alert dialog will come up, and when they click it away the next one will come up.
I can't seem to get more than one dialog box to come up like that in Android. Is it possible to display back to back AlertDialogs where a second one pops up as soon as the first is finished?
You execute 2 consecutive call to 'showDialog()' after eachother and the second will show after the 1st was dismissed:
showDialog(FIRST_DIALOG_ID);
showDialog(SECOND_DIALOG_ID);
Ofcourse you also have to implement onCreateDialog().
If you feel that you will be having multiple dialogs, one after another, you could create a custom class that holds all of the information for the alert, such as the title, text, icon, etc. From there, create an arraylist to store the custom class objects. When you are done with your first dialog, remove it from the arraylist, then check to see if there are any remaining dialogs that need to be presented.
The only issue you'll run into is that it will be much more difficult if you want to have different conditions in your Confirm and Cancel options.
public class DialogObject(){
String title;
String body;
String iconName; // or just an Image asset
}
ArrayList<DialogObject> dialogList = new ArrayList<>();
When a dialog is required, add it to the list if there is a dialog already on screen
dialogList.add(new DialogObject(param1, param2, param3));
This may not be the best way, but it is an option. The ArrayList will need to be in a separate class itself so you don't lose the data when moving from screen to screen.
For example - Note the "static" keyword.
public class DialogHolder(){
public static ArrayList<DialogObject> = new ArrayList<>();
}

Categories

Resources