Android ListView Multiple Columns from an Array - android

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.

Related

Listview with linear straight progressbars

I'm new to android and now trying to make a small project.
It includes a listview with textviews and a linear progressbar (its value doesn't need to change).
Simple Adapter was used to fill data to textviews.
But to set progressbar value, which adapter should be used.
I can't image how to set value to progressbar.
Please help with this.
Here is my code:
main.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">
<ListView
android:id="#+id/dblist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector" />
</LinearLayout>
list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >
<!-- ListRow Left sied Thumbnail image -->
<LinearLayout android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:layout_alignParentLeft="true"
android:background="#drawable/image_bg"
android:layout_marginRight="5dip">
<ImageView
android:id="#+id/list_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="#drawable/ic_launcher"/>
</LinearLayout>
<!-- Title Of Song-->
<TextView
android:id="#+id/col1tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/thumbnail"
android:layout_toRightOf="#+id/thumbnail"
android:text="Rihanna Love the way lie"
android:textColor="#040404"
android:typeface="sans"
android:textSize="15dip"
android:textStyle="bold"/>
<!-- Artist Name -->
<TextView
android:id="#+id/col2tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/col1tv"
android:textColor="#343434"
android:textSize="10dip"
android:layout_marginTop="1dip"
android:layout_toRightOf="#+id/thumbnail"
android:text="Just gona stand there and ..." />
<TextView
android:id="#+id/package_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/col1tv"
android:textColor="#343434"
android:textSize="10dip"
android:layout_marginTop="1dip"
android:layout_toRightOf="#+id/col2tv"
android:text=" puzzles, 0 solved" />
<!-- Rightend Duration -->
<!-- Rightend Arrow -->
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/arrow"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
main.java
String[] from = new String[] {"id", "name", "size"};
int[] to = new int[] { R.id.id, R.id.name, R.id.size };
// prepare the list of all records
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < pkg_id_col.size(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("id", id_col.get(i));
map.put("name", name_col.get(i));
map.put("size", size.get(i));
fillMaps.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.list_row_add_new, from, to);
lv_add_new.setAdapter(adapter);
The SimpleAdapter knows how to bind data only for TextViews and ImageView. To bind your ProgressBar use a SimpleAdapter.ViewBinder:
String[] from = new String[] {"id", "name", "size", "progress"};
int[] to = new int[] { R.id.id, R.id.name, R.id.size, R.id.theIdOfTheProgressBar}; // it seems that you don't have a `ProgressBar` in the layout
// prepare the list of all records
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < pkg_id_col.size(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("id", id_col.get(i));
map.put("name", name_col.get(i));
map.put("size", size.get(i));
map.put("progress", valueForProgressBar); // progress will hold the progress for your ProgressBar
fillMaps.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.list_row_add_new, from, to);
adapter.setViewBinder(new SimpleAdapter.ViewBinder() {
public boolean setViewValue(View view, Object data, String textRepresentation) {
if (view.getId == R.id.theIdOfTheProgressBar) {
((ProgressBar) view).setProgress(Integer.parseInt(data));
return true;
}
return false;
}
});

Android - is there a way to have a 2-column row layout and tell which column a user presses?

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

How to get Images from JSON to main.xml file

I am not getting images from JSON.
How to get "LogoFileName" image from my JSON. I am only getting the title and description. But i need LogoFileName also.
If any body knows any solutions please help me.
Code:
{"Channels":[{"AlertDetailsFont":"","AlertDetailsFontId":0,"AlertHeaderFont":"","AlertHeaderFontId":0,"Category":"Education","CategoryId":7,"ChannelId":53,"Color1":"Red","Color2":"Black","Color3":"Black","Color4":"","Color5":"","ColorCode1":"FF0000","ColorCode2":"000000","ColorCode3":"000000","ColorCode4":"","ColorCode5":"","ColorId1":1,"ColorId2":3,"ColorId3":3,"ColorId4":0,"ColorId5":0,"Description":"This must be visible now","FontSize":"12px","FontSizeId":1,"HeaderFont":"Arial","HeaderFontId":2,"IsBold":false,"IsItalic":false,"LogoFileName":"http:\/\/183.82.97.146:1111\/CAWebsite\/UserImages\/ChannelLogos\/02022012_170628.jpg","ModifiedBy":12,"ModifiedDate":"\/Date(1328182588000+0530)\/","ModifiedUsername":"skrt","PublicChannelId":"f12d79aa-4267-11e1-a94b-0021706baaba","TickerFont":"","TickerFontId":0,"Title":"Books"},,.................etc
Hi here is my code..
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="6dip"
android:src="#drawable/icon" />
<TextView
android:id="#+id/item_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/icon"
android:layout_alignWithParentIfMissing="true"
android:layout_toRightOf="#+id/icon"
android:gravity="center_vertical"
android:text="My Application" />
<TextView
android:id="#+id/item_subtitle"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/icon"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Simple application that shows how to use RelativeLayout" />
</RelativeLayout>
MainActivity.java
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
JSONObject json = JSONfunctions.getJSONfromURL("http://my path...");
try{
JSONArray channels = json.getJSONArray("Channels");
for(int i=0;i<channels.length();i++){
HashMap<String, String> map = new HashMap<String, String> ();
JSONObject e = channels.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("name", e.getString("Title"));
map.put("Category", e.getString("Category"));
//map.put("imageUrl", e.getString("LogoFileName"));
imageUrl= e.getString("LogoFileName");
System.out.println("Logo Url:"+imageUrl);
mylist.add(map);
}
}catch(JSONException e)
{
Log.e("log_tag", "Error parsing data "+e.toString());
}
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main,
new String[] { "name", "Category" },
new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
Let me guess: you do not see the System.out.println("Logo Url:"+imageUrl) output, right? Use Log instead. Or am I getting you wrong?

ArrayList of Hashmaps to ListView nullpointerexception

I keep getting this error:
Unable to start activity ComponentInfo{com.Nostradamus/com.Nostradamus.Contactenlijst}: java.lang.NullPointerException
And I don't know what I do wrong.
It has something to do with the last code:
lv.setAdapter(new SimpleAdapter(this, mylist, R.layout.list_item, from, to));
This is my java code:
public class Contactenlijst extends Activity {
ListView lv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] from = new String[] {"voornaam", "achternaam", "geboortedatum", "adres", "postcode", "woonplaats", "email", "telefoon"};
int[] to = new int[]{R.id.voornaam, R.id.achternaam, R.id.geboortedatum, R.id.adres, R.id.postcode, R.id.woonplaats, R.id.email, R.id.telefoon};
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
Log.d("test","test2");
// Get the data (see above)
JSONObject json = Database
.getJSONfromURL("http://fabian.nostradamus.nu/Android/getcontactinfo.php");
Log.d("test","test3");
try {
JSONArray contactinfo = json.getJSONArray("contactlijst");
Log.d("test","test4");
// Loop the Array
for (int i = 0; i < contactinfo.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = contactinfo.getJSONObject(i);
map.put("voornaam", e.getString("staff_name"));
map.put("achternaam", e.getString("staff_lastname"));
map.put("geboortedatum", e.getString("staff_dateofbirth"));
map.put("adres", e.getString("staff_address"));
map.put("postcode", e.getString("staff_address_postal"));
map.put("woonplaats", e.getString("staff_address_city"));
map.put("email", e.getString("staff_email"));
map.put("telefoon", e.getString("staff_phone"));
mylist.add(map);
Log.e("test",map.toString());
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
lv = (ListView) findViewById(R.id.list);
lv.setAdapter(new SimpleAdapter(this, mylist, R.layout.list_item, from, to));
}
}
And these are my xmls:
Contactenlijst
<?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">
<TextView android:id="#+id/voornaam"
android:layout_width="50dip"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/achternaam"
android:layout_width="70dip"
android:layout_height="wrap_content" android:layout_weight="1"/>
<TextView android:id="#+id/geboortedatum"
android:layout_width="60dip"
android:layout_height="wrap_content" android:layout_weight="1"/>
<TextView android:id="#+id/adres"
android:layout_width="50dip"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/postcode"
android:layout_width="70dip"
android:layout_height="wrap_content" android:layout_weight="1"/>
<TextView android:id="#+id/woonplaats"
android:layout_width="60dip"
android:layout_height="wrap_content" android:layout_weight="1"/>
<TextView android:id="#+id/email"
android:layout_width="60dip"
android:layout_height="wrap_content" android:layout_weight="1"/>
<TextView android:id="#+id/telefoon"
android:layout_width="60dip"
android:layout_height="wrap_content" android:layout_weight="1"/>
Contactview
<?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/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:drawSelectorOnTop="false"/>
<TextView android:id="#id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="No data">
</TextView>
</LinearLayout>
and Listitem
<?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>
you didn't set the layout using
setContentView(R.layout.yourlayout);
so without add layout you are using your listview that's why you getting NullPointerException.
add this above line after
super.onCreate(savedInstanceState);
or before the use listview object
You need to call setContentView(...) before lv = (ListView) findViewById(R.id.list);
setContentView(...) inflates the XML file for your layout. If you don't do this, then lv will be null.

How to make a android interface with "football league table" style?

I need to show a football league table on my app. Like this: http://www.fuencalientedelapalma.org/sitios/Fuencaliente/VF7/PublishingImages/Clasificacion.png
As you see i need to do some kind of Android XML layout that shows 9 columns, and 10 rows, plus one more row for the name of the column.
I read all the documentation of listview on the android developers guide, but i can't find the way to do it. And also i search on google and i can't find the way to do it.
Code examples are welcome
thanks
You can use listView like this, inflating you own listlayout :
maListViewPerso = (ListView) findViewById(R.id.listviewperso);
ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
for (int i = 0; i<statDB.getListStat().size(); i++){
Stat stat= statDB.getListStat().get(i);
listId.add(statDB.getListStat().get(i).getId()+"");
String message = " Chiffre d'affaire : "+statDB.getListFinancier(stat.getId()+"").get(0).getCaBrut()+" euros";
map = new HashMap<String, String>();
titre
map.put("titre", stat.getDate());
map.put("description", message);
map.put("img", String.valueOf(R.drawable.icon_crisalid));
map.put("stat_in",""+statDB.getListStat().get(i).getId());
listItem.add(map);
}
statDB.close();
SimpleAdapter mSchedule = new SimpleAdapter (this.getBaseContext(), listItem, R.layout.listview,
new String[] {"img", "titre", "description"}, new int[] {R.id.img, R.id.titre, R.id.description});
maListViewPerso.setAdapter(mSchedule);
maListViewPerso.setOnItemClickListener(new OnItemClickListener() {
#Override
#SuppressWarnings("unchecked")
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
HashMap<String, String> map = (HashMap<String, String>) maListViewPerso.getItemAtPosition(position);
Intent intent=new Intent().setClass(ListActivity.this, DetailActivity.class);
intent.putExtra("stat_in", map.get("stat_in") );
intent.putExtra("listId",listId);
startActivity(intent);
}
});
this is my listview code :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="20px"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingLeft="10px"
android:layout_weight="1"
>
<TextView android:id="#+id/titre"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="16px"
android:textStyle="bold"
android:textColor="#color/textcol"
/>
<TextView android:id="#+id/description"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="12px"
android:textColor="#color/textcol"
android:paddingRight="10px"
/>
</LinearLayout>
and this is how I check the listview :
<?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"
android:background="#color/backgroundcolor"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="fill_horizontal"
android:paddingTop="5px"
android:paddingBottom="5px"
android:background="#drawable/bg2"
>
<TextView
android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
/>
</LinearLayout>
<ListView
android:id="#+id/listviewperso"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
you just have to adapt the listview.

Categories

Resources