ListActivity with ImageView

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

ListActivity with ImageView

Postby darolla » Sat Oct 25, 2008 2:47 pm

Hello,

I tried to develop a CustomList that extends ListActivity.

Right now I am searching for a possibility to add different images to each line.

Who can help me?

Greetings,
Marco

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. import java.util.ArrayList;
  3.  
  4. import java.util.HashMap;
  5.  
  6. import java.util.List;
  7.  
  8. import java.util.Map;
  9.  
  10.  
  11.  
  12. import android.app.ListActivity;
  13.  
  14. import android.content.Intent;
  15.  
  16. import android.os.Bundle;
  17.  
  18. import android.view.View;
  19.  
  20. import android.widget.ListView;
  21.  
  22. import android.widget.SimpleAdapter;
  23.  
  24.  
  25.  
  26. public class CustomList extends ListActivity {
  27.  
  28.  
  29.  
  30.         List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
  31.  
  32.  
  33.  
  34.         /** Called when the activity is first created. */
  35.  
  36.         @Override
  37.  
  38.         public void onCreate(Bundle icicle) {
  39.  
  40.                 super.onCreate(icicle);
  41.  
  42.                 setContentView(R.layout.customlist);
  43.  
  44.                
  45.  
  46.                 Intent i = new Intent();
  47.  
  48.                 i.setClass(CustomList.this, SomeActivity1.class);
  49.  
  50.                 addItem( list, "Fußball (525)", i );
  51.  
  52.                
  53.  
  54.                 i = new Intent();
  55.  
  56.                 i.setClass(CustomList.this, SomeActivity2.class);
  57.  
  58.                 addItem( list, "Deutschland (28)", i );
  59.  
  60.                
  61.  
  62.                 i = new Intent();
  63.  
  64.                 i.setClass(CustomList.this, SomeActivity3.class);
  65.  
  66.                 addItem( list, "1. Bundesliga (9)", i );
  67.  
  68.  
  69.  
  70.                 SimpleAdapter notes = new SimpleAdapter(this, list,
  71.  
  72.                                 R.layout.customlist_item_two_line_row,
  73.  
  74.                                 new String[] { "title" }, new int[] { R.id.text1 });
  75.  
  76.                 setListAdapter(notes);
  77.  
  78.         }
  79.  
  80.  
  81.  
  82.         public void addItem(List<Map<String,Object>> data, String name, Intent intent) {
  83.  
  84.         Map<String, Object> temp = new HashMap<String, Object>();
  85.  
  86.         temp.put("title", name);
  87.  
  88.         temp.put("intent", intent);        
  89.  
  90.         data.add(temp);
  91.  
  92.     }
  93.  
  94.    
  95.  
  96.     @Override
  97.  
  98.     public void onListItemClick(ListView l, View v, int position, long id) {
  99.  
  100.         Map map = (Map) l.getItemAtPosition(position);
  101.  
  102.         Intent intent = (Intent) map.get("intent");
  103.  
  104.         startActivity(intent);
  105.  
  106.     }    
  107.  
  108. }
  109.  
  110.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.         android:orientation="vertical"
  7.  
  8.         android:layout_width="fill_parent"
  9.  
  10.         android:layout_height="fill_parent">
  11.  
  12.  
  13.  
  14.         <ListView android:id="@+id/android:list"
  15.  
  16.                 android:layout_width="fill_parent"
  17.  
  18.                 android:layout_height="fill_parent" />
  19.  
  20.  
  21.  
  22.         <TextView android:id="@+id/android:empty"
  23.  
  24.                 android:layout_width="fill_parent"
  25.  
  26.                 android:layout_height="fill_parent"
  27.  
  28.         />
  29.  
  30. </LinearLayout>
  31.  
  32.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.         android:layout_width="fill_parent"
  7.  
  8.         android:layout_height="wrap_content"
  9.  
  10.         android:orientation="horizontal">
  11.  
  12.  
  13.  
  14.         <ImageView android:id="@+id/iv"
  15.  
  16.                 android:layout_width="wrap_content"
  17.  
  18.                 android:layout_height="wrap_content"
  19.  
  20.                 android:src="@drawable/soccer" />
  21.  
  22.        
  23.  
  24.         <TextView android:id="@+id/text1"
  25.  
  26.                 android:textSize="16px"
  27.  
  28.                 android:layout_width="fill_parent"
  29.  
  30.                 android:layout_height="fill_parent"
  31.  
  32.                 android:text="Text..."
  33.  
  34.                 android:paddingTop="13px"
  35.  
  36.                 android:paddingLeft="3px" />
  37.  
  38. </LinearLayout>
  39.  
  40.  
Parsed in 0.003 seconds, using GeSHi 1.0.8.4
Attachments
device.jpg
device.jpg (10.21 KiB) Viewed 837 times
Last edited by darolla on Sat Oct 25, 2008 2:55 pm, edited 1 time in total.
User avatar
darolla
Master Developer
Master Developer
 
Posts: 273
Joined: Thu Sep 25, 2008 5:16 pm
Location: Dortmund, Germany

Top

Postby darolla » Sat Oct 25, 2008 2:54 pm

Hello,

my first Idea is to overload the addItem like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public void addItem(List<Map<String,Object>> data, String name, Intent intent, int icon) {
  3.  
  4.         Map<String, Object> temp = new HashMap<String, Object>();
  5.  
  6.         temp.put("title", name);
  7.  
  8.         temp.put("intent", intent);
  9.  
  10.         temp.put("icon", icon);
  11.  
  12.         data.add(temp);
  13.  
  14. }
  15.  
  16.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


now I can add my pictures like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. Intent i = new Intent();
  3.  
  4. i.setClass(CustomList.this, Bets2Activity.class);
  5.  
  6. addItem( list, "Fußball (525)", i, R.drawable.some_picture1 );
  7.  
  8.        
  9.  
  10. i = new Intent();
  11.  
  12. i.setClass(CustomList.this, Bets3Activity.class);
  13.  
  14. addItem( list, "Deutschland (28)", i, R.drawable.some_picture2 );
  15.  
  16.                
  17.  
  18. i = new Intent();
  19.  
  20. i.setClass(CustomList.this, EventsActivity.class);
  21.  
  22. addItem( list, "1. Bundesliga (9)", i, R.drawable.some_picture3 );
  23.  
  24.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Greetings,
Marco
User avatar
darolla
Master Developer
Master Developer
 
Posts: 273
Joined: Thu Sep 25, 2008 5:16 pm
Location: Dortmund, Germany

Postby darolla » Sat Oct 25, 2008 3:13 pm

Hello,

the last thing should be to alter the Adapter like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. SimpleAdapter notes = new SimpleAdapter(this, list,
  3.  
  4.                 R.layout.customlist_item_two_line_row,
  5.  
  6.                 new String[] { "title", "icon" }, new int[] { R.id.text1, R.id.iv });
  7.  
  8. setListAdapter(notes);
  9.  
  10.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


And yes, it's working fine :)

Greetings,
Marco
Attachments
device.jpg
device.jpg (10.98 KiB) Viewed 827 times
User avatar
darolla
Master Developer
Master Developer
 
Posts: 273
Joined: Thu Sep 25, 2008 5:16 pm
Location: Dortmund, Germany

Postby Ramblurr » Sat Oct 25, 2008 3:37 pm

How would you right align the text, so the image and text are on the opposite sides of the row?
User avatar
Ramblurr
Freshman
Freshman
 
Posts: 4
Joined: Sat Oct 25, 2008 5:40 am

Postby darolla » Sat Oct 25, 2008 3:55 pm

that you would do inside this xml

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="wrap_content"
  5.     android:orientation="horizontal">
  6.  
  7.      <ImageView android:id="@+id/iv"
  8.          android:layout_width="wrap_content"
  9.          android:layout_height="wrap_content"
  10.          android:src="@drawable/soccer" />
  11.      
  12.      <TextView android:id="@+id/text1"
  13.          android:textSize="16px"
  14.          android:layout_width="fill_parent"
  15.          android:layout_height="fill_parent"
  16.          android:text="Text..."
  17.          android:paddingTop="13px"
  18.          android:paddingLeft="3px" />
  19. </LinearLayout>
  20.  
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


just play around a litte, eclipse will help u (after installing the plugin and showing the xml in design mode and the properties view opened)

greetings,
marco

ps: maybe u wanna get started with DroidDraw
User avatar
darolla
Master Developer
Master Developer
 
Posts: 273
Joined: Thu Sep 25, 2008 5:16 pm
Location: Dortmund, Germany

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 8 guests