I have been playing with the list activity tutorial here:
http://developer.android.com/resources/tutorials/views/hello-listview.html
which tells you to start off extending List activity.
by public class Main extends ListActivity {
Which is based on inflating a textview only layout.
<?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="fill_parent"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
If I want to customize the layout more by adding images,and an extra linear layout above the list adapter etc- is it possible using this method- if so how do I do it?
It is possible by using a SimpleAdapter.
Here is an example :
// Create the item mapping
String[] from = new String[] { "title", "description" };
int[] to = new int[] { R.id.title, R.id.description };
Now "title" is mapped to R.id.title, and "description" to R.id.description (defined in the XML below).
// Add some rows
List<HashMap<String, Object>> fillMaps = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("title", "First title"); // This will be shown in R.id.title
map.put("description", "description 1"); // And this in R.id.description
fillMaps.add(map);
map = new HashMap<String, Object>();
map.put("title", "Second title");
map.put("description", "description 2");
fillMaps.add(map);
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.row, from, to);
setListAdapter(adapter);
This is the corresponding XML layout, here named row.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
I used two TextViews but it works the same with any kind of view.
Related
I created an android actionbar tab application, with 4 tabs, one of the tab is a
fragment class which is used to contain a list view,to display a list of static information:
However, I have an error at this line of code:
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList,
R.layout.listoflockers, from, to);
These are my 2 xml files used to create a list view:
listoflockers.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
lockerinforow.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/flag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/email"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
/>
<TextView
android:id="#+id/cur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
/>
<TextView
android:id="#+id/size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
/>
</LinearLayout>
</LinearLayout>
public class myLockerFragment.java{
.....
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_mylocker, container, false);
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>
();
for(int i=0;i<10;i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("txt", "Locker No : " + i);
hm.put("cur","Location : " + currency[i]);
hm.put("size","Size : " + size[i]);
hm.put("flag", Integer.toString(flags[i]) );
aList.add(hm);
}
// Keys used in Hashmap
String[] from = { "flag","txt","cur","size" };
// Ids of views in listview_layout
int[] to = { R.id.flag,R.id.txt,R.id.cur,R.id.size};
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList,
R.layout.listoflockers, from, to);
// Getting a reference to listview of main.xml layout file
ListView listView = ( ListView ) rootView.findViewById(R.id.listview);
// Setting the adapter to the listView
listView.setAdapter(adapter);
return rootView;
}
Please help
I am confused at this line:
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList,
R.layout.listoflockers, from, to);
giving me the error:
The method getBaseContext() is undefined for the type myLockerFragment
you can use getActivity() inside a Fragment to get the context that holds the Fragment itself
Do this-
SimpleAdapter adapter = new SimpleAdapter(getActivity(), aList,
R.layout.listoflockers, from, to);
SimpleAdapter adapter = new SimpleAdapter(getActivity().getApplicationContext(), aList, R.layout.listoflockeres, from, to);
You need to use the getActivity() method inside Fragments
Besides using getBaseContext() you need to use the getActivity() in Fragments to get the context of the Fragment.
Try out as below:
SimpleAdapter adapter = new SimpleAdapter(getActivity(), aList,R.layout.listoflockers, from, to);
I have this layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1.0">
<TextView android:id="#+id/TRAIN_CELL"
android:layout_weight=".8"
android:layout_height="wrap_content"
android:layout_width="0dip"
android:textSize="16sp"/>
<TextView android:id="#+id/TO_CELL"
android:textSize="20sp"
android:textStyle="bold"
android:gravity="center"
android:textColor="#color/light_best_blue"
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight=".2"/>
</LinearLayout>
and this is the snippet that references this in the parent screen
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="#+id/label"
android:textSize="17sp">
</ListView>
and I am trying to have some text on the left side, and a > character on the right side so that the user knows to choose it.
Here is how I populate the list:
private List<HashMap<String, String>> fillMaps;
private SimpleAdapter adapter;
ListView list = null;
list = (ListView) findViewById(android.R.id.list);
// My data
fillMaps = new ArrayList<HashMap<String, String>>();
adapter = new SimpleAdapter(this, fillMaps, R.layout.my_problems,
new String[] {"train", "to"},
new int[] {R.id.TRAIN_CELL, R.id.TO_CELL});
for ( int i = 0; i < obj.length(); i++ )
{
HashMap<String, String> map = new HashMap<String, String>();
JSONObject o = obj.getJSONObject(i);
problem_title = o.getString("problem_title");
problem_id = o.getString("problem_id");
String has_new_comments = o.getString("has_new_comments");
String is_private = o.getString("is_private");
Problem p = new Problem ( );
p.setProblemId(problem_id);
p.setProblemName(problem_title);
p.setIsPrivate(is_private);
p.setHasNewComments(has_new_comments);
map.put("train", p.toString() );
map.put("to", ">");
fillMaps.add(map);
problems.add( p );
}
adapter.notifyDataSetChanged();
But the screen looks like mess with the column on the left appearing right next to the column on the right with no space, and the ">" character is not lined up on the same column but it spaced randomly depending where the left column ends.
Any thoughts on what may be going wrong?
Thanks!
In a 2-column row layout, is there a way to tell which of the columns the user clicked or pressed on?
If the second column can be a button, it would be even better, but I am not sure how to make it into a button. Here is what I am doing:
private List<HashMap<String, String>> fillMaps;
private SimpleAdapter simple_adapter;
ListView list = null;
and later:
list = (ListView) findViewById(android.R.id.list);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
// My data
fillMaps = new ArrayList<HashMap<String, String>>();
simple_adapter = new SimpleAdapter(this, fillMaps, R.layout.comment_list,
new String[] {"train", "from"},
new int[] {R.id.TRAIN_CELL, R.id.FROM_CELL });
// This was the middle item R.id.FROM_CELL,
list.setAdapter(simple_adapter);
list.setTextFilterEnabled(true);
and I populate the list like this:
for ( int i = 0; i < obj.length(); i++ )
{
JSONObject o = obj.getJSONObject(i);
HashMap<String, String> map = new HashMap<String, String>();
... Some variables set here...
map.put("train", comment);
map.put("from", "Edit");
fillMaps.add(map);
discussion.add( d );
}
}
simple_adapter.notifyDataSetChanged();
Here is the comment_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="4dip"
android:paddingBottom="6dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="13sp"
android:weightSum="1.0"
android:orientation="horizontal">
<TextView android:id="#+id/TRAIN_CELL"
android:textSize="16sp"
android:layout_height="wrap_content"
android:layout_width="275dip"/>
<TextView android:id="#+id/FROM_CELL"
android:textSize="16sp"
android:textStyle="bold"
android:layout_height="wrap_content"
android:layout_width="50dip"/>
</LinearLayout>
Thanks!
try using some clickable items in the two columns of the row and handle those click events.
Suppose give two buttons in those two columns and add setOnClickListeners to them and finally check which button is click in the activity
You can use something like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="4dip"
android:paddingBottom="6dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="13sp"
android:weightSum="1.0"
android:orientation="horizontal">
<Button
android:id="#+id/TRAIN_CELL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_height="wrap_content" />
<Button
android:id="#+id/FROM_CELL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/label_grid_view" />
</LinearLayout>
Be back if you have any issues
I have a list view with following row template.
<?xml version="1.0" encoding="utf-8"?>
<!-- row.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal">
<RelativeLayout android:clickable="true"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="#drawable/android_btn_large" android:gravity="center_vertical"
android:layout_weight="1">
<TextView android:id="#+id/txtLeftButton" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_toRightOf="#+id/imgLeftButton"
android:text="Riverside Park" android:textColor="#FFFFFF"
android:layout_alignParentLeft="true"></TextView>
<ImageView android:id="#id/imgLeftButton" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_toLeftOf="#id/txtLeftButton"
android:layout_alignParentLeft="true" android:layout_centerVertical="true"
android:layout_alignParentRight="true" android:src="#drawable/plus_icon_480">
</ImageView>
</RelativeLayout>
<RelativeLayout android:clickable="true"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="#drawable/android_btn_large" android:gravity="center_vertical"
android:layout_weight="1">
<TextView android:id="#+id/txtRightButton" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_toRightOf="#+id/imgRightButton"
android:text="Riverside Park" android:textColor="#FFFFFF"
android:layout_alignParentLeft="true"></TextView>
<ImageView android:id="#id/imgRightButton" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_toLeftOf="#id/txtRightButton"
android:layout_alignParentLeft="true" android:layout_centerVertical="true"
android:layout_alignParentRight="true" android:src="#drawable/plus_icon_480">
</ImageView>
</RelativeLayout>
</LinearLayout>
And the data is stored in a List as...
List<String[]> rows = new ArrayList<String[]>();
rows.add(new String[] { "id", "name" });
I want to know what Adapter I should use to fill the ListView with these rows assuming I need 2 column data in a single row?
Just use a SimpleAdapter with a list of HashMap
ListView list = (ListView) findViewById(R.id.ListView01);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("id", "1");
map.put("name", "bob");
mylist.add(map);
//Each row is a new hashmap
map = new HashMap<String, String>();
map.put("id", "2");
map.put("name", "sally");
mylist.add(map);
//ect...
mSchedule = new SimpleAdapter(this, mylist, R.layout.listrow,
new String[] {"id", "name"}, new int[] {R.id.txtID, R.id.txtName});
list.setAdapter(mSchedule)
You could implement the ListViewAdapter interface in a class and inflate the row-xml in the getView-method. You can use a regular activity with a simple LinearLayout and a list view in it.
I've been doing this for a few months now and I've found it almost impossible to find an example where the ListView is a ListView not a ListActivity. In my program all I'm wanting to do is have half the screen a ListView and the other half something else so the ListView can be moved around in the layout. Does anyone know any such examples?
I have created implementation of ListView, not ListActivity.
list_produk.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/lvListProduk"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.98"
tools:listitem="#layout/list_produk_row" >
</ListView>
<Button
android:id="#+id/btnListProdukBack"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/back_" />
</LinearLayout>
list_produk_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/tvProdukName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
ListProduk.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_produk);
String[] from = new String[] {"prd_name"};
int[] to = new int[] {R.id.tvProdukName};
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("prd_name", "BlackBerry");
fillMaps.add(map);
map = new HashMap<String, String>();
map.put("prd_name", "Android");
fillMaps.add(map);
map = new HashMap<String, String>();
map.put("prd_name", "iPhone");
fillMaps.add(map);
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.list_produk_row, from, to);
ListView listView = (ListView) findViewById(R.id.lvListProduk);
listView.setAdapter(adapter);
}
list_produk_row.xml, that contains a TextView, is the layout for list item of ListView.