I'm new to Android and I think I'm trying to do something really basic: I have a 5 strings in my Array (say 'One', 'Two', ...). I want to add these 5 strings to my list view in my listactivity.
My List:
<ListView
android:id="#+id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
My List Row:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="#+id/homeItemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
Basically, I want to bind the Array items to the TextView homeItemName. I might add other items in my row later, so I can't just bind the listview to the entries.
Thanks!
For code, take a quick look at this step-by-step tutorial
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));
ListView lv = getListView();
It shows a basic implementation of an ArrayAdapter:
R.layout.list_item : is the xml layout (list_item.xml) that will be used for every ROW of your
listview.
COUNTRIES is the array of Strings.
You can use an ArrayAdapter to bind your data. Since you want to be able to add extra data items to the view, you to give the adapter an ArrayList (since an array is of a fixed size). Items should be added via the ArrayAdapter and your ArrayList will be updated automatically. I have an example at http://www.box.net/shared/yduel9txya
Related
I'm making an app which allows a user to search the various countries. The user can make filtered searches (such as, for example, search only countries of a specific continent, etc...). All this info (Countries and their continents are stored in my Firebase Realtime Database).
In my FilteredResults.java fragment I want to have a variable number of ImageViews (the number of the size of a List<String>).
This sketch I drew might help you understand it better:
Each one of this rectangles are ImageViews.
This is my XML (I just have a scrollview because I don't know how to create an "array" of ImageViews...)
<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">
<TextView
android:id="#+id/txtResultadosFiltrados"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="28dp"
android:layout_marginTop="0dp"
android:text="Resultados Filtrados"
android:textColor="#323B45"
android:textSize="24sp"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="28dp" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ScrollView>
</RelativeLayout>
In order to solve your problem in an optimal way, you should use one of:
a plain old ListView,
a newer, more powerful RecyclerView.
These views support so called adapters which map between a model (in your case: List<String>) and a list of items (in your case a list of ImageView's).
Here you have a complete example of a simple app which loads lists of posts from the Firebase Database:
https://github.com/firebase/quickstart-android/tree/master/database
This example is based on RecyclerViews, you can find an a direct usage here:
https://github.com/firebase/quickstart-android/blob/master/database/app/src/main/java/com/google/firebase/quickstart/database/fragment/PostListFragment.java
Use RecyclerView instead of ScrollView. As keeping array of ImageView and adding them in a ScrollView will create a explosive headache.
I created a listview and recycler/card view with firebase no problem, but I cannot do a gridview involving pictures using picasso library. I have been trying for 3 days now.
Can someone point me in the correct direction? I can't seem to find any tutorials or videos on such process...
Thank you in advance.
I hope this process will work for you.
Define GridView in your layout like this
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="2"></GridView>
Create an adapter inflating layout like this,
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:scaleType="fitXY"/>
Fetch ImageUrl from firebase add in arraylist and provide that arraylist to adapter.
get that imageUrl from arrylist and set to the imageview in adpter by using picasso.
I am wondering what's the difference between #+id/android:list and #+id/list. I know the last one which is a regular id assignment but the first looks different. What makes it special?
Where I saw it:
I was studying on ListView, ListAdapter and things like that and the author define the ListView in layout xml file as below :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="#+id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/main_no_items"/>
</LinearLayout>
and also let me mention #+id/android:empty id as well.
And he also extends ListActivity class.
Here is the source of the article.
And also what's in my mind as questions are :
Should we extend ListActivity? Maybe I want an Activity which also contains other Views.
We use #+id/android:list just because we extend ListActivity or we can use the same convention if we extend Activity?
Thanks.
Resource IDs in Android are specific to a package (which is good, or else you'd have lots of conflicts if your app is dealing with several packages at the same time).
#+id/list will create a resource ID in your app (=your package) with the name "list" and give it a unique ID. In code, that would be R.id.list.
#android:id/list will use the ID "list" from the package android (which, in code, would be android.R.id.list.
EDIT: Need to add the corrections David Hedlund pointed out: The proper reference would be #android:id/list. Also, + indicates you're defining a new ID - you obviously don't need that when you're referencing something that was defined in the Android API.
I think the example code you posted has a typo, so it should be #android:id/list (without the +). From the ListActivity javadoc:
your own view MUST contain a ListView object with the id "#android:id/list"
#android:id/list is specific to ListActivity, so you do not need it if you are adding a ListView into any other kind of Activity. You should extend ListActivity if you want the user to do more than view the list. For example, you can override ListActivity.onListItemClick to respond to clicks on an item in the list.
Similarly, #id/android:empty (again, without the +), is a special case for ListActivity. This allows you to specify an alternative view that should be displayed when your list is empty. That View will not be displayed when the list is populated.
in android,
In XML: #[package:]layout/filename
like
android:id="#+id/android:list"
This is the standard way to refer to a list view when refering to listFragment or listActivity
so filename is android:list is a reference to ListView.
navigate to res/values/ids.xml
you will find <item type="id" name="list" />
ListView is a view group that displays a list of scrollable items. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database query and converts each item result into a view that's placed into the list.
This isn't much of a question, more of an explanation, I like to know whats happening and not treat things like a black box. I'm making an android app, and I created a form where you can dynamically make a new field to add to the form, and the field has 2 edit text next to each other.
formLayout.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<EditText
android:id="#+id/team_form_player_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginRight="7dp"
android:layout_weight="1"
android:background="#color/white"
android:hint="#string/form_hint_team_player_name"
android:inputType="textCapWords"
android:padding="15dp"/>
<EditText
android:id="#+id/team_form_player_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="#color/white"
android:hint="#string/form_hint_team_player_number"
android:inputType="number"
android:padding="15dp"/>
</LinearLayout>
And this is inside a button listener when clicked, it'll create the new field
View layout = getLayoutInflater().inflate(R.layout.layout_add_new_field_team, null);
EditText name = (EditText)layout.findViewById(R.id.team_form_player_name);
EditText number = (EditText)layout.findViewById(R.id.team_form_player_number);
playerName.add(name); //arraylist
playerNumber.add(number); //arraylist
containerLayout.addView(layout);
being that its a different layout i had to inflate the view. anyway, my question is this. how does this even work? by code, I'm adding an edit text, each time to its respectful array. im adding the same layout each time, im not even using "new" so its not instantiating anything new. and if I create 10 fields at once, then fill in the fields. and run this method
for(EditText edit : playerNumber) {
String test = edit.getText().toString();
System.out.println(test);
}
it gives me all the correct values in each field. How does that happen? Because on add field click, it instantly adds the edit text to the array with the fields EMPTY. and theres no code inputting the values into the array. I'm just boggled that this works and would like to know how it does. anyone have any idea?
.inflate(layoutResId) == "create new instance of this layout"
So basically every time you use inflate you are creating instances of all the Views in the layout, in your case a LinearLayout and two EditText Views inside it.
I have written the code for creating a list out of some arrays shown below!
code runs properly and output is as expected!
update for people with same prob: nice tutorial for custom listview
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView1 = (ListView) findViewById(R.id.listView1);
String[] items = { "some", "fancy", "items", "to", "show" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.listitem, items);
listView1.setAdapter(adapter);
}
activity_main.xml
<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=".MainActivity" >
<ListView
android:id="#+id/listView1"
android:layout_height="fill_parent"
android:layout_width="fill_parent" />
</RelativeLayout>
listview.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:padding="22dp"
android:gravity="center_horizontal"
/>
what I want to accomplish?
change color and font of the text in each list item to a different one..and do some task on tapping on them...
also is it possible to get another listview inside the same listview
eg. if I click on a list item it again shows me a list (kind of a sub list) with different list items on that same activity(or screen).and some action could be done on tapping the sub list items.
Detailed answers are appreciated as I am new to android development..
Thanks!
1.change color and font of the text in each list item to a different one..and do some task on
tapping on them...
Make a Custom Adapter, override getVIew() of that adapter and make
changes on color and text in it.
override onItemClick() for your ListView. to accomplish click event
for list item.
Now
2. also is it possible to get another listview inside the same listview eg. if I click on a
list item it again shows me a list (kind of a sub list) with different list items on that
same activity(or screen).and some action could be done on tapping the sub list items.
3. what are my other list styling options..
Its a Expandable ListView Just look at link for more
information.
And Tutorial
You'll want to look into a custom ArrayAdapter as seen here http://www.vogella.com/articles/AndroidListView/article.html
That will take care of the first and third questions. As for the second one that's not possible with the default implementation of ListView but there are libraries that enable you to create drop down listItems.
For 1st question, you can use paulthom12345' answer.
2nd question: You have to use exapandableListView
for more details please see: Android ExpandableListView - Looking for a tutorial
3rd question is not constructive and very vague. Please edit the question and explain in greater detail.
Use Customized List View for it.
It is not exactly possible to have a list inside a list. Instead of it, use expandable list
Have a look at Android Listview with different layout for each row